|
| 1 | +package ghcr |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/google/go-github/v62/github" |
| 9 | + "github.com/jarcoal/httpmock" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | +) |
| 12 | + |
| 13 | +func setup() { |
| 14 | + httpmock.Activate() |
| 15 | +} |
| 16 | + |
| 17 | +func teardown() { |
| 18 | + httpmock.DeactivateAndReset() |
| 19 | +} |
| 20 | + |
| 21 | +func registerCommonResponders() { |
| 22 | + httpmock.RegisterResponder("GET", "https://api.github.com/users/test-user-owner", |
| 23 | + func(req *http.Request) (*http.Response, error) { |
| 24 | + return httpmock.NewStringResponse(200, `{"type":"User"}`), nil |
| 25 | + }) |
| 26 | + httpmock.RegisterResponder("GET", "https://api.github.com/users/test-org-owner", |
| 27 | + func(req *http.Request) (*http.Response, error) { |
| 28 | + return httpmock.NewStringResponse(200, `{"type":"Organization"}`), nil |
| 29 | + }) |
| 30 | +} |
| 31 | + |
| 32 | +func registerTagResponders() { |
| 33 | + httpmock.RegisterResponder("GET", "https://api.github.com/users/test-user-owner/packages/container/test-repo/versions", |
| 34 | + func(req *http.Request) (*http.Response, error) { |
| 35 | + return httpmock.NewStringResponse(200, `[ |
| 36 | + { |
| 37 | + "name": "sha123", |
| 38 | + "metadata": { |
| 39 | + "container": { |
| 40 | + "tags": ["tag1", "tag2"] |
| 41 | + } |
| 42 | + }, |
| 43 | + "created_at": "2023-07-08T12:34:56Z" |
| 44 | + } |
| 45 | + ]`), nil |
| 46 | + }) |
| 47 | + httpmock.RegisterResponder("GET", "https://api.github.com/orgs/test-org-owner/packages/container/test-repo/versions", |
| 48 | + func(req *http.Request) (*http.Response, error) { |
| 49 | + return httpmock.NewStringResponse(200, `[ |
| 50 | + { |
| 51 | + "name": "sha123", |
| 52 | + "metadata": { |
| 53 | + "container": { |
| 54 | + "tags": ["tag1", "tag2"] |
| 55 | + } |
| 56 | + }, |
| 57 | + "created_at": "2023-07-08T12:34:56Z" |
| 58 | + } |
| 59 | + ]`), nil |
| 60 | + }) |
| 61 | +} |
| 62 | + |
| 63 | +func TestClient_Tags(t *testing.T) { |
| 64 | + setup() |
| 65 | + defer teardown() |
| 66 | + |
| 67 | + ctx := context.Background() |
| 68 | + host := "ghcr.io" |
| 69 | + |
| 70 | + t.Run("successful tags fetch", func(t *testing.T) { |
| 71 | + httpmock.Reset() |
| 72 | + registerCommonResponders() |
| 73 | + registerTagResponders() |
| 74 | + |
| 75 | + client := New(Options{}) |
| 76 | + client.client = github.NewClient(nil) // Use the default HTTP client |
| 77 | + |
| 78 | + tags, err := client.Tags(ctx, host, "test-user-owner", "test-repo") |
| 79 | + assert.NoError(t, err) |
| 80 | + assert.Len(t, tags, 2) |
| 81 | + assert.Equal(t, "tag1", tags[0].Tag) |
| 82 | + assert.Equal(t, "tag2", tags[1].Tag) |
| 83 | + }) |
| 84 | + |
| 85 | + t.Run("failed to fetch owner type", func(t *testing.T) { |
| 86 | + httpmock.Reset() |
| 87 | + httpmock.RegisterResponder("GET", "https://api.github.com/users/test-user-owner", |
| 88 | + func(req *http.Request) (*http.Response, error) { |
| 89 | + return httpmock.NewStringResponse(404, `{"message": "Not Found"}`), nil |
| 90 | + }) |
| 91 | + |
| 92 | + client := New(Options{}) |
| 93 | + client.client = github.NewClient(nil) // Use the default HTTP client |
| 94 | + |
| 95 | + _, err := client.Tags(ctx, host, "test-user-owner", "test-repo") |
| 96 | + assert.Error(t, err) |
| 97 | + }) |
| 98 | + |
| 99 | + t.Run("token not set, no authorization header", func(t *testing.T) { |
| 100 | + httpmock.Reset() |
| 101 | + httpmock.RegisterResponder("GET", "https://api.github.com/users/test-user-owner", |
| 102 | + func(req *http.Request) (*http.Response, error) { |
| 103 | + if req.Header.Get("Authorization") != "" { |
| 104 | + t.Errorf("expected no Authorization header, got %s", req.Header.Get("Authorization")) |
| 105 | + } |
| 106 | + return httpmock.NewStringResponse(200, `{"type":"User"}`), nil |
| 107 | + }) |
| 108 | + registerTagResponders() |
| 109 | + |
| 110 | + client := New(Options{}) // No token provided |
| 111 | + client.client = github.NewClient(nil) |
| 112 | + |
| 113 | + _, err := client.Tags(ctx, host, "test-user-owner", "test-repo") |
| 114 | + assert.NoError(t, err) |
| 115 | + }) |
| 116 | + |
| 117 | + t.Run("token set, authorization header sent", func(t *testing.T) { |
| 118 | + token := "test-token" |
| 119 | + httpmock.Reset() |
| 120 | + httpmock.RegisterResponder("GET", "https://api.github.com/users/test-user-owner", |
| 121 | + func(req *http.Request) (*http.Response, error) { |
| 122 | + authHeader := req.Header.Get("Authorization") |
| 123 | + expectedAuthHeader := "Bearer " + token |
| 124 | + if authHeader != expectedAuthHeader { |
| 125 | + t.Errorf("expected Authorization header %s, got %s", expectedAuthHeader, authHeader) |
| 126 | + } |
| 127 | + return httpmock.NewStringResponse(200, `{"type":"User"}`), nil |
| 128 | + }) |
| 129 | + |
| 130 | + registerTagResponders() |
| 131 | + |
| 132 | + client := New(Options{Token: token}) |
| 133 | + |
| 134 | + _, err := client.Tags(ctx, host, "test-user-owner", "test-repo") |
| 135 | + assert.NoError(t, err) |
| 136 | + }) |
| 137 | + |
| 138 | + t.Run("ownerType returns user", func(t *testing.T) { |
| 139 | + httpmock.Reset() |
| 140 | + registerCommonResponders() |
| 141 | + registerTagResponders() |
| 142 | + |
| 143 | + client := New(Options{}) |
| 144 | + client.client = github.NewClient(nil) // Use the default HTTP client |
| 145 | + |
| 146 | + tags, err := client.Tags(ctx, host, "test-user-owner", "test-repo") |
| 147 | + assert.NoError(t, err) |
| 148 | + assert.Len(t, tags, 2) |
| 149 | + assert.Equal(t, "tag1", tags[0].Tag) |
| 150 | + assert.Equal(t, "tag2", tags[1].Tag) |
| 151 | + }) |
| 152 | + |
| 153 | + t.Run("ownerType returns org", func(t *testing.T) { |
| 154 | + httpmock.Reset() |
| 155 | + registerCommonResponders() |
| 156 | + registerTagResponders() |
| 157 | + |
| 158 | + client := New(Options{}) |
| 159 | + client.client = github.NewClient(nil) // Use the default HTTP client |
| 160 | + |
| 161 | + tags, err := client.Tags(ctx, host, "test-org-owner", "test-repo") |
| 162 | + assert.NoError(t, err) |
| 163 | + assert.Len(t, tags, 2) |
| 164 | + assert.Equal(t, "tag1", tags[0].Tag) |
| 165 | + assert.Equal(t, "tag2", tags[1].Tag) |
| 166 | + }) |
| 167 | +} |
0 commit comments