|
1 | 1 | package github |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/json" |
4 | 5 | "fmt" |
5 | 6 | "net/http" |
6 | 7 | "net/url" |
7 | 8 | "testing" |
| 9 | + "time" |
8 | 10 |
|
9 | 11 | "github.com/google/go-cmp/cmp" |
10 | 12 | "github.com/google/go-github/v82/github" |
11 | 13 | ) |
12 | 14 |
|
13 | | -var ( |
14 | | - testTeamID = 432574718 |
15 | | - testGroupID = 1234567890 |
16 | | -) |
| 15 | +func buildMockResponsesForMigrationV0toV1(mockResponsesOptions mockResponsesOptionsEMUGroupMappingMigrationV0V1) []*mockResponse { |
| 16 | + responseBodyJson, err := json.Marshal(mockResponsesOptions.ExternalGroupList) |
| 17 | + if err != nil { |
| 18 | + panic(fmt.Sprintf("error marshalling external groups response: %s", err)) |
| 19 | + } |
17 | 20 |
|
18 | | -func buildMockResponsesForMigrationV0toV1() []*mockResponse { |
| 21 | + mockTeamResponseJson, err := json.Marshal(mockResponsesOptions.Team) |
| 22 | + if err != nil { |
| 23 | + panic(fmt.Sprintf("error marshalling mock team response: %s", err)) |
| 24 | + } |
19 | 25 | return []*mockResponse{ |
20 | 26 | { |
21 | | - ExpectedUri: fmt.Sprintf("/orgs/%s/teams/%s/external-groups", "test-org", "test-team"), |
| 27 | + ExpectedUri: fmt.Sprintf("/orgs/%s/teams/%s/external-groups", mockResponsesOptions.OrgSlug, mockResponsesOptions.TeamSlug), |
22 | 28 | ExpectedHeaders: map[string]string{ |
23 | 29 | "Accept": "application/vnd.github.v3+json", |
24 | 30 | }, |
25 | | - ResponseBody: fmt.Sprintf(` |
26 | | -{ |
27 | | - "groups": [ |
28 | | - { |
29 | | - "group_id": %d, |
30 | | - "group_name": "test-group", |
31 | | - "updated_at": "2021-01-24T11:31:04-06:00" |
32 | | - } |
33 | | - ] |
34 | | -}`, int64(testGroupID)), |
35 | | - StatusCode: 201, |
| 31 | + ResponseBody: string(responseBodyJson), |
| 32 | + StatusCode: mockResponsesOptions.externalGroupsResponseStatusCode, |
36 | 33 | }, |
37 | 34 | { |
38 | | - ExpectedUri: fmt.Sprintf("/orgs/%s/teams/%s", "test-org", "test-team"), |
| 35 | + ExpectedUri: fmt.Sprintf("/orgs/%s/teams/%s", mockResponsesOptions.OrgSlug, mockResponsesOptions.TeamSlug), |
39 | 36 | ExpectedHeaders: map[string]string{ |
40 | 37 | "Accept": "application/vnd.github.v3+json", |
41 | 38 | }, |
42 | | - ResponseBody: fmt.Sprintf(` |
43 | | -{ |
44 | | - "id": %d |
45 | | -} |
46 | | -`, testTeamID), |
47 | | - StatusCode: 200, |
| 39 | + ResponseBody: string(mockTeamResponseJson), |
| 40 | + StatusCode: mockResponsesOptions.teamResponseStatusCode, |
48 | 41 | }, |
49 | 42 | } |
50 | 43 | } |
51 | 44 |
|
| 45 | +type mockResponsesOptionsEMUGroupMappingMigrationV0V1 struct { |
| 46 | + OrgSlug string |
| 47 | + TeamSlug string |
| 48 | + externalGroupsResponseStatusCode int |
| 49 | + teamResponseStatusCode int |
| 50 | + ExternalGroupList github.ExternalGroupList |
| 51 | + Team github.Team |
| 52 | +} |
| 53 | + |
52 | 54 | func Test_resourceGithubEMUGroupMappingStateUpgradeV0(t *testing.T) { |
53 | 55 | t.Parallel() |
54 | 56 |
|
| 57 | + const testOrgSlug = "test-org" |
| 58 | + const testTeamSlug = "test-team" |
| 59 | + const testTeamID = 432574718 |
| 60 | + const testGroupID = 1234567890 |
| 61 | + |
55 | 62 | meta := &Owner{ |
56 | | - name: "test-org", |
| 63 | + name: testOrgSlug, |
57 | 64 | } |
58 | 65 |
|
59 | 66 | for _, d := range []struct { |
60 | | - testName string |
61 | | - rawState map[string]any |
62 | | - want map[string]any |
63 | | - buildMockResponses func() []*mockResponse |
64 | | - shouldError bool |
| 67 | + testName string |
| 68 | + rawState map[string]any |
| 69 | + want map[string]any |
| 70 | + shouldError bool |
| 71 | + mockResponsesOptions mockResponsesOptionsEMUGroupMappingMigrationV0V1 |
65 | 72 | }{ |
66 | 73 | { |
67 | 74 | testName: "migrates v0 to v1", |
68 | 75 | rawState: map[string]any{ |
69 | | - "id": "teams/test-team/external-groups", |
70 | | - "team_slug": "test-team", |
| 76 | + "id": fmt.Sprintf("teams/%s/%d/external-groups", testTeamSlug, testGroupID), |
| 77 | + "team_slug": testTeamSlug, |
71 | 78 | "group_id": testGroupID, |
72 | 79 | }, |
73 | 80 | want: map[string]any{ |
74 | | - "id": "432574718:test-team:1234567890", |
75 | | - "team_slug": "test-team", |
| 81 | + "id": fmt.Sprintf("%d:%s:%d", testTeamID, testTeamSlug, testGroupID), |
| 82 | + "team_slug": testTeamSlug, |
76 | 83 | "team_id": int64(testTeamID), |
77 | 84 | "group_id": testGroupID, |
78 | 85 | }, |
79 | | - buildMockResponses: buildMockResponsesForMigrationV0toV1, |
80 | | - shouldError: false, |
| 86 | + shouldError: false, |
| 87 | + mockResponsesOptions: mockResponsesOptionsEMUGroupMappingMigrationV0V1{ |
| 88 | + OrgSlug: testOrgSlug, |
| 89 | + TeamSlug: testTeamSlug, |
| 90 | + externalGroupsResponseStatusCode: 201, |
| 91 | + teamResponseStatusCode: 200, |
| 92 | + ExternalGroupList: github.ExternalGroupList{ |
| 93 | + Groups: []*github.ExternalGroup{{ |
| 94 | + GroupID: github.Ptr(int64(testGroupID)), |
| 95 | + GroupName: github.Ptr(testOrgSlug), |
| 96 | + UpdatedAt: github.Ptr(github.Timestamp{Time: time.Now()}), |
| 97 | + }}, |
| 98 | + }, |
| 99 | + Team: github.Team{ |
| 100 | + ID: github.Ptr(int64(testTeamID)), |
| 101 | + }, |
| 102 | + }, |
81 | 103 | }, |
82 | 104 | } { |
83 | 105 | t.Run(d.testName, func(t *testing.T) { |
84 | 106 | t.Parallel() |
85 | 107 |
|
86 | | - ts := githubApiMock(d.buildMockResponses()) |
| 108 | + ts := githubApiMock(buildMockResponsesForMigrationV0toV1(d.mockResponsesOptions)) |
87 | 109 | defer ts.Close() |
88 | 110 |
|
89 | 111 | httpCl := http.DefaultClient |
|
0 commit comments