|
| 1 | +package scmclient |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "io" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | +) |
| 11 | + |
| 12 | +func TestBitbucketCloud_GetFileContent_BasicAuth(t *testing.T) { |
| 13 | + fileContent := []byte("apiVersion: tekton.dev/v1\nkind: Task\n") |
| 14 | + |
| 15 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 16 | + if r.URL.Path != "/2.0/repositories/test_org/test_repo/src/main/tasks/build.yaml" { |
| 17 | + t.Errorf("unexpected path: %s", r.URL.Path) |
| 18 | + } |
| 19 | + // Basic auth — username:api_token format |
| 20 | + username, password, ok := r.BasicAuth() |
| 21 | + if !ok { |
| 22 | + t.Error("expected Basic auth, got none") |
| 23 | + } |
| 24 | + if username != "test_user" { |
| 25 | + t.Errorf("unexpected username: %s", username) |
| 26 | + } |
| 27 | + if password != "test_apitoken" { |
| 28 | + t.Errorf("unexpected password: %s", password) |
| 29 | + } |
| 30 | + w.Write(fileContent) |
| 31 | + })) |
| 32 | + defer server.Close() |
| 33 | + |
| 34 | + client := newBitbucketCloudClient(server.URL, "test_user:test_apitoken") |
| 35 | + got, err := client.GetFileContent(context.Background(), "test_org", "test_repo", "tasks/build.yaml", "main") |
| 36 | + if err != nil { |
| 37 | + t.Fatalf("unexpected error: %v", err) |
| 38 | + } |
| 39 | + if string(got) != string(fileContent) { |
| 40 | + t.Errorf("got %q, want %q", got, fileContent) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +func TestBitbucketCloud_GetFileContent_BearerAuth(t *testing.T) { |
| 45 | + fileContent := []byte("apiVersion: tekton.dev/v1\nkind: Task\n") |
| 46 | + |
| 47 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 48 | + // Bearer auth — bare token, no username |
| 49 | + authHeader := r.Header.Get("Authorization") |
| 50 | + if authHeader != "Bearer test_repotoken" { |
| 51 | + t.Errorf("unexpected Authorization header: %s", authHeader) |
| 52 | + } |
| 53 | + _, _, ok := r.BasicAuth() |
| 54 | + if ok { |
| 55 | + t.Error("expected Bearer auth, got Basic auth") |
| 56 | + } |
| 57 | + w.Write(fileContent) |
| 58 | + })) |
| 59 | + defer server.Close() |
| 60 | + |
| 61 | + client := newBitbucketCloudClient(server.URL, "test_repotoken") |
| 62 | + got, err := client.GetFileContent(context.Background(), "test_org", "test_repo", "tasks/build.yaml", "main") |
| 63 | + if err != nil { |
| 64 | + t.Fatalf("unexpected error: %v", err) |
| 65 | + } |
| 66 | + if string(got) != string(fileContent) { |
| 67 | + t.Errorf("got %q, want %q", got, fileContent) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func TestBitbucketCloud_GetFileContent_Error(t *testing.T) { |
| 72 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 73 | + w.WriteHeader(http.StatusNotFound) |
| 74 | + w.Write([]byte(`{"type": "error", "error": {"message": "Not Found"}}`)) |
| 75 | + })) |
| 76 | + defer server.Close() |
| 77 | + |
| 78 | + client := newBitbucketCloudClient(server.URL, "test_user:test_apitoken") |
| 79 | + _, err := client.GetFileContent(context.Background(), "test_org", "test_repo", "missing.yaml", "main") |
| 80 | + if err == nil { |
| 81 | + t.Fatal("expected error for 404, got nil") |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +func TestBitbucketCloud_GetFileContent_RawBytes(t *testing.T) { |
| 86 | + // Verify raw bytes are returned directly — no base64 decoding |
| 87 | + rawContent := []byte("raw: content\nno: encoding\n") |
| 88 | + |
| 89 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 90 | + w.Write(rawContent) |
| 91 | + })) |
| 92 | + defer server.Close() |
| 93 | + |
| 94 | + client := newBitbucketCloudClient(server.URL, "test_user:test_apitoken") |
| 95 | + got, err := client.GetFileContent(context.Background(), "test_org", "test_repo", "file.yaml", "main") |
| 96 | + if err != nil { |
| 97 | + t.Fatalf("unexpected error: %v", err) |
| 98 | + } |
| 99 | + if string(got) != string(rawContent) { |
| 100 | + t.Errorf("got %q, want %q", got, rawContent) |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +func TestBitbucketCloud_GetCommitSHA(t *testing.T) { |
| 105 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 106 | + if r.URL.Path != "/2.0/repositories/test_org/test_repo/commit/main" { |
| 107 | + t.Errorf("unexpected path: %s", r.URL.Path) |
| 108 | + } |
| 109 | + // Bitbucket Cloud uses "hash" not "sha" |
| 110 | + io.WriteString(w, `{"hash": "abc123def456abc123def456abc123def456abc1"}`) |
| 111 | + })) |
| 112 | + defer server.Close() |
| 113 | + |
| 114 | + client := newBitbucketCloudClient(server.URL, "test_user:test_apitoken") |
| 115 | + got, err := client.GetCommitSHA(context.Background(), "test_org", "test_repo", "main") |
| 116 | + if err != nil { |
| 117 | + t.Fatalf("unexpected error: %v", err) |
| 118 | + } |
| 119 | + if got != "abc123def456abc123def456abc123def456abc1" { |
| 120 | + t.Errorf("got %q, want %q", got, "abc123def456abc123def456abc123def456abc1") |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +func TestBitbucketCloud_GetCommitSHA_EmptyHash(t *testing.T) { |
| 125 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 126 | + io.WriteString(w, `{"hash": ""}`) |
| 127 | + })) |
| 128 | + defer server.Close() |
| 129 | + |
| 130 | + client := newBitbucketCloudClient(server.URL, "test_user:test_apitoken") |
| 131 | + _, err := client.GetCommitSHA(context.Background(), "test_org", "test_repo", "main") |
| 132 | + if err == nil { |
| 133 | + t.Fatal("expected error for empty hash, got nil") |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +func TestBitbucketCloud_GetCloneURL(t *testing.T) { |
| 138 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 139 | + if r.URL.Path != "/2.0/repositories/test_org/test_repo" { |
| 140 | + t.Errorf("unexpected path: %s", r.URL.Path) |
| 141 | + } |
| 142 | + io.WriteString(w, `{ |
| 143 | + "links": { |
| 144 | + "clone": [ |
| 145 | + {"name": "ssh", "href": "git@bitbucket.org:test_org/test_repo.git"}, |
| 146 | + {"name": "https", "href": "https://test_user@bitbucket.org/test_org/test_repo.git"} |
| 147 | + ] |
| 148 | + } |
| 149 | + }`) |
| 150 | + })) |
| 151 | + defer server.Close() |
| 152 | + |
| 153 | + client := newBitbucketCloudClient(server.URL, "test_user:test_apitoken") |
| 154 | + got, err := client.GetCloneURL(context.Background(), "test_org", "test_repo") |
| 155 | + if err != nil { |
| 156 | + t.Fatalf("unexpected error: %v", err) |
| 157 | + } |
| 158 | + if got != "https://test_user@bitbucket.org/test_org/test_repo.git" { |
| 159 | + t.Errorf("got %q, want %q", got, "https://test_user@bitbucket.org/test_org/test_repo.git") |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +func TestBitbucketCloud_GetCloneURL_NoHTTPS(t *testing.T) { |
| 164 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 165 | + // Only SSH link, no HTTPS |
| 166 | + io.WriteString(w, `{ |
| 167 | + "links": { |
| 168 | + "clone": [ |
| 169 | + {"name": "ssh", "href": "git@bitbucket.org:test_org/test_repo.git"} |
| 170 | + ] |
| 171 | + } |
| 172 | + }`) |
| 173 | + })) |
| 174 | + defer server.Close() |
| 175 | + |
| 176 | + client := newBitbucketCloudClient(server.URL, "test_user:test_apitoken") |
| 177 | + _, err := client.GetCloneURL(context.Background(), "test_org", "test_repo") |
| 178 | + if err == nil { |
| 179 | + t.Fatal("expected error when no https clone URL found, got nil") |
| 180 | + } |
| 181 | + if !strings.Contains(err.Error(), "no https clone URL found") { |
| 182 | + t.Errorf("unexpected error message: %v", err) |
| 183 | + } |
| 184 | +} |
| 185 | + |
| 186 | +func TestBitbucketCloud_splitUsernamePassword(t *testing.T) { |
| 187 | + tests := []struct { |
| 188 | + token string |
| 189 | + wantUsername string |
| 190 | + wantPassword string |
| 191 | + }{ |
| 192 | + {"test_user:test_password", "test_user", "test_password"}, |
| 193 | + {"test_user:pass:with:colons", "test_user", "pass:with:colons"}, |
| 194 | + {"baretoken", "", "baretoken"}, |
| 195 | + {":emptyusername", "", "emptyusername"}, |
| 196 | + } |
| 197 | + for _, tt := range tests { |
| 198 | + gotUsername, gotPassword := splitUsernamePassword(tt.token) |
| 199 | + if gotUsername != tt.wantUsername { |
| 200 | + t.Errorf("splitUsernamePassword(%q) username = %q, want %q", tt.token, gotUsername, tt.wantUsername) |
| 201 | + } |
| 202 | + if gotPassword != tt.wantPassword { |
| 203 | + t.Errorf("splitUsernamePassword(%q) password = %q, want %q", tt.token, gotPassword, tt.wantPassword) |
| 204 | + } |
| 205 | + } |
| 206 | +} |
0 commit comments