|
| 1 | +package projects_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "ld-cli/internal/projects" |
| 6 | + "testing" |
| 7 | + |
| 8 | + ldapi "github.com/launchdarkly/api-client-go/v14" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | +) |
| 13 | + |
| 14 | +func strPtr(s string) *string { |
| 15 | + return &s |
| 16 | +} |
| 17 | + |
| 18 | +type GetProjectsResponse struct{} |
| 19 | + |
| 20 | +type MockClient struct{} |
| 21 | + |
| 22 | +func (c MockClient) List(ctx context.Context) (*ldapi.Projects, error) { |
| 23 | + totalCount := int32(1) |
| 24 | + |
| 25 | + return &ldapi.Projects{ |
| 26 | + Links: map[string]ldapi.Link{ |
| 27 | + "last": { |
| 28 | + Href: strPtr("/api/v2/projects?expand=environments&limit=1&offset=1"), |
| 29 | + Type: strPtr("application/json"), |
| 30 | + }, |
| 31 | + "next": { |
| 32 | + Href: strPtr("/api/v2/projects?expand=environments&limit=1&offset=0"), |
| 33 | + Type: strPtr("application/json"), |
| 34 | + }, |
| 35 | + "self": { |
| 36 | + Href: strPtr("/api/v2/projects?expand=environments&limit=1"), |
| 37 | + Type: strPtr("application/json"), |
| 38 | + }, |
| 39 | + }, |
| 40 | + Items: []ldapi.Project{ |
| 41 | + { |
| 42 | + Id: "000000000000000000000001", |
| 43 | + Key: "test-project", |
| 44 | + }, |
| 45 | + }, |
| 46 | + TotalCount: &totalCount, |
| 47 | + }, nil |
| 48 | +} |
| 49 | + |
| 50 | +func TestListProjects(t *testing.T) { |
| 51 | + t.Run("returns a paginated list of projects", func(t *testing.T) { |
| 52 | + expected := `{ |
| 53 | + "_links": { |
| 54 | + "last": { |
| 55 | + "href": "/api/v2/projects?expand=environments&limit=1&offset=1", |
| 56 | + "type": "application/json" |
| 57 | + }, |
| 58 | + "next": { |
| 59 | + "href": "/api/v2/projects?expand=environments&limit=1&offset=0", |
| 60 | + "type": "application/json" |
| 61 | + }, |
| 62 | + "self": { |
| 63 | + "href": "/api/v2/projects?expand=environments&limit=1", |
| 64 | + "type": "application/json" |
| 65 | + } |
| 66 | + }, |
| 67 | + "items": [ |
| 68 | + { |
| 69 | + "_id": "000000000000000000000001", |
| 70 | + "_links": null, |
| 71 | + "includeInSnippetByDefault": false, |
| 72 | + "key": "test-project", |
| 73 | + "name": "", |
| 74 | + "tags": null |
| 75 | + } |
| 76 | + ], |
| 77 | + "totalCount": 1 |
| 78 | + }` |
| 79 | + mockClient := MockClient{} |
| 80 | + |
| 81 | + response, err := projects.ListProjects(context.Background(), mockClient) |
| 82 | + |
| 83 | + require.NoError(t, err) |
| 84 | + assert.JSONEq(t, expected, string(response)) |
| 85 | + }) |
| 86 | + |
| 87 | + t.Run("with invalid accessToken is forbidden", func(t *testing.T) { |
| 88 | + }) |
| 89 | +} |
0 commit comments