github.com/mundipagg/boleto-api@v0.0.0-20230620145841-3f9ec742599f/infrastructure/storage/azure_blob_test.go (about) 1 //go:build integration || !unit 2 // +build integration !unit 3 4 package storage_test 5 6 import ( 7 "context" 8 "testing" 9 "time" 10 11 "github.com/mundipagg/boleto-api/config" 12 "github.com/mundipagg/boleto-api/infrastructure/storage" 13 "github.com/mundipagg/boleto-api/mock" 14 "github.com/stretchr/testify/assert" 15 ) 16 17 func Test_NewAzureBlob_WhenInvalidParameters_ReturnError(t *testing.T) { 18 AzureBlobClient, err := storage.NewAzureBlob("", "", "", false) 19 20 expected := "either the AZURE_STORAGE_ACCOUNT, AZURE_STORAGE_ACCESS_KEY or Container name cannot be empty" 21 22 assert.Equal(t, expected, err.Error()) 23 assert.Nil(t, AzureBlobClient) 24 } 25 26 func Test_NewAzureBlob_WhenValidParameters_ReturAzureBlobClient(t *testing.T) { 27 mock.StartMockService("9033") 28 AzureBlobClient, err := storage.NewAzureBlob( 29 config.Get().AzureStorageAccount, 30 config.Get().AzureStorageAccessKey, 31 config.Get().AzureStorageContainerName, 32 config.Get().DevMode) 33 34 assert.Nil(t, err) 35 assert.NotNil(t, AzureBlobClient) 36 } 37 38 func TestAzureBlob_Download(t *testing.T) { 39 mock.StartMockService("9036") 40 azureBlobInst, err := storage.NewAzureBlob( 41 config.Get().AzureStorageAccount, 42 config.Get().AzureStorageAccessKey, 43 config.Get().AzureStorageContainerName, 44 config.Get().DevMode, 45 ) 46 assert.Nil(t, err) 47 48 type args struct { 49 path string 50 filename string 51 } 52 tests := []struct { 53 name string 54 ab *storage.AzureBlob 55 args args 56 want string 57 wantErr bool 58 }{ 59 { 60 name: "Donwload successfully", 61 ab: azureBlobInst, 62 args: args{ 63 path: config.Get().AzureStorageOpenBankSkPath, 64 filename: config.Get().AzureStorageOpenBankSkName, 65 }, 66 want: "secret", 67 wantErr: false, 68 }, 69 } 70 for _, tt := range tests { 71 t.Run(tt.name, func(t *testing.T) { 72 got, err := tt.ab.Download(tt.args.path, tt.args.filename) 73 assert.False(t, (err != nil) != tt.wantErr) 74 assert.NotNil(t, got) 75 }) 76 } 77 } 78 79 func Test_Upload_WhenValidParameters_LoadsSuccessfully(t *testing.T) { 80 mock.StartMockService("9035") 81 clientBlob, err := storage.GetClient() 82 83 assert.Nil(t, err) 84 85 payload := `{"ID":"6127b37d36b0e8770b1668ae","uid":"7ce410cd-0682-11ec-852e-00059a3c7a00","secretkey":"7ce410cd-0682-11ec-852e-00059a3c7a00","publickey":"dad58ecd903ceda1ce6e479ff1e6fab399c8207dd000af127cbcdbb5cd3dfe8d","boleto":{"authentication":{},"agreement":{"agreementNumber":1103388,"agency":"3337"},"title":{"createDate":"2021-08-26T00:00:00Z","expireDateTime":"2021-08-31T00:00:00Z","expireDate":"2021-08-31","amountInCents":200,"ourNumber":14000000019047441,"instructions":"NÃO RECEBER APÓS O VENCIMENTO. O prazo de compensação de boleto é de até 3 dias úteis após o pagamento, o valor do limite poderá ficar bloqueado até o processamento.","documentNumber":"12345678901","boletoType":"OUT","BoletoTypeCode":"99"},"recipient":{"name":"Nome do Recebedor (Loja)","document":{"type":"CNPJ","number":"18727053000174"},"address":{"street":"Logradouro do Recebedor","number":"1000","complement":"Sala 01","zipCode":"00000000","city":"Cidade do Recebedor","district":"Bairro do Recebdor","stateCode":"RJ"}},"buyer":{"name":"Nome do Comprador (Cliente)","email":"comprador@gmail.com","document":{"type":"CPF","number":"11282705792"},"address":{"street":"Logradouro do Comprador","number":"1000","complement":"Casa 01","zipCode":"01001000","city":"Cidade do Comprador","district":"Bairro do Comprador","stateCode":"SC"}},"bankNumber":104,"requestKey":"5239ad4a-2a97-4d39-905a-2cc304971d11"},"bankId":104,"createDate":"2021-08-26T12:30:05.2479181-03:00","bankNumber":"104-0","digitableLine":"10492.00650 61000.100042 09922.269841 3 72670000001000","ourNumber":"14000000099222698","barcode":"10493726700000010002006561000100040992226984","links":[{"href":"http://localhost:3000/boleto?fmt=html\u0026id=6127b37d36b0e8770b1668ae\u0026pk=dad58ecd903ceda1ce6e479ff1e6fab399c8207dd000af127cbcdbb5cd3dfe8d","rel":"html","method":"GET"},{"href":"http://localhost:3000/boleto?fmt=pdf\u0026id=6127b37d36b0e8770b1668ae\u0026pk=dad58ecd903ceda1ce6e479ff1e6fab399c8207dd000af127cbcdbb5cd3dfe8d","rel":"pdf","method":"GET"}]}` 86 87 ctx, cancel := context.WithTimeout(context.Background(), time.Duration(time.Second*20)) 88 defer cancel() 89 90 _, err = clientBlob.UploadAsJson( 91 ctx, 92 "FileNameTest.json", 93 payload) 94 95 assert.Nil(t, err) 96 } 97 98 func Test_Upload_WhenInvalidAuthentication_LoadsSuccessfully(t *testing.T) { 99 mock.StartMockService("9034") 100 clientBlob, _ := storage.NewAzureBlob( 101 "loginXXX", 102 "passwordXXX", 103 config.Get().AzureStorageContainerName, 104 config.Get().DevMode, 105 ) 106 107 ctx, cancel := context.WithTimeout(context.Background(), time.Duration(time.Second*20)) 108 defer cancel() 109 110 _, err := clientBlob.UploadAsJson( 111 ctx, 112 "fileNamePrefix", 113 "payload") 114 115 assert.NotNil(t, err) 116 }