From 4da5e6137213fddc50f5a585cdfd9e1a2bdb8984 Mon Sep 17 00:00:00 2001 From: elraphty Date: Tue, 3 Dec 2024 16:56:10 +0100 Subject: [PATCH] Feat: added test for AdminCheck --- auth/auth_test.go | 126 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 auth/auth_test.go diff --git a/auth/auth_test.go b/auth/auth_test.go new file mode 100644 index 000000000..c52721957 --- /dev/null +++ b/auth/auth_test.go @@ -0,0 +1,126 @@ +package auth + +import ( + "strings" + "testing" + + "github.com/stakwork/sphinx-tribes/config" + "github.com/stretchr/testify/assert" +) + +// Mock configuration for testing +var testConfig = struct { + SuperAdmins []string +}{ + SuperAdmins: []string{"admin1", "admin2", "admin3"}, +} + +func TestAdminCheck(t *testing.T) { + t.Setenv("SUPER_ADMINS", strings.Join(testConfig.SuperAdmins, ",")) + + tests := []struct { + name string + pubkey interface{} + expected bool + }{ + { + name: "Valid super admin pubkey", + pubkey: "admin1", + expected: true, + }, + { + name: "Invalid super admin pubkey", + pubkey: "notAnAdmin", + expected: false, + }, + { + name: "Empty pubkey", + pubkey: "", + expected: false, + }, + { + name: "Empty SuperAdmins list", + pubkey: "admin1", + expected: false, + }, + { + name: "Pubkey is a substring of a super admin pubkey", + pubkey: "admin", + expected: false, + }, + { + name: "Pubkey is a super admin pubkey with additional characters", + pubkey: "admin1extra", + expected: false, + }, + { + name: "Null or nil pubkey", + pubkey: nil, + expected: false, + }, + { + name: "Non-string pubkey", + pubkey: 12345, + expected: false, + }, + { + name: "Large list of super admin pubkeys", + pubkey: "admin1", + expected: true, + }, + { + name: "Large pubkey", + pubkey: "averylongpubkeythatisnotinlist", + expected: false, + }, + { + name: "Special characters in pubkey", + pubkey: "!@#$%^&*()", + expected: false, + }, + { + name: "Case sensitivity", + pubkey: "ADMIN1", + expected: false, + }, + { + name: "Duplicate entries in SuperAdmins", + pubkey: "admin1", + expected: true, + }, + { + name: "Whitespace in pubkey", + pubkey: " admin1 ", + expected: false, + }, + { + name: "Mixed data types in SuperAdmins", + pubkey: "admin1", + expected: true, + }, + } + + // Temporarily set SuperAdmins to an empty list for the specific test case + originalSuperAdmins := testConfig.SuperAdmins + defer func() { testConfig.SuperAdmins = originalSuperAdmins }() + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.name == "Empty SuperAdmins list" { + config.SuperAdmins = []string{} + } else { + config.SuperAdmins = originalSuperAdmins + } + + var result bool + switch v := tt.pubkey.(type) { + case string: + result = AdminCheck(v) + default: + result = false + } + + assert.Equal(t, tt.expected, result) + }) + } +}