Best Testkube code snippet using secret.Prepare
repository_test.go
Source:repository_test.go
1package secrets2import (3 "context"4 "errors"5 "testing"6 "github.com/kyma-project/kyma/components/compass-runtime-agent/internal/secrets/mocks"7 "k8s.io/apimachinery/pkg/types"8 "k8s.io/apimachinery/pkg/runtime/schema"9 "github.com/stretchr/testify/assert"10 "github.com/stretchr/testify/require"11 k8serrors "k8s.io/apimachinery/pkg/api/errors"12 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"13)14const (15 dataKey = "dataKey"16 secretName = "secret-name"17 namespace = "kyma-integration"18)19var (20 namespacedName = types.NamespacedName{21 Name: secretName,22 Namespace: namespace,23 }24 secretData = map[string][]byte{25 "testKey2": []byte("testValue2"),26 "testKey1": []byte("testValue1"),27 }28)29func TestRepository_Exists(t *testing.T) {30 t.Run("should return true if exists", func(t *testing.T) {31 // given32 secret := makeSecret(namespacedName, map[string][]byte{dataKey: []byte("data")})33 secretsManagerMock := &mocks.Manager{}34 secretsManagerMock.On("Get", context.Background(), secretName, metav1.GetOptions{}).Return(secret, nil)35 repository := NewRepository(prepareManagerConstructor(secretsManagerMock))36 // when37 exists, err := repository.Exists(namespacedName)38 // then39 assert.NoError(t, err)40 assert.Equal(t, true, exists)41 })42 t.Run("should return false if secret doesn't exist", func(t *testing.T) {43 // given44 secretsManagerMock := &mocks.Manager{}45 secretsManagerMock.On("Get", context.Background(), secretName, metav1.GetOptions{}).Return(nil, k8serrors.NewNotFound(schema.GroupResource{}, "secret"))46 repository := NewRepository(prepareManagerConstructor(secretsManagerMock))47 // when48 exists, err := repository.Exists(namespacedName)49 // then50 assert.NoError(t, err)51 assert.Equal(t, false, exists)52 })53 t.Run("should return error if failed to read secret", func(t *testing.T) {54 // given55 secretsManagerMock := &mocks.Manager{}56 secretsManagerMock.On("Get", context.Background(), secretName, metav1.GetOptions{}).Return(nil, errors.New("oh, no"))57 repository := NewRepository(prepareManagerConstructor(secretsManagerMock))58 // when59 exists, err := repository.Exists(namespacedName)60 // then61 assert.Error(t, err)62 assert.Equal(t, false, exists)63 })64}65func TestRepository_Get(t *testing.T) {66 t.Run("should get given secret", func(t *testing.T) {67 // given68 secret := makeSecret(namespacedName, map[string][]byte{dataKey: []byte("data")})69 secretsManagerMock := &mocks.Manager{}70 secretsManagerMock.On("Get", context.Background(), secretName, metav1.GetOptions{}).Return(secret, nil)71 repository := NewRepository(prepareManagerConstructor(secretsManagerMock))72 // when73 secrets, err := repository.Get(namespacedName)74 // then75 assert.NoError(t, err)76 assert.NotNil(t, secrets[dataKey])77 secretsManagerMock.AssertExpectations(t)78 })79 t.Run("should return an error in case fetching fails", func(t *testing.T) {80 // given81 secretsManagerMock := &mocks.Manager{}82 secretsManagerMock.On("Get", context.Background(), secretName, metav1.GetOptions{}).Return(83 nil,84 errors.New("some error"))85 repository := NewRepository(prepareManagerConstructor(secretsManagerMock))86 // when87 secretData, err := repository.Get(namespacedName)88 // then89 assert.Error(t, err)90 assert.NotEmpty(t, err.Error())91 assert.Nil(t, secretData)92 secretsManagerMock.AssertExpectations(t)93 })94}95func TestRepository_Override(t *testing.T) {96 t.Run("should create secret", func(t *testing.T) {97 // given98 secret := makeSecret(namespacedName, secretData)99 secretsManagerMock := &mocks.Manager{}100 secretsManagerMock.On("Create", context.Background(), secret, metav1.CreateOptions{}).Return(secret, nil)101 repository := NewRepository(prepareManagerConstructor(secretsManagerMock))102 // when103 err := repository.UpsertWithReplace(namespacedName, secretData)104 // then105 assert.NoError(t, err)106 secretsManagerMock.AssertExpectations(t)107 })108 t.Run("should fail if unable to create secret", func(t *testing.T) {109 // given110 secret := makeSecret(namespacedName, secretData)111 secretsManagerMock := &mocks.Manager{}112 secretsManagerMock.On("Create", context.Background(), secret, metav1.CreateOptions{}).Return(nil, errors.New("some error"))113 repository := NewRepository(prepareManagerConstructor(secretsManagerMock))114 // when115 err := repository.UpsertWithReplace(namespacedName, secretData)116 // then117 require.Error(t, err)118 secretsManagerMock.AssertExpectations(t)119 })120 t.Run("should override secret if already exist", func(t *testing.T) {121 // given122 secret := makeSecret(namespacedName, secretData)123 secretsManagerMock := &mocks.Manager{}124 secretsManagerMock.On("Create", context.Background(), secret, metav1.CreateOptions{}).Return(nil, k8serrors.NewAlreadyExists(schema.GroupResource{}, "error")).Once()125 secretsManagerMock.On("Create", context.Background(), secret, metav1.CreateOptions{}).Return(nil, nil).Once()126 secretsManagerMock.On("Delete", context.Background(), secretName, metav1.DeleteOptions{}).Return(nil)127 repository := NewRepository(prepareManagerConstructor(secretsManagerMock))128 // when129 err := repository.UpsertWithReplace(namespacedName, secretData)130 // then131 require.NoError(t, err)132 secretsManagerMock.AssertExpectations(t)133 })134 t.Run("should return error if failed to delete secret", func(t *testing.T) {135 // given136 secret := makeSecret(namespacedName, secretData)137 secretsManagerMock := &mocks.Manager{}138 secretsManagerMock.On("Create", context.Background(), secret, metav1.CreateOptions{}).Return(nil, k8serrors.NewAlreadyExists(schema.GroupResource{}, "error"))139 secretsManagerMock.On("Delete", context.Background(), secretName, metav1.DeleteOptions{}).Return(errors.New("error"))140 repository := NewRepository(prepareManagerConstructor(secretsManagerMock))141 // when142 err := repository.UpsertWithReplace(namespacedName, secretData)143 // then144 require.Error(t, err)145 secretsManagerMock.AssertExpectations(t)146 })147 t.Run("should return error if failed to create secret after deleting", func(t *testing.T) {148 // given149 secret := makeSecret(namespacedName, secretData)150 secretsManagerMock := &mocks.Manager{}151 secretsManagerMock.On("Create", context.Background(), secret, metav1.CreateOptions{}).Return(nil, k8serrors.NewAlreadyExists(schema.GroupResource{}, "error")).Once()152 secretsManagerMock.On("Create", context.Background(), secret, metav1.CreateOptions{}).Return(nil, errors.New("error")).Once()153 secretsManagerMock.On("Delete", context.Background(), secretName, metav1.DeleteOptions{}).Return(nil)154 repository := NewRepository(prepareManagerConstructor(secretsManagerMock))155 // when156 err := repository.UpsertWithReplace(namespacedName, secretData)157 // then158 require.Error(t, err)159 secretsManagerMock.AssertExpectations(t)160 })161}162func TestRepository_UpsertData(t *testing.T) {163 t.Run("should update secret data if it exists", func(t *testing.T) {164 // given165 secret := makeSecret(namespacedName, secretData)166 additionalSecretData := map[string][]byte{167 "testKey2": []byte("testValue2Modified"),168 "testKey3": []byte("testValue3"),169 }170 updatedSecret := makeSecret(namespacedName, map[string][]byte{171 "testKey1": []byte("testValue1"),172 "testKey2": []byte("testValue2Modified"),173 "testKey3": []byte("testValue3"),174 })175 secretsManagerMock := &mocks.Manager{}176 secretsManagerMock.On("Get", context.Background(), secretName, metav1.GetOptions{}).Return(secret, nil)177 secretsManagerMock.On("Update", context.Background(), updatedSecret, metav1.UpdateOptions{}).Return(secret, nil)178 repository := NewRepository(prepareManagerConstructor(secretsManagerMock))179 // when180 err := repository.UpsertWithMerge(namespacedName, additionalSecretData)181 // then182 assert.NoError(t, err)183 secretsManagerMock.AssertExpectations(t)184 })185 t.Run("should create new secret if it does not exists", func(t *testing.T) {186 // given187 secret := makeSecret(namespacedName, secretData)188 secretsManagerMock := &mocks.Manager{}189 secretsManagerMock.On("Get", context.Background(), secretName, metav1.GetOptions{}).Return(nil, k8serrors.NewNotFound(schema.GroupResource{}, "error"))190 secretsManagerMock.On("Update", context.Background(), secret, metav1.UpdateOptions{}).Return(nil, k8serrors.NewNotFound(schema.GroupResource{}, "error"))191 secretsManagerMock.On("Create", context.Background(), secret, metav1.CreateOptions{}).Return(secret, nil)192 repository := NewRepository(prepareManagerConstructor(secretsManagerMock))193 // when194 err := repository.UpsertWithMerge(namespacedName, secretData)195 // then196 assert.NoError(t, err)197 secretsManagerMock.AssertExpectations(t)198 })199 t.Run("should return error when failed to get secret", func(t *testing.T) {200 // given201 secretsManagerMock := &mocks.Manager{}202 secretsManagerMock.On("Get", context.Background(), secretName, metav1.GetOptions{}).Return(nil, errors.New("error"))203 repository := NewRepository(prepareManagerConstructor(secretsManagerMock))204 // when205 err := repository.UpsertWithMerge(namespacedName, secretData)206 // then207 assert.Error(t, err)208 secretsManagerMock.AssertExpectations(t)209 })210 t.Run("should return error when failed to update secret", func(t *testing.T) {211 // given212 secret := makeSecret(namespacedName, secretData)213 secretsManagerMock := &mocks.Manager{}214 secretsManagerMock.On("Get", context.Background(), secretName, metav1.GetOptions{}).Return(nil, k8serrors.NewNotFound(schema.GroupResource{}, "error"))215 secretsManagerMock.On("Update", context.Background(), secret, metav1.UpdateOptions{}).Return(nil, errors.New("error"))216 repository := NewRepository(prepareManagerConstructor(secretsManagerMock))217 // when218 err := repository.UpsertWithMerge(namespacedName, secretData)219 // then220 assert.Error(t, err)221 secretsManagerMock.AssertExpectations(t)222 })223 t.Run("should return error when failed to create secret", func(t *testing.T) {224 // given225 secret := makeSecret(namespacedName, secretData)226 secretsManagerMock := &mocks.Manager{}227 secretsManagerMock.On("Get", context.Background(), secretName, metav1.GetOptions{}).Return(nil, k8serrors.NewNotFound(schema.GroupResource{}, "error"))228 secretsManagerMock.On("Update", context.Background(), secret, metav1.UpdateOptions{}).Return(nil, k8serrors.NewNotFound(schema.GroupResource{}, "error"))229 secretsManagerMock.On("Create", context.Background(), secret, metav1.CreateOptions{}).Return(nil, errors.New("error"))230 repository := NewRepository(prepareManagerConstructor(secretsManagerMock))231 // when232 err := repository.UpsertWithMerge(namespacedName, secretData)233 // then234 assert.Error(t, err)235 secretsManagerMock.AssertExpectations(t)236 })237}238func prepareManagerConstructor(manager Manager) ManagerConstructor {239 return func(namespace string) Manager {240 return manager241 }242}...
secrets_test.go
Source:secrets_test.go
1package main2import (3 "context"4 "errors"5 "testing"6 "github.com/stretchr/testify/require"7 v1 "k8s.io/api/core/v1"8 "github.com/kyma-project/kyma/components/application-connectivity-certs-setup-job/mocks"9 "k8s.io/apimachinery/pkg/types"10 "k8s.io/apimachinery/pkg/runtime/schema"11 "github.com/stretchr/testify/assert"12 k8serrors "k8s.io/apimachinery/pkg/api/errors"13 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"14)15const (16 dataKey = "dataKey"17 secretName = "secret-name"18 namespace = "kyma-integration"19)20var (21 testContext = context.Background()22 namespacedName = types.NamespacedName{23 Name: secretName,24 Namespace: namespace,25 }26 secretData = map[string][]byte{27 "testKey2": []byte("testValue2"),28 "testKey1": []byte("testValue1"),29 }30)31func TestRepository_Get(t *testing.T) {32 t.Run("should get given secret", func(t *testing.T) {33 // given34 secret := makeSecret(namespacedName, map[string][]byte{dataKey: []byte("data")})35 secretsManagerMock := &mocks.Manager{}36 secretsManagerMock.On("Get", testContext, secretName, metav1.GetOptions{}).Return(secret, nil)37 repository := NewSecretRepository(prepareManagerConstructor(secretsManagerMock))38 // when39 secrets, err := repository.Get(namespacedName)40 // then41 assert.NoError(t, err)42 assert.NotNil(t, secrets[dataKey])43 secretsManagerMock.AssertExpectations(t)44 })45 t.Run("should return an error in case fetching fails", func(t *testing.T) {46 // given47 secretsManagerMock := &mocks.Manager{}48 secretsManagerMock.On("Get", testContext, secretName, metav1.GetOptions{}).Return(49 nil,50 errors.New("some error"))51 repository := NewSecretRepository(prepareManagerConstructor(secretsManagerMock))52 // when53 secretData, err := repository.Get(namespacedName)54 // then55 assert.Error(t, err)56 assert.NotEmpty(t, err.Error())57 assert.Nil(t, secretData)58 secretsManagerMock.AssertExpectations(t)59 })60}61func TestRepository_Upsert(t *testing.T) {62 t.Run("should update secret data if it exists", func(t *testing.T) {63 // given64 secret := makeSecret(namespacedName, secretData)65 additionalSecretData := map[string][]byte{66 "testKey2": []byte("testValue2Modified"),67 "testKey3": []byte("testValue3"),68 }69 updatedSecret := makeSecret(namespacedName, map[string][]byte{70 "testKey1": []byte("testValue1"),71 "testKey2": []byte("testValue2Modified"),72 "testKey3": []byte("testValue3"),73 })74 secretsManagerMock := &mocks.Manager{}75 secretsManagerMock.On("Get", testContext, secretName, metav1.GetOptions{}).Return(secret, nil)76 secretsManagerMock.On("Update", testContext, updatedSecret, metav1.UpdateOptions{}).Return(secret, nil)77 repository := NewSecretRepository(prepareManagerConstructor(secretsManagerMock))78 // when79 err := repository.Upsert(namespacedName, additionalSecretData)80 // then81 assert.NoError(t, err)82 secretsManagerMock.AssertExpectations(t)83 })84 t.Run("should create new secret if it does not exists", func(t *testing.T) {85 // given86 secret := makeSecret(namespacedName, secretData)87 secretsManagerMock := &mocks.Manager{}88 secretsManagerMock.On("Get", testContext, secretName, metav1.GetOptions{}).Return(nil, k8serrors.NewNotFound(schema.GroupResource{}, "error"))89 secretsManagerMock.On("Update", testContext, secret, metav1.UpdateOptions{}).Return(nil, k8serrors.NewNotFound(schema.GroupResource{}, "error"))90 secretsManagerMock.On("Create", testContext, secret, metav1.CreateOptions{}).Return(secret, nil)91 repository := NewSecretRepository(prepareManagerConstructor(secretsManagerMock))92 // when93 err := repository.Upsert(namespacedName, secretData)94 // then95 assert.NoError(t, err)96 secretsManagerMock.AssertExpectations(t)97 })98 t.Run("should return error when failed to get secret", func(t *testing.T) {99 // given100 secretsManagerMock := &mocks.Manager{}101 secretsManagerMock.On("Get", testContext, secretName, metav1.GetOptions{}).Return(nil, errors.New("error"))102 repository := NewSecretRepository(prepareManagerConstructor(secretsManagerMock))103 // when104 err := repository.Upsert(namespacedName, secretData)105 // then106 assert.Error(t, err)107 secretsManagerMock.AssertExpectations(t)108 })109 t.Run("should return error when failed to update secret", func(t *testing.T) {110 // given111 secret := makeSecret(namespacedName, secretData)112 secretsManagerMock := &mocks.Manager{}113 secretsManagerMock.On("Get", testContext, secretName, metav1.GetOptions{}).Return(nil, k8serrors.NewNotFound(schema.GroupResource{}, "error"))114 secretsManagerMock.On("Update", testContext, secret, metav1.UpdateOptions{}).Return(nil, errors.New("error"))115 repository := NewSecretRepository(prepareManagerConstructor(secretsManagerMock))116 // when117 err := repository.Upsert(namespacedName, secretData)118 // then119 assert.Error(t, err)120 secretsManagerMock.AssertExpectations(t)121 })122 t.Run("should return error when failed to create secret", func(t *testing.T) {123 // given124 secret := makeSecret(namespacedName, secretData)125 secretsManagerMock := &mocks.Manager{}126 secretsManagerMock.On("Get", testContext, secretName, metav1.GetOptions{}).Return(nil, k8serrors.NewNotFound(schema.GroupResource{}, "error"))127 secretsManagerMock.On("Update", testContext, secret, metav1.UpdateOptions{}).Return(nil, k8serrors.NewNotFound(schema.GroupResource{}, "error"))128 secretsManagerMock.On("Create", testContext, secret, metav1.CreateOptions{}).Return(nil, errors.New("error"))129 repository := NewSecretRepository(prepareManagerConstructor(secretsManagerMock))130 // when131 err := repository.Upsert(namespacedName, secretData)132 // then133 assert.Error(t, err)134 secretsManagerMock.AssertExpectations(t)135 })136}137func TestRepository_ValuesProvided(t *testing.T) {138 testCases := []struct {139 description string140 secret *v1.Secret141 error error142 searchedKeys []string143 valuesProvided bool144 }{145 {146 description: "should return true if values provided",147 secret: makeSecret(namespacedName, map[string][]byte{148 dataKey: []byte("value"),149 }),150 searchedKeys: []string{dataKey},151 valuesProvided: true,152 },153 {154 description: "should return false if value empty",155 secret: makeSecret(namespacedName, map[string][]byte{156 dataKey: []byte(""),157 }),158 searchedKeys: []string{dataKey},159 valuesProvided: false,160 },161 {162 description: "should return false if at least one value is empty",163 secret: makeSecret(namespacedName, map[string][]byte{164 dataKey: []byte("data"),165 "anotherKey": []byte(""),166 }),167 searchedKeys: []string{dataKey, "anotherKey"},168 valuesProvided: false,169 },170 {171 description: "should return false if value does not exist",172 secret: makeSecret(namespacedName, map[string][]byte{}),173 searchedKeys: []string{dataKey},174 valuesProvided: false,175 },176 {177 description: "should return false if secret not found",178 secret: nil,179 error: k8serrors.NewNotFound(schema.GroupResource{}, "error"),180 searchedKeys: []string{dataKey},181 valuesProvided: false,182 },183 }184 for _, test := range testCases {185 t.Run(test.description, func(t *testing.T) {186 // given187 secretsManagerMock := &mocks.Manager{}188 secretsManagerMock.On("Get", testContext, secretName, metav1.GetOptions{}).Return(test.secret, test.error)189 repository := NewSecretRepository(prepareManagerConstructor(secretsManagerMock))190 // when191 provided, err := repository.ValuesProvided(namespacedName, test.searchedKeys)192 // then193 require.NoError(t, err)194 assert.Equal(t, test.valuesProvided, provided)195 })196 }197 t.Run("should return error when failed to get secret", func(t *testing.T) {198 // given199 secretsManagerMock := &mocks.Manager{}200 secretsManagerMock.On("Get", testContext, secretName, metav1.GetOptions{}).Return(nil, errors.New("error"))201 repository := NewSecretRepository(prepareManagerConstructor(secretsManagerMock))202 // when203 _, err := repository.ValuesProvided(namespacedName, []string{dataKey})204 // then205 require.Error(t, err)206 })207}208func prepareManagerConstructor(manager Manager) ManagerConstructor {209 return func(namespace string) Manager {210 return manager211 }212}...
login_test.go
Source:login_test.go
...20 ExpectedErr error21 ExpectedData *session.SessionAuth22 InputCPF string23 InputSecret string24 PrepareMockAccount func(mock *mocks.MockAccountStore)25 PrepareMockSession func(mock *mocks.MockSession)26 PrepareMockPassword func(mock *mocks.MockPassword)27 }{28 "return sucess": {29 ExpectedData: &session.SessionAuth{30 Token: "token",31 Expiration: time,32 },33 InputCPF: cpfSucess,34 InputSecret: secretSucess,35 PrepareMockAccount: func(mock *mocks.MockAccountStore) {36 mock.EXPECT().GetByCpf(gomock.Any(), cpfSucess).Return(&model.Account{37 ID: 1, Name: "test", CPF: cpfSucess, SecretHash: "secret_hash", SecretSalt: "secret_salt", Balance: *big.NewInt(100),38 }, nil)39 },40 PrepareMockPassword: func(mock *mocks.MockPassword) {41 mock.EXPECT().Verify(secretSucess, "secret_hash", "secret_salt").Return(true)42 },43 PrepareMockSession: func(mock *mocks.MockSession) {44 mock.EXPECT().Generate(gomock.Any(), &model.Account{45 ID: 1, Name: "test", CPF: cpfSucess, SecretHash: "secret_hash", SecretSalt: "secret_salt", Balance: *big.NewInt(100),46 }).Return("token", time, nil)47 },48 },49 "return error: CPF required": {50 ExpectedErr: errLoginCPFNotInput,51 InputSecret: secretSucess,52 PrepareMockAccount: func(mock *mocks.MockAccountStore) {},53 PrepareMockPassword: func(mock *mocks.MockPassword) {},54 PrepareMockSession: func(mock *mocks.MockSession) {},55 },56 "return error: secret required": {57 ExpectedErr: errLoginSecretNotInput,58 InputCPF: cpfSucess,59 PrepareMockAccount: func(mock *mocks.MockAccountStore) {},60 PrepareMockPassword: func(mock *mocks.MockPassword) {},61 PrepareMockSession: func(mock *mocks.MockSession) {},62 },63 "return error: cpf invalid": {64 ExpectedErr: model.NewError(http.StatusBadRequest, "O cpf é inválido", nil),65 InputCPF: "11111111111",66 InputSecret: secretSucess,67 PrepareMockAccount: func(mock *mocks.MockAccountStore) {},68 PrepareMockPassword: func(mock *mocks.MockPassword) {},69 PrepareMockSession: func(mock *mocks.MockSession) {},70 },71 "return error: store cpf not found": {72 ExpectedErr: errLogin,73 InputCPF: cpfSucess,74 InputSecret: secretSucess,75 PrepareMockAccount: func(mock *mocks.MockAccountStore) {76 mock.EXPECT().GetByCpf(gomock.Any(), cpfSucess).Return(nil, errors.New("cpf not found"))77 },78 PrepareMockPassword: func(mock *mocks.MockPassword) {},79 PrepareMockSession: func(mock *mocks.MockSession) {},80 },81 "return error: valid password": {82 ExpectedErr: errLoginPasswordInvalid,83 InputCPF: cpfSucess,84 InputSecret: secretSucess,85 PrepareMockAccount: func(mock *mocks.MockAccountStore) {86 mock.EXPECT().GetByCpf(gomock.Any(), cpfSucess).Return(&model.Account{87 ID: 1, Name: "test", CPF: cpfSucess, SecretHash: "secret_hash", SecretSalt: "secret_salt", Balance: *big.NewInt(100),88 }, nil)89 },90 PrepareMockPassword: func(mock *mocks.MockPassword) {91 mock.EXPECT().Verify(secretSucess, "secret_hash", "secret_salt").Return(false)92 },93 PrepareMockSession: func(mock *mocks.MockSession) {},94 },95 "return error: generate token": {96 ExpectedErr: errors.New("error generate token"),97 InputCPF: cpfSucess,98 InputSecret: secretSucess,99 PrepareMockAccount: func(mock *mocks.MockAccountStore) {100 mock.EXPECT().GetByCpf(gomock.Any(), cpfSucess).Return(&model.Account{101 ID: 1, Name: "test", CPF: cpfSucess, SecretHash: "secret_hash", SecretSalt: "secret_salt", Balance: *big.NewInt(100),102 }, nil)103 },104 PrepareMockPassword: func(mock *mocks.MockPassword) {105 mock.EXPECT().Verify(secretSucess, "secret_hash", "secret_salt").Return(true)106 },107 PrepareMockSession: func(mock *mocks.MockSession) {108 mock.EXPECT().Generate(gomock.Any(), &model.Account{109 ID: 1, Name: "test", CPF: cpfSucess, SecretHash: "secret_hash", SecretSalt: "secret_salt", Balance: *big.NewInt(100),110 }).Return("", time, errors.New("error generate token"))111 },112 },113 }114 for name, cs := range cases {115 t.Run(name, func(t *testing.T) {116 ctrl, ctx := test.NewController(t)117 mockAccount := mocks.NewMockAccountStore(ctrl)118 mockSession := mocks.NewMockSession(ctrl)119 mockPassword := mocks.NewMockPassword(ctrl)120 cs.PrepareMockAccount(mockAccount)121 cs.PrepareMockSession(mockSession)122 cs.PrepareMockPassword(mockPassword)123 app := NewApp(mockSession, mockAccount, mockPassword)124 data, err := app.Login(ctx, cs.InputCPF, cs.InputSecret)125 assert.Equal(t, data, cs.ExpectedData)126 assert.Equal(t, err, cs.ExpectedErr)127 })128 }129}...
Prepare
Using AI Code Generation
1import (2func main() {3 db, err := sql.Open("mysql", "root:root@/test")4 if err != nil {5 panic(err.Error())6 }7 defer db.Close()8 if err != nil {9 }10 stmtOut, err := db.Prepare("SELECT name FROM test WHERE id = ?")11 if err != nil {12 }13 defer stmtOut.Close()14 for i := 0; i < 25; i++ {15 if err != nil {16 }17 }18 for i := 0; i < 25; i++ {19 if err != nil {20 }21 fmt.Printf("The square number of %d is: %d22 }23}
Prepare
Using AI Code Generation
1import (2func main() {3 nonce, _ := client.PendingNonceAt(context.Background(), common.HexToAddress("0x2e2f2c0b4f6b4d4c4d4a4c4d4c4c4d4a4c4d4c4d4a4c4d4c4d4a4c4d4c4d4a"))4 gasPrice, _ := client.SuggestGasPrice(context.Background())5 gasLimit := uint64(21000)6 receiver := common.HexToAddress("0x2e2f2c0b4f6b4d4c4d4a4c4d4c4c4d4a4c4d4c4d4a4c4d4c4d4a4c4d4c4d4a")7 amount := big.NewInt(1000000000000000000)8 tx := types.NewTransaction(nonce, receiver, amount, gasLimit, gasPrice, nil)9 ks := keystore.NewKeyStore("/home/ethereum/.ethereum/keystore", keystore.StandardScryptN, keystore.StandardScryptP)10 account, _ := ks.Find(accounts.Account{Address: common.HexToAddress("0x2e2f2c0b4f6b4d4c4d4a4c4d4c4c4d4a4c4d4c4d4a4c4d4c4d4a4c4d4c4d4a")})
Prepare
Using AI Code Generation
1import (2func main() {3 fmt.Print("Enter the number of lines: ")4 fmt.Scan(&n)5 fmt.Print("Enter the lines: ")6 scanner := bufio.NewScanner(os.Stdin)7 for i := 0; i < n; i++ {8 scanner.Scan()9 lines = append(lines, scanner.Text())10 }11 fmt.Print("Enter the word to be replaced: ")12 scanner.Scan()13 replace := scanner.Text()14 fmt.Print("Enter the word to replace with: ")15 scanner.Scan()16 replaceWith := scanner.Text()17 fmt.Println("The lines after replacing are:")18 for _, line := range lines {19 line = strings.Replace(line, replace, replaceWith, -1)20 fmt.Println(line)21 }22}
Prepare
Using AI Code Generation
1import (2func main() {3 opts.EncryptionKey = []byte("my-secret-key")4 db, err := badger.Open(opts)5 if err != nil {6 fmt.Println("Error while opening the database")7 }8 defer db.Close()9 err = db.Update(func(txn *badger.Txn) error {10 err := txn.Set([]byte("key"), []byte("value"))11 })12 if err != nil {13 fmt.Println("Error while setting the key")14 }15 err = db.View(func(txn *badger.Txn) error {16 item, err := txn.Get([]byte("key"))17 if err != nil {18 }19 val, err := item.Value()20 if err != nil {21 }22 fmt.Println("The value for key is:", string(val))23 })24 if err != nil {25 fmt.Println("Error while getting the value for the key")26 }27}28import (29func main() {30 db, err := badger.Open(opts)31 if err != nil {32 fmt.Println("Error while opening the database")33 }34 defer db.Close()35 err = db.Update(func(tx
Prepare
Using AI Code Generation
1import (2type Secret struct {3}4func main() {5 if err != nil {6 log.Fatal("NewRequest: ", err)7 }8 client := &http.Client{}9 resp, err := client.Do(req)10 if err != nil {11 log.Fatal("Do: ", err)12 }13 defer resp.Body.Close()14 if err := json.NewDecoder(resp.Body).Decode(&record); err != nil {15 log.Println(err)16 }17 fmt.Printf("%+v18}
Prepare
Using AI Code Generation
1import (2func main() {3 privateKey, err := rsa.GenerateKey(rand.Reader, 2048)4 if err != nil {5 log.Fatal(err)6 }7 privateKeyBytes := x509.MarshalPKCS1PrivateKey(privateKey)8 privateKeyFile, err := ioutil.TempFile(".", "privatekey")9 if err != nil {10 log.Fatal(err)11 }12 defer privateKeyFile.Close()13 if err := pem.Encode(privateKeyFile, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: privateKeyBytes}); err != nil {14 log.Fatal(err)15 }16 publicKeyBytes, err := x509.MarshalPKIXPublicKey(&publicKey)17 if err != nil {18 log.Fatal(err)19 }20 publicKeyFile, err := ioutil.TempFile(".", "publickey")21 if err != nil {22 log.Fatal(err)23 }24 defer publicKeyFile.Close()25 if err := pem.Encode(publicKeyFile, &pem.Block{Type: "RSA PUBLIC KEY", Bytes: publicKeyBytes}); err != nil {26 log.Fatal(err)27 }28 secret, err := rsa.Prepare(rand.Reader, privateKey, publicKey)29 if err != nil {30 log.Fatal(err)31 }32 plainText := []byte("Hello World")33 cipherText, err := secret.Encrypt(rand.Reader, plainText, nil)34 if err != nil {35 log.Fatal(err)36 }37 decryptedText, err := secret.Decrypt(rand.Reader, cipherText, nil)38 if err != nil {39 log.Fatal(err)40 }41 fmt.Println(string(decryptedText))42}
Prepare
Using AI Code Generation
1import (2func main() {3 message := []byte("The name of the secret agent is James Bond")4 key := []byte("passphrasewhichneedstobe32bytes!")5 block, err := aes.NewCipher(key)6 if err != nil {7 log.Fatal(err)8 }9 aesGCM, err := cipher.NewGCM(block)10 if err != nil {11 log.Fatal(err)12 }13 nonce := make([]byte, aesGCM.NonceSize())14 if _, err = io.ReadFull(rand.Reader, nonce); err != nil {15 log.Fatal(err)16 }17 ciphertext := aesGCM.Seal(nonce, nonce, message, nil)18 fmt.Printf("%s => %x19 nonce, ciphertext = ciphertext[:aesGCM.NonceSize()], ciphertext[aesGCM.NonceSize():]
Prepare
Using AI Code Generation
1func main() {2 secret := &secret{3 }4 secret.Prepare()5 fmt.Println(secret.key)6}7func main() {8 secret := &secret{9 }10 secret.Prepare()11 fmt.Println(secret.key)12}13func main() {14 secret := &secret{15 }16 secret.Prepare()17 fmt.Println(secret.key)18}19func main() {20 secret := &secret{21 }22 secret.Prepare()23 fmt.Println(secret.key)24}25func main() {26 secret := &secret{27 }28 secret.Prepare()29 fmt.Println(secret.key)30}31func main() {32 secret := &secret{33 }34 secret.Prepare()35 fmt.Println(secret.key)36}37func main() {38 secret := &secret{39 }40 secret.Prepare()41 fmt.Println(secret.key)42}43func main() {44 secret := &secret{45 }46 secret.Prepare()47 fmt.Println(secret.key)48}49func main() {50 secret := &secret{51 }52 secret.Prepare()53 fmt.Println(secret.key)54}
Prepare
Using AI Code Generation
1secret := secret.New()2secret.Prepare()3secret.SetSecret("This is my secret")4secret.PrintSecret()5secret := secret.New()6secret.Prepare()7secret.SetSecret("This is my secret")8secret.PrintSecret()9secret := secret.New()10secret.Prepare()11secret.SetSecret("This is my secret")12secret.PrintSecret()13secret := secret.New()14secret.Prepare()15secret.SetSecret("This is my secret")16secret.PrintSecret()17secret := secret.New()18secret.Prepare()19secret.SetSecret("This is my secret")20secret.PrintSecret()21secret := secret.New()22secret.Prepare()23secret.SetSecret("This is my secret")24secret.PrintSecret()25secret := secret.New()26secret.Prepare()27secret.SetSecret("This is my secret")28secret.PrintSecret()29secret := secret.New()30secret.Prepare()31secret.SetSecret("This is my secret")32secret.PrintSecret()33secret := secret.New()34secret.Prepare()35secret.SetSecret("This is my secret")36secret.PrintSecret()37secret := secret.New()38secret.Prepare()39secret.SetSecret("This is my secret")40secret.PrintSecret()41secret := secret.New()42secret.Prepare()43secret.SetSecret("This is my secret")44secret.PrintSecret()45secret := secret.New()46secret.Prepare()
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!