github.com/minio/console@v1.3.0/api/user_account_test.go (about) 1 // This file is part of MinIO Console Server 2 // Copyright (c) 2021 MinIO, Inc. 3 // 4 // This program is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Affero General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // This program is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Affero General Public License for more details. 13 // 14 // You should have received a copy of the GNU Affero General Public License 15 // along with this program. If not, see <http://www.gnu.org/licenses/>. 16 17 package api 18 19 import ( 20 "context" 21 "errors" 22 "net/http" 23 "testing" 24 25 accountApi "github.com/minio/console/api/operations/account" 26 "github.com/minio/console/models" 27 "github.com/stretchr/testify/assert" 28 ) 29 30 func Test_getChangePasswordResponse(t *testing.T) { 31 assert := assert.New(t) 32 session := &models.Principal{ 33 AccountAccessKey: "TESTTEST", 34 } 35 CurrentSecretKey := "string" 36 NewSecretKey := "string" 37 changePasswordParameters := accountApi.AccountChangePasswordParams{ 38 HTTPRequest: &http.Request{}, 39 Body: &models.AccountChangePasswordRequest{ 40 CurrentSecretKey: &CurrentSecretKey, 41 NewSecretKey: &NewSecretKey, 42 }, 43 } 44 loginResponse, actualError := getChangePasswordResponse(session, changePasswordParameters) 45 expected := (*models.LoginResponse)(nil) 46 assert.Equal(expected, loginResponse) 47 expectedError := "error please check your current password" // errChangePassword 48 assert.Equal(expectedError, actualError.APIError.DetailedMessage) 49 } 50 51 func Test_changePassword(t *testing.T) { 52 client := AdminClientMock{} 53 type args struct { 54 ctx context.Context 55 client AdminClientMock 56 session *models.Principal 57 currentSecretKey string 58 newSecretKey string 59 } 60 tests := []struct { 61 name string 62 args args 63 wantErr bool 64 mock func() 65 }{ 66 { 67 name: "password changed successfully", 68 args: args{ 69 client: client, 70 ctx: context.Background(), 71 session: &models.Principal{ 72 AccountAccessKey: "TESTTEST", 73 }, 74 currentSecretKey: "TESTTEST", 75 newSecretKey: "TESTTEST2", 76 }, 77 mock: func() { 78 minioChangePasswordMock = func(_ context.Context, _, _ string) error { 79 return nil 80 } 81 }, 82 }, 83 { 84 name: "error when changing password", 85 args: args{ 86 client: client, 87 ctx: context.Background(), 88 session: &models.Principal{ 89 AccountAccessKey: "TESTTEST", 90 }, 91 currentSecretKey: "TESTTEST", 92 newSecretKey: "TESTTEST2", 93 }, 94 mock: func() { 95 minioChangePasswordMock = func(_ context.Context, _, _ string) error { 96 return errors.New("there was an error, please try again") 97 } 98 }, 99 wantErr: true, 100 }, 101 { 102 name: "error because current password doesn't match", 103 args: args{ 104 client: client, 105 ctx: context.Background(), 106 session: &models.Principal{ 107 AccountAccessKey: "TESTTEST", 108 }, 109 currentSecretKey: "TESTTEST", 110 newSecretKey: "TESTTEST2", 111 }, 112 mock: func() { 113 minioChangePasswordMock = func(_ context.Context, _, _ string) error { 114 return errors.New("there was an error, please try again") 115 } 116 }, 117 wantErr: true, 118 }, 119 } 120 for _, tt := range tests { 121 t.Run(tt.name, func(_ *testing.T) { 122 if tt.mock != nil { 123 tt.mock() 124 } 125 if err := changePassword(tt.args.ctx, tt.args.client, tt.args.session, tt.args.newSecretKey); (err != nil) != tt.wantErr { 126 t.Errorf("changePassword() error = %v, wantErr %v", err, tt.wantErr) 127 } 128 }) 129 } 130 }