|
| 1 | +package integration |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "github.com/Optum/dce-cli/client/operations" |
| 6 | + "github.com/Optum/dce-cli/mocks" |
| 7 | + "github.com/Optum/dce-cli/pkg/service" |
| 8 | + "github.com/aws/aws-sdk-go/aws" |
| 9 | + "github.com/go-openapi/runtime" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/mock" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | + "testing" |
| 14 | + "time" |
| 15 | +) |
| 16 | + |
| 17 | +func TestLeasesCreate(t *testing.T) { |
| 18 | + |
| 19 | + t.Run("with --expires-on flag", func(t *testing.T) { |
| 20 | + leasesCreateTest(t, &leaseCreateTestCase{ |
| 21 | + commandArgs: []string{ |
| 22 | + "leases", "create", |
| 23 | + "-p", "test-user", |
| 24 | + "-b", "100", "-c", "USD", |
| 25 | + |
| 26 | + "--expires-on", "1h", |
| 27 | + }, |
| 28 | + expectedCreateLeaseRequest: operations.PostLeasesBody{ |
| 29 | + BudgetAmount: aws.Float64(100), |
| 30 | + BudgetCurrency: aws.String("USD"), |
| 31 | + BudgetNotificationEmails: [] string{ "[email protected]"}, |
| 32 | + ExpiresOn: float64(time.Now().Add(time.Hour).Unix()), |
| 33 | + PrincipalID: aws.String("test-user"), |
| 34 | + }, |
| 35 | + mockAccountID: "123456789012", |
| 36 | + expectedJSONOutput: map[string]interface{}{ |
| 37 | + "accountId": "123456789012", |
| 38 | + "budgetAmount": float64(100), |
| 39 | + "budgetCurrency": "USD", |
| 40 | + "budgetNotificationEmails": [] interface{}{ "[email protected]"}, |
| 41 | + "principalId": "test-user", |
| 42 | + "expiresOn": time.Now().Add(time.Hour).Unix(), |
| 43 | + }, |
| 44 | + }) |
| 45 | + }) |
| 46 | + |
| 47 | +} |
| 48 | + |
| 49 | +type leaseCreateTestCase struct { |
| 50 | + // Args to pass to the dce-cli |
| 51 | + commandArgs []string |
| 52 | + |
| 53 | + // Request body we expect to bbe sent to the `POST /leases` endpoint |
| 54 | + expectedCreateLeaseRequest operations.PostLeasesBody |
| 55 | + |
| 56 | + // AccountID to return from the `POST /leases` endpoint |
| 57 | + mockAccountID string |
| 58 | + |
| 59 | + // JSON we expect the CLI operation to output to stdout |
| 60 | + expectedJSONOutput map[string]interface{} |
| 61 | +} |
| 62 | + |
| 63 | +func leasesCreateTest(t *testing.T, testCase *leaseCreateTestCase) { |
| 64 | + cli := NewCLITest(t) |
| 65 | + |
| 66 | + // Mock the DCE API |
| 67 | + api := &mocks.APIer{} |
| 68 | + |
| 69 | + // Should send a `POST /leases` request |
| 70 | + expectedLease := testCase.expectedCreateLeaseRequest |
| 71 | + api.On("PostLeases", |
| 72 | + mock.MatchedBy(func(params *operations.PostLeasesParams) bool { |
| 73 | + // Check that ExpiresOn is within ~5s of our expectation |
| 74 | + // to account for actual time passing |
| 75 | + assert.InDelta(t, expectedLease.ExpiresOn, params.Lease.ExpiresOn, 5) |
| 76 | + |
| 77 | + // Pull out ExpiresOn field, |
| 78 | + // so we can assert.Equals on the rest of the struct |
| 79 | + expectedLeaseCopy := expectedLease |
| 80 | + expectedLeaseCopy.ExpiresOn = 0 |
| 81 | + actualLeaseCopy := params.Lease |
| 82 | + actualLeaseCopy.ExpiresOn = 0 |
| 83 | + |
| 84 | + // Check the rest of the lease object |
| 85 | + assert.Equal(t, expectedLeaseCopy, actualLeaseCopy) |
| 86 | + return true |
| 87 | + }), nil). |
| 88 | + Return(func(params *operations.PostLeasesParams, wt runtime.ClientAuthInfoWriter) *operations.PostLeasesCreated { |
| 89 | + // Return a Lease, which looks like the requested lease |
| 90 | + // (but with an account ID) |
| 91 | + return &operations.PostLeasesCreated{ |
| 92 | + Payload: &operations.PostLeasesCreatedBody{ |
| 93 | + AccountID: testCase.mockAccountID, |
| 94 | + BudgetAmount: *params.Lease.BudgetAmount, |
| 95 | + BudgetCurrency: *params.Lease.BudgetCurrency, |
| 96 | + BudgetNotificationEmails: params.Lease.BudgetNotificationEmails, |
| 97 | + ExpiresOn: params.Lease.ExpiresOn, |
| 98 | + PrincipalID: *params.Lease.PrincipalID, |
| 99 | + }, |
| 100 | + } |
| 101 | + }, nil) |
| 102 | + |
| 103 | + // Mock the output writer |
| 104 | + out := &mocks.OutputWriter{} |
| 105 | + out.On("Write", mock.MatchedBy(func(out []byte) bool { |
| 106 | + var actualJSONOutput map[string]interface{} |
| 107 | + err := json.Unmarshal(out, &actualJSONOutput) |
| 108 | + assert.Nil(t, err, "output should be valid JSON") |
| 109 | + |
| 110 | + // Check that ExpiresOn is within ~5s of our expectation |
| 111 | + // to account for actual time passing |
| 112 | + _ = testCase.expectedJSONOutput |
| 113 | + expectedExpiresOn := testCase.expectedJSONOutput["expiresOn"].(int64) |
| 114 | + actualExpiresOn := actualJSONOutput["expiresOn"].(float64) |
| 115 | + assert.InDelta(t, |
| 116 | + expectedExpiresOn, |
| 117 | + actualExpiresOn, |
| 118 | + 5, |
| 119 | + ) |
| 120 | + |
| 121 | + // Pull out the expiresOn field, |
| 122 | + // so we can assert.Equals on the rest of JSON |
| 123 | + actualJSONOutput["expiresOn"] = int64(0) |
| 124 | + testCase.expectedJSONOutput["expiresOn"] = int64(0) |
| 125 | + |
| 126 | + assert.Equal(t, testCase.expectedJSONOutput, actualJSONOutput) |
| 127 | + |
| 128 | + return true |
| 129 | + })).Return(0, nil) |
| 130 | + |
| 131 | + // Mock the Authentication service (would pop open browser to auth user) |
| 132 | + authSvc := &mocks.Authenticater{} |
| 133 | + authSvc.On("Authenticate").Return(nil) |
| 134 | + |
| 135 | + cli.Inject(func(input *injectorInput) { |
| 136 | + service.ApiClient = api |
| 137 | + service.Out = out |
| 138 | + input.service.Authenticater = authSvc |
| 139 | + }) |
| 140 | + |
| 141 | + // Run `dce leases create` command |
| 142 | + err := cli.Execute(testCase.commandArgs) |
| 143 | + require.Nil(t, err) |
| 144 | + |
| 145 | + api.AssertExpectations(t) |
| 146 | + out.AssertNumberOfCalls(t, "Write", 1) |
| 147 | +} |
0 commit comments