-
Notifications
You must be signed in to change notification settings - Fork 117
test(auth): cover hyphenated whitelist owner matching #188
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| package auth | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "ghproxy/config" | ||
| ) | ||
|
|
||
| func TestSplitUserRepoWhitelist_PreservesHyphenatedOwner(t *testing.T) { | ||
| user, repo := splitUserRepoWhitelist("jow-/ucode") | ||
|
|
||
| if user != "jow-" { | ||
| t.Fatalf("user = %q, want %q", user, "jow-") | ||
| } | ||
| if repo != "ucode" { | ||
| t.Fatalf("repo = %q, want %q", repo, "ucode") | ||
| } | ||
| } | ||
|
|
||
| func TestInitWhitelist_AllowsHyphenatedOwnerRepo(t *testing.T) { | ||
| tmpDir := t.TempDir() | ||
| whitelistPath := filepath.Join(tmpDir, "whitelist.json") | ||
|
|
||
| content := []byte(`{"whitelist":["jow-/ucode"]}`) | ||
| if err := os.WriteFile(whitelistPath, content, 0o600); err != nil { | ||
| t.Fatalf("write whitelist file: %v", err) | ||
| } | ||
|
|
||
| whitelistInstance = nil | ||
| whitelistInitErr = nil | ||
|
|
||
| cfg := &config.Config{} | ||
| cfg.Whitelist.WhitelistFile = whitelistPath | ||
|
|
||
| if err := InitWhitelist(cfg); err != nil { | ||
| t.Fatalf("InitWhitelist() error = %v", err) | ||
| } | ||
|
|
||
| if !CheckWhitelist("jow-", "ucode") { | ||
| t.Fatal("CheckWhitelist() = false, want true for jow-/ucode") | ||
| } | ||
|
|
||
| if CheckWhitelist("jow", "ucode") { | ||
| t.Fatal("CheckWhitelist() = true, want false for non-hyphenated owner") | ||
| } | ||
|
|
||
| if CheckWhitelist("jow-", "other") { | ||
| t.Fatal("CheckWhitelist() = true, want false for unmatched repo") | ||
| } | ||
|
|
||
| t.Cleanup(func() { | ||
| whitelistInstance = nil | ||
| whitelistInitErr = nil | ||
| }) | ||
|
Comment on lines
+31
to
+56
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 将 当前在 Line 53 才注册清理;如果 Line 37-50 之间触发 🛠️ 建议修改- whitelistInstance = nil
- whitelistInitErr = nil
+ prevWhitelistInstance := whitelistInstance
+ prevWhitelistInitErr := whitelistInitErr
+ t.Cleanup(func() {
+ whitelistInstance = prevWhitelistInstance
+ whitelistInitErr = prevWhitelistInitErr
+ })
+ whitelistInstance = nil
+ whitelistInitErr = nil
@@
- t.Cleanup(func() {
- whitelistInstance = nil
- whitelistInitErr = nil
- })Based on learnings: The blacklist and whitelist functionality in auth/ package uses maps for O(1) lookups, separates user-level and repo-level controls, and ensures thread safety using sync.Once. 🤖 Prompt for AI Agents |
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
t.Cleanupfunction should be registered immediately after the global variables are modified. In its current position at the end of the test, it will not be executed ifInitWhitelistfails or if any assertion fails before the registration point. Moving it up ensures that the global state is properly reset even on test failure, preventing side effects on other tests in the same package.