Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Unit Tests] - AdminCheck #2071

Open
tomsmith8 opened this issue Dec 3, 2024 · 0 comments
Open

[Unit Tests] - AdminCheck #2071

tomsmith8 opened this issue Dec 3, 2024 · 0 comments

Comments

@tomsmith8
Copy link

Unit Test Coverage for "AdminCheck"


Stakwork Run


Unit Test Code


package auth

import (
  "testing"

  "github.com/stretchr/testify/assert"
)

// Mock configuration for testing
var config struct {
  SuperAdmins []string
}

func TestAdminCheck(t *testing.T) {
  // Test case 1: Valid super admin pubkey
  t.Run("ValidSuperAdminPubkey", func(t *testing.T) {
  	config.SuperAdmins = []string{"admin1", "admin2", "admin3"}
  	pubkey := "admin1"
  	result := AdminCheck(pubkey)
  	assert.True(t, result)
  })

  // Test case 2: Invalid super admin pubkey
  t.Run("InvalidSuperAdminPubkey", func(t *testing.T) {
  	config.SuperAdmins = []string{"admin1", "admin2", "admin3"}
  	pubkey := "notAnAdmin"
  	result := AdminCheck(pubkey)
  	assert.False(t, result)
  })

  // Test case 3: Empty pubkey
  t.Run("EmptyPubkey", func(t *testing.T) {
  	config.SuperAdmins = []string{"admin1", "admin2", "admin3"}
  	pubkey := ""
  	result := AdminCheck(pubkey)
  	assert.False(t, result)
  })

  // Test case 4: Empty config.SuperAdmins list
  t.Run("EmptySuperAdminsList", func(t *testing.T) {
  	config.SuperAdmins = []string{}
  	pubkey := "admin1"
  	result := AdminCheck(pubkey)
  	assert.False(t, result)
  })

  // Test case 5: Pubkey is a substring of a valid super admin pubkey
  t.Run("PubkeySubstringOfValidAdmin", func(t *testing.T) {
  	config.SuperAdmins = []string{"admin1", "admin2", "admin3"}
  	pubkey := "admin"
  	result := AdminCheck(pubkey)
  	assert.False(t, result)
  })

  // Test case 6: Null or None pubkey (not applicable in Go, but test empty string)
  t.Run("NullPubkey", func(t *testing.T) {
  	config.SuperAdmins = []string{"admin1", "admin2", "admin3"}
  	var pubkey string // zero value is an empty string
  	result := AdminCheck(pubkey)
  	assert.False(t, result)
  })

  // Test case 7: Null or None config.SuperAdmins (not applicable in Go, but test empty list)
  t.Run("NullSuperAdmins", func(t *testing.T) {
  	config.SuperAdmins = nil
  	pubkey := "admin1"
  	result := AdminCheck(pubkey)
  	assert.False(t, result)
  })

  // Test case 8: Large config.SuperAdmins list with valid pubkey
  t.Run("LargeSuperAdminsListValidPubkey", func(t *testing.T) {
  	config.SuperAdmins = make([]string, 10000)
  	for i := 0; i < 9999; i++ {
  		config.SuperAdmins[i] = "admin" + string(i)
  	}
  	config.SuperAdmins[9999] = "validAdmin"
  	pubkey := "validAdmin"
  	result := AdminCheck(pubkey)
  	assert.True(t, result)
  })

  // Test case 9: Large config.SuperAdmins list with invalid pubkey
  t.Run("LargeSuperAdminsListInvalidPubkey", func(t *testing.T) {
  	config.SuperAdmins = make([]string, 10000)
  	for i := 0; i < 10000; i++ {
  		config.SuperAdmins[i] = "admin" + string(i)
  	}
  	pubkey := "notInList"
  	result := AdminCheck(pubkey)
  	assert.False(t, result)
  })

  // Test case 10: Special characters in pubkey
  t.Run("SpecialCharactersInPubkey", func(t *testing.T) {
  	config.SuperAdmins = []string{"admin1", "admin2", "admin@#!"}
  	pubkey := "admin@#!"
  	result := AdminCheck(pubkey)
  	assert.True(t, result)
  })

  // Test case 11: Case sensitivity
  t.Run("CaseSensitivity", func(t *testing.T) {
  	config.SuperAdmins = []string{"Admin1", "admin2", "admin3"}
  	pubkey := "admin1"
  	result := AdminCheck(pubkey)
  	assert.False(t, result) // Assuming case-sensitive
  })

  // Test case 12: Duplicate entries in config.SuperAdmins
  t.Run("DuplicateEntriesInSuperAdmins", func(t *testing.T) {
  	config.SuperAdmins = []string{"admin1", "admin1", "admin2"}
  	pubkey := "admin1"
  	result := AdminCheck(pubkey)
  	assert.True(t, result)
  })

  // Test case 13: Pubkey with leading/trailing spaces
  t.Run("PubkeyWithLeadingTrailingSpaces", func(t *testing.T) {
  	config.SuperAdmins = []string{"admin1", "admin2", "admin3"}
  	pubkey := " admin1 "
  	result := AdminCheck(pubkey)
  	assert.False(t, result) // Assuming spaces are not trimmed
  })
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant