|
1 | 1 | package clouduserprojectassignment_test
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "context" |
5 | 4 | "testing"
|
| 5 | + "time" |
6 | 6 |
|
7 |
| - "github.com/stretchr/testify/assert" |
| 7 | + "github.com/hashicorp/terraform-plugin-framework/types" |
8 | 8 | "github.com/mongodb/terraform-provider-mongodbatlas/internal/service/clouduserprojectassignment"
|
9 |
| - // "go.mongodb.org/atlas-sdk/v20231115003/admin" use latest version |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "go.mongodb.org/atlas-sdk/v20250312005/admin" |
| 11 | +) |
| 12 | + |
| 13 | +const ( |
| 14 | + testUserID = "user-123" |
| 15 | + testUsername = "jdoe" |
| 16 | + testFirstName = "John" |
| 17 | + testLastName = "Doe" |
| 18 | + testCountry = "CA" |
| 19 | + testMobile = "+1555123456" |
| 20 | + testInviter = "admin" |
| 21 | + testOrgMembershipStatus = "ACTIVE" |
| 22 | + testInviterUsername = "" |
| 23 | + |
| 24 | + testProjectRoleOwner = "PROJECT_OWNER" |
| 25 | + testProjectRoleRead = "PROJECT_READ_ONLY" |
| 26 | + testProjectRoleMember = "PROJECT_MEMBER" |
| 27 | + |
| 28 | + testProjectID = "project-123" |
| 29 | + testOrgID = "org-123" |
| 30 | +) |
| 31 | + |
| 32 | +var ( |
| 33 | + when = time.Date(2020, 1, 2, 3, 4, 5, 0, time.UTC) |
| 34 | + testCreatedAt = when.Format(time.RFC3339) |
| 35 | + testInvitationCreatedAt = when.Add(-24 * time.Hour).Format(time.RFC3339) |
| 36 | + testInvitationExpiresAt = when.Add(24 * time.Hour).Format(time.RFC3339) |
| 37 | + testLastAuth = when.Add(-2 * time.Hour).Format(time.RFC3339) |
| 38 | + |
| 39 | + testProjectRoles = []string{testProjectRoleMember, testProjectRoleOwner} |
10 | 40 | )
|
11 | 41 |
|
12 | 42 | type sdkToTFModelTestCase struct {
|
13 |
| - SDKResp *admin.CloudUserProjectAssignment |
| 43 | + SDKResp *admin.GroupUserResponse |
14 | 44 | expectedTFModel *clouduserprojectassignment.TFModel
|
15 | 45 | }
|
16 | 46 |
|
17 | 47 | func TestCloudUserProjectAssignmentSDKToTFModel(t *testing.T) {
|
18 |
| - testCases := map[string]sdkToTFModelTestCase{ // TODO: consider adding test cases to contemplate all possible API responses |
| 48 | + ctx := t.Context() |
| 49 | + |
| 50 | + fullResp := &admin.GroupUserResponse{ |
| 51 | + Id: testUserID, |
| 52 | + Username: testUsername, |
| 53 | + FirstName: admin.PtrString(testFirstName), |
| 54 | + LastName: admin.PtrString(testLastName), |
| 55 | + Country: admin.PtrString(testCountry), |
| 56 | + MobileNumber: admin.PtrString(testMobile), |
| 57 | + OrgMembershipStatus: testOrgMembershipStatus, |
| 58 | + CreatedAt: admin.PtrTime(when), |
| 59 | + LastAuth: admin.PtrTime(when.Add(-2 * time.Hour)), |
| 60 | + InvitationCreatedAt: admin.PtrTime(when.Add(-24 * time.Hour)), |
| 61 | + InvitationExpiresAt: admin.PtrTime(when.Add(24 * time.Hour)), |
| 62 | + InviterUsername: admin.PtrString(testInviterUsername), |
| 63 | + Roles: testProjectRoles, |
| 64 | + } |
| 65 | + |
| 66 | + expectedRoles, _ := types.SetValueFrom(ctx, types.StringType, testProjectRoles) |
| 67 | + |
| 68 | + expectedFullModel := &clouduserprojectassignment.TFModel{ |
| 69 | + UserId: types.StringValue(testUserID), |
| 70 | + Username: types.StringValue(testUsername), |
| 71 | + ProjectId: types.StringValue(testProjectID), |
| 72 | + FirstName: types.StringValue(testFirstName), |
| 73 | + LastName: types.StringValue(testLastName), |
| 74 | + Country: types.StringValue(testCountry), |
| 75 | + MobileNumber: types.StringValue(testMobile), |
| 76 | + OrgMembershipStatus: types.StringValue(testOrgMembershipStatus), |
| 77 | + CreatedAt: types.StringValue(testCreatedAt), |
| 78 | + LastAuth: types.StringValue(testLastAuth), |
| 79 | + InvitationCreatedAt: types.StringValue(testInvitationCreatedAt), |
| 80 | + InvitationExpiresAt: types.StringValue(testInvitationExpiresAt), |
| 81 | + InviterUsername: types.StringValue(testInviterUsername), |
| 82 | + Roles: expectedRoles, |
| 83 | + } |
| 84 | + |
| 85 | + testCases := map[string]sdkToTFModelTestCase{ |
| 86 | + "nil SDK response": { |
| 87 | + SDKResp: nil, |
| 88 | + expectedTFModel: nil, |
| 89 | + }, |
19 | 90 | "Complete SDK response": {
|
20 |
| - SDKResp: &admin.CloudUserProjectAssignment{ |
21 |
| - }, |
22 |
| - expectedTFModel: &clouduserprojectassignment.TFModel{ |
23 |
| - }, |
| 91 | + SDKResp: fullResp, |
| 92 | + expectedTFModel: expectedFullModel, |
24 | 93 | },
|
25 | 94 | }
|
26 | 95 |
|
27 | 96 | for testName, tc := range testCases {
|
28 | 97 | t.Run(testName, func(t *testing.T) {
|
29 |
| - resultModel, diags := clouduserprojectassignment.NewTFModel(context.Background(), tc.SDKResp) |
30 |
| - if diags.HasError() { |
31 |
| - t.Errorf("unexpected errors found: %s", diags.Errors()[0].Summary()) |
32 |
| - } |
33 |
| - assert.Equal(t, tc.expectedTFModel, resultModel, "created terraform model did not match expected output") |
| 98 | + resultModel, diags := clouduserprojectassignment.NewTFModel(t.Context(), testProjectID, tc.SDKResp) |
| 99 | + assert.False(t, diags.HasError(), "expected no diagnostics") |
| 100 | + assert.Equal(t, tc.expectedTFModel, resultModel, "TFModel did not match expected") |
34 | 101 | })
|
35 | 102 | }
|
36 | 103 | }
|
37 | 104 |
|
| 105 | +func TestNewProjectUserRequest(t *testing.T) { |
| 106 | + ctx := t.Context() |
| 107 | + expectedRoles, _ := types.SetValueFrom(ctx, types.StringType, testProjectRoles) |
38 | 108 |
|
39 |
| -type tfToSDKModelTestCase struct { |
40 |
| - tfModel *clouduserprojectassignment.TFModel |
41 |
| - expectedSDKReq *admin.CloudUserProjectAssignment |
42 |
| -} |
43 |
| - |
44 |
| -func TestCloudUserProjectAssignmentTFModelToSDK(t *testing.T) { |
45 |
| - testCases := map[string]tfToSDKModelTestCase{ |
46 |
| - "Complete TF state": { |
47 |
| - tfModel: &clouduserprojectassignment.TFModel{ |
| 109 | + testCases := map[string]struct { |
| 110 | + plan *clouduserprojectassignment.TFModel |
| 111 | + expected *admin.GroupUserRequest |
| 112 | + }{ |
| 113 | + "Complete model": { |
| 114 | + plan: &clouduserprojectassignment.TFModel{ |
| 115 | + UserId: types.StringValue(testUserID), |
| 116 | + Username: types.StringValue(testUsername), |
| 117 | + ProjectId: types.StringValue(testProjectID), |
| 118 | + FirstName: types.StringValue(testFirstName), |
| 119 | + LastName: types.StringValue(testLastName), |
| 120 | + Country: types.StringValue(testCountry), |
| 121 | + MobileNumber: types.StringValue(testMobile), |
| 122 | + OrgMembershipStatus: types.StringValue(testOrgMembershipStatus), |
| 123 | + CreatedAt: types.StringValue(testCreatedAt), |
| 124 | + LastAuth: types.StringValue(testLastAuth), |
| 125 | + InvitationCreatedAt: types.StringValue(testInvitationCreatedAt), |
| 126 | + InvitationExpiresAt: types.StringValue(testInvitationExpiresAt), |
| 127 | + InviterUsername: types.StringValue(testInviterUsername), |
| 128 | + Roles: expectedRoles, |
48 | 129 | },
|
49 |
| - expectedSDKReq: &admin.CloudUserProjectAssignment{ |
| 130 | + expected: &admin.GroupUserRequest{ |
| 131 | + Username: testUsername, |
| 132 | + Roles: testProjectRoles, |
50 | 133 | },
|
51 | 134 | },
|
52 | 135 | }
|
53 | 136 |
|
54 |
| - for testName, tc := range testCases { |
55 |
| - t.Run(testName, func(t *testing.T) { |
56 |
| - apiReqResult, diags := clouduserprojectassignment.NewAtlasReq(context.Background(), tc.tfModel) |
57 |
| - if diags.HasError() { |
58 |
| - t.Errorf("unexpected errors found: %s", diags.Errors()[0].Summary()) |
59 |
| - } |
60 |
| - assert.Equal(t, tc.expectedSDKReq, apiReqResult, "created sdk model did not match expected output") |
| 137 | + for name, tc := range testCases { |
| 138 | + t.Run(name, func(t *testing.T) { |
| 139 | + req, diags := clouduserprojectassignment.NewProjectUserReq(ctx, tc.plan) |
| 140 | + assert.False(t, diags.HasError(), "expected no diagnostics") |
| 141 | + assert.Equal(t, tc.expected, req) |
61 | 142 | })
|
62 | 143 | }
|
63 | 144 | }
|
64 |
| - |
65 |
| - |
|
0 commit comments