|
| 1 | +package packages |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "net/http" |
| 6 | + "testing" |
| 7 | + |
| 8 | + mockhttp "github.com/jfrog/jfrog-cli-application/apptrust/http/mocks" |
| 9 | + mockservice "github.com/jfrog/jfrog-cli-application/apptrust/service/mocks" |
| 10 | + "go.uber.org/mock/gomock" |
| 11 | + |
| 12 | + "github.com/jfrog/jfrog-cli-application/apptrust/model" |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | +) |
| 15 | + |
| 16 | +func TestBindPackage(t *testing.T) { |
| 17 | + ctrl := gomock.NewController(t) |
| 18 | + defer ctrl.Finish() |
| 19 | + |
| 20 | + service := NewPackageService() |
| 21 | + |
| 22 | + tests := []struct { |
| 23 | + name string |
| 24 | + request *model.BindPackageRequest |
| 25 | + mockResponse *http.Response |
| 26 | + mockError error |
| 27 | + expectedError string |
| 28 | + }{ |
| 29 | + { |
| 30 | + name: "success", |
| 31 | + request: &model.BindPackageRequest{ |
| 32 | + ApplicationKey: "test-app", |
| 33 | + Type: "npm", |
| 34 | + Name: "test-package", |
| 35 | + Version: "1.0.0", |
| 36 | + }, |
| 37 | + mockResponse: &http.Response{StatusCode: 201}, |
| 38 | + mockError: nil, |
| 39 | + expectedError: "", |
| 40 | + }, |
| 41 | + { |
| 42 | + name: "failure - bad request", |
| 43 | + request: &model.BindPackageRequest{ |
| 44 | + ApplicationKey: "test-app", |
| 45 | + Type: "npm", |
| 46 | + Name: "test-package", |
| 47 | + Version: "1.0.0", |
| 48 | + }, |
| 49 | + mockResponse: &http.Response{StatusCode: 400}, |
| 50 | + mockError: nil, |
| 51 | + expectedError: "failed to bind package. Status code: 400", |
| 52 | + }, |
| 53 | + { |
| 54 | + name: "failure - unauthorized", |
| 55 | + request: &model.BindPackageRequest{ |
| 56 | + ApplicationKey: "test-app", |
| 57 | + Type: "npm", |
| 58 | + Name: "test-package", |
| 59 | + Version: "1.0.0", |
| 60 | + }, |
| 61 | + mockResponse: &http.Response{StatusCode: 401}, |
| 62 | + mockError: nil, |
| 63 | + expectedError: "failed to bind package. Status code: 401", |
| 64 | + }, |
| 65 | + { |
| 66 | + name: "failure - not found", |
| 67 | + request: &model.BindPackageRequest{ |
| 68 | + ApplicationKey: "non-existent-app", |
| 69 | + Type: "npm", |
| 70 | + Name: "test-package", |
| 71 | + Version: "1.0.0", |
| 72 | + }, |
| 73 | + mockResponse: &http.Response{StatusCode: 404}, |
| 74 | + mockError: nil, |
| 75 | + expectedError: "failed to bind package. Status code: 404", |
| 76 | + }, |
| 77 | + { |
| 78 | + name: "failure - internal server error", |
| 79 | + request: &model.BindPackageRequest{ |
| 80 | + ApplicationKey: "test-app", |
| 81 | + Type: "npm", |
| 82 | + Name: "test-package", |
| 83 | + Version: "1.0.0", |
| 84 | + }, |
| 85 | + mockResponse: &http.Response{StatusCode: 500}, |
| 86 | + mockError: nil, |
| 87 | + expectedError: "failed to bind package. Status code: 500", |
| 88 | + }, |
| 89 | + { |
| 90 | + name: "http client error", |
| 91 | + request: &model.BindPackageRequest{ |
| 92 | + ApplicationKey: "test-app", |
| 93 | + Type: "npm", |
| 94 | + Name: "test-package", |
| 95 | + Version: "1.0.0", |
| 96 | + }, |
| 97 | + mockResponse: nil, |
| 98 | + mockError: errors.New("http client error"), |
| 99 | + expectedError: "http client error", |
| 100 | + }, |
| 101 | + } |
| 102 | + |
| 103 | + for _, tt := range tests { |
| 104 | + t.Run(tt.name, func(t *testing.T) { |
| 105 | + mockHttpClient := mockhttp.NewMockApptrustHttpClient(ctrl) |
| 106 | + mockHttpClient.EXPECT().Post("/v1/package", tt.request). |
| 107 | + Return(tt.mockResponse, []byte(""), tt.mockError).Times(1) |
| 108 | + |
| 109 | + mockCtx := mockservice.NewMockContext(ctrl) |
| 110 | + mockCtx.EXPECT().GetHttpClient().Return(mockHttpClient).Times(1) |
| 111 | + |
| 112 | + err := service.BindPackage(mockCtx, tt.request) |
| 113 | + if tt.expectedError == "" { |
| 114 | + assert.NoError(t, err) |
| 115 | + } else { |
| 116 | + assert.Error(t, err) |
| 117 | + assert.Contains(t, err.Error(), tt.expectedError) |
| 118 | + } |
| 119 | + }) |
| 120 | + } |
| 121 | +} |
0 commit comments