11
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func writeTeaConfig(t *testing.T, content string) string {
|
||||
t.Helper()
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "config.yml")
|
||||
if err := os.WriteFile(path, []byte(content), 0o600); err != nil {
|
||||
t.Fatalf("write tea config: %v", err)
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
func TestResolveGiteaToken_EnvVarTakesPriority(t *testing.T) {
|
||||
t.Setenv("GITEA_TOKEN", "envtoken123")
|
||||
path := writeTeaConfig(t, `logins:
|
||||
- name: test
|
||||
url: https://gitea.example.com
|
||||
token: yamltoken
|
||||
default: true
|
||||
`)
|
||||
tok, err := resolveGiteaToken("https://gitea.example.com", path)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if tok != "envtoken123" {
|
||||
t.Errorf("got %q, want envtoken123", tok)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveGiteaToken_DefaultLogin(t *testing.T) {
|
||||
t.Setenv("GITEA_TOKEN", "")
|
||||
path := writeTeaConfig(t, `logins:
|
||||
- name: work
|
||||
url: https://gitea.work.com
|
||||
token: worktoken
|
||||
default: false
|
||||
- name: personal
|
||||
url: https://gitea.personal.com
|
||||
token: personaltoken
|
||||
default: true
|
||||
`)
|
||||
tok, err := resolveGiteaToken("https://unknown.host.com", path)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if tok != "personaltoken" {
|
||||
t.Errorf("got %q, want personaltoken", tok)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveGiteaToken_HostMatch(t *testing.T) {
|
||||
t.Setenv("GITEA_TOKEN", "")
|
||||
path := writeTeaConfig(t, `logins:
|
||||
- name: work
|
||||
url: https://gitea.work.com
|
||||
token: worktoken
|
||||
default: false
|
||||
- name: personal
|
||||
url: https://gitea.personal.com
|
||||
token: personaltoken
|
||||
default: true
|
||||
`)
|
||||
tok, err := resolveGiteaToken("https://gitea.work.com", path)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if tok != "worktoken" {
|
||||
t.Errorf("got %q, want worktoken", tok)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveGiteaToken_EmptyLogins(t *testing.T) {
|
||||
t.Setenv("GITEA_TOKEN", "")
|
||||
path := writeTeaConfig(t, `logins: []`)
|
||||
_, err := resolveGiteaToken("https://gitea.example.com", path)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for empty logins")
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveGiteaToken_FirstLoginFallback(t *testing.T) {
|
||||
t.Setenv("GITEA_TOKEN", "")
|
||||
path := writeTeaConfig(t, `logins:
|
||||
- name: first
|
||||
url: https://gitea.first.com
|
||||
token: firsttoken
|
||||
default: false
|
||||
- name: second
|
||||
url: https://gitea.second.com
|
||||
token: secondtoken
|
||||
default: false
|
||||
`)
|
||||
tok, err := resolveGiteaToken("https://unknown.host.com", path)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if tok != "firsttoken" {
|
||||
t.Errorf("got %q, want firsttoken", tok)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user