|
| 1 | +// Copyright (c) 2023, SailPoint Technologies, Inc. All rights reserved. |
| 2 | +package connector |
| 3 | + |
| 4 | +import ( |
| 5 | + "bytes" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/golang/mock/gomock" |
| 11 | + "github.com/sailpoint-oss/sailpoint-cli/internal/mocks" |
| 12 | +) |
| 13 | + |
| 14 | +func TestChangePasswordWithoutInput(t *testing.T) { |
| 15 | + ctrl := gomock.NewController(t) |
| 16 | + defer ctrl.Finish() |
| 17 | + |
| 18 | + client := mocks.NewMockClient(ctrl) |
| 19 | + term := mocks.NewMockTerm(ctrl) |
| 20 | + |
| 21 | + cmd := newConnInvokeChangePasswordCmd(client, term) |
| 22 | + addRequiredFlagsFromParentCmd(cmd) |
| 23 | + |
| 24 | + b := new(bytes.Buffer) |
| 25 | + cmd.SetOut(b) |
| 26 | + cmd.SetArgs([]string{"-c", "test-connector", "--config-json", "{}"}) |
| 27 | + |
| 28 | + err := cmd.Execute() |
| 29 | + if err == nil { |
| 30 | + t.Errorf("failed to detect error: reading account without identity") |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +func TestChangePasswordWithIdentityAndPassword(t *testing.T) { |
| 35 | + ctrl := gomock.NewController(t) |
| 36 | + defer ctrl.Finish() |
| 37 | + |
| 38 | + i := `{"connectorRef":"test-connector","tag":"latest","type":"std:change-password","config":{},` + |
| 39 | + `"input":{"identity":"john.doe","key":{"simple":{"id":"john.doe"}},"password":"password"}}` |
| 40 | + |
| 41 | + client := mocks.NewMockClient(ctrl) |
| 42 | + client.EXPECT(). |
| 43 | + Post(gomock.Any(), gomock.Any(), "application/json", bytes.NewReader([]byte(i))). |
| 44 | + Return(&http.Response{StatusCode: http.StatusOK, Body: io.NopCloser(bytes.NewReader([]byte("{}")))}, nil) |
| 45 | + |
| 46 | + term := mocks.NewMockTerm(ctrl) |
| 47 | + term.EXPECT(). |
| 48 | + PromptPassword(gomock.Any()). |
| 49 | + Return("password", nil) |
| 50 | + |
| 51 | + cmd := newConnInvokeChangePasswordCmd(client, term) |
| 52 | + addRequiredFlagsFromParentCmd(cmd) |
| 53 | + |
| 54 | + b := new(bytes.Buffer) |
| 55 | + cmd.SetOut(b) |
| 56 | + cmd.SetArgs([]string{"john.doe", "-c", "test-connector", "--config-json", "{}"}) |
| 57 | + |
| 58 | + err := cmd.Execute() |
| 59 | + if err != nil { |
| 60 | + t.Errorf("command failed with err: %s", err) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +func TestChangePasswordWithIdentityAndPasswordAndUniqueId(t *testing.T) { |
| 65 | + ctrl := gomock.NewController(t) |
| 66 | + defer ctrl.Finish() |
| 67 | + |
| 68 | + i := `{"connectorRef":"test-connector","tag":"latest","type":"std:change-password","config":{},` + |
| 69 | + `"input":{"identity":"john.doe","key":{"compound":{"lookupId":"john.doe","uniqueId":"12345"}},"password":"password"}}` |
| 70 | + |
| 71 | + client := mocks.NewMockClient(ctrl) |
| 72 | + client.EXPECT(). |
| 73 | + Post(gomock.Any(), gomock.Any(), "application/json", bytes.NewReader([]byte(i))). |
| 74 | + Return(&http.Response{StatusCode: http.StatusOK, Body: io.NopCloser(bytes.NewReader([]byte("{}")))}, nil) |
| 75 | + |
| 76 | + term := mocks.NewMockTerm(ctrl) |
| 77 | + term.EXPECT(). |
| 78 | + PromptPassword(gomock.Any()). |
| 79 | + Return("password", nil) |
| 80 | + |
| 81 | + cmd := newConnInvokeChangePasswordCmd(client, term) |
| 82 | + addRequiredFlagsFromParentCmd(cmd) |
| 83 | + |
| 84 | + b := new(bytes.Buffer) |
| 85 | + cmd.SetOut(b) |
| 86 | + cmd.SetArgs([]string{"john.doe", "12345", "-c", "test-connector", "--config-json", "{}"}) |
| 87 | + |
| 88 | + err := cmd.Execute() |
| 89 | + if err != nil { |
| 90 | + t.Errorf("command failed with err: %s", err) |
| 91 | + } |
| 92 | +} |
0 commit comments