|
| 1 | +package v1 |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "encoding/json" |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | + |
| 13 | + "github.com/device-management-toolkit/console/internal/entity/dto/v1" |
| 14 | + "github.com/device-management-toolkit/console/internal/mocks" |
| 15 | +) |
| 16 | + |
| 17 | +func TestGetBootCapabilities(t *testing.T) { |
| 18 | + t.Parallel() |
| 19 | + |
| 20 | + tests := []struct { |
| 21 | + name string |
| 22 | + mock func(m *mocks.MockDeviceManagementFeature) |
| 23 | + expectedCode int |
| 24 | + response interface{} |
| 25 | + }{ |
| 26 | + { |
| 27 | + name: "getBootCapabilities - successful retrieval", |
| 28 | + mock: func(m *mocks.MockDeviceManagementFeature) { |
| 29 | + m.EXPECT().GetBootCapabilities(context.Background(), "valid-guid"). |
| 30 | + Return(dto.BootCapabilities{IDER: true, SOL: true}, nil) |
| 31 | + }, |
| 32 | + expectedCode: http.StatusOK, |
| 33 | + response: dto.BootCapabilities{IDER: true, SOL: true}, |
| 34 | + }, |
| 35 | + { |
| 36 | + name: "getBootCapabilities - service failure", |
| 37 | + mock: func(m *mocks.MockDeviceManagementFeature) { |
| 38 | + m.EXPECT().GetBootCapabilities(context.Background(), "valid-guid"). |
| 39 | + Return(dto.BootCapabilities{}, ErrGeneral) |
| 40 | + }, |
| 41 | + expectedCode: http.StatusInternalServerError, |
| 42 | + response: nil, |
| 43 | + }, |
| 44 | + } |
| 45 | + |
| 46 | + for _, tc := range tests { |
| 47 | + tc := tc |
| 48 | + |
| 49 | + t.Run(tc.name, func(t *testing.T) { |
| 50 | + t.Parallel() |
| 51 | + |
| 52 | + deviceManagement, engine := deviceManagementTest(t) |
| 53 | + tc.mock(deviceManagement) |
| 54 | + |
| 55 | + req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, "/api/v1/amt/boot/capabilities/valid-guid", http.NoBody) |
| 56 | + require.NoError(t, err) |
| 57 | + |
| 58 | + w := httptest.NewRecorder() |
| 59 | + engine.ServeHTTP(w, req) |
| 60 | + |
| 61 | + require.Equal(t, tc.expectedCode, w.Code) |
| 62 | + |
| 63 | + if tc.expectedCode == http.StatusOK { |
| 64 | + jsonBytes, _ := json.Marshal(tc.response) |
| 65 | + require.Equal(t, string(jsonBytes), w.Body.String()) |
| 66 | + } |
| 67 | + }) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func TestSetRPEEnabled(t *testing.T) { |
| 72 | + t.Parallel() |
| 73 | + |
| 74 | + tests := []struct { |
| 75 | + name string |
| 76 | + requestBody interface{} |
| 77 | + mock func(m *mocks.MockDeviceManagementFeature) |
| 78 | + expectedCode int |
| 79 | + }{ |
| 80 | + { |
| 81 | + name: "setRPEEnabled - successful (enabled=true)", |
| 82 | + requestBody: dto.RPERequest{Enabled: true}, |
| 83 | + mock: func(m *mocks.MockDeviceManagementFeature) { |
| 84 | + m.EXPECT().SetRPEEnabled(context.Background(), "valid-guid", true). |
| 85 | + Return(nil) |
| 86 | + }, |
| 87 | + expectedCode: http.StatusOK, |
| 88 | + }, |
| 89 | + { |
| 90 | + name: "setRPEEnabled - successful (enabled=false)", |
| 91 | + requestBody: dto.RPERequest{Enabled: false}, |
| 92 | + mock: func(m *mocks.MockDeviceManagementFeature) { |
| 93 | + m.EXPECT().SetRPEEnabled(context.Background(), "valid-guid", false). |
| 94 | + Return(nil) |
| 95 | + }, |
| 96 | + expectedCode: http.StatusOK, |
| 97 | + }, |
| 98 | + { |
| 99 | + name: "setRPEEnabled - invalid JSON payload", |
| 100 | + requestBody: "invalid-json", |
| 101 | + mock: func(_ *mocks.MockDeviceManagementFeature) { |
| 102 | + }, |
| 103 | + expectedCode: http.StatusInternalServerError, |
| 104 | + }, |
| 105 | + { |
| 106 | + name: "setRPEEnabled - service failure", |
| 107 | + requestBody: dto.RPERequest{Enabled: true}, |
| 108 | + mock: func(m *mocks.MockDeviceManagementFeature) { |
| 109 | + m.EXPECT().SetRPEEnabled(context.Background(), "valid-guid", true). |
| 110 | + Return(ErrGeneral) |
| 111 | + }, |
| 112 | + expectedCode: http.StatusInternalServerError, |
| 113 | + }, |
| 114 | + } |
| 115 | + |
| 116 | + for _, tc := range tests { |
| 117 | + tc := tc |
| 118 | + |
| 119 | + t.Run(tc.name, func(t *testing.T) { |
| 120 | + t.Parallel() |
| 121 | + |
| 122 | + deviceManagement, engine := deviceManagementTest(t) |
| 123 | + tc.mock(deviceManagement) |
| 124 | + |
| 125 | + reqBody, _ := json.Marshal(tc.requestBody) |
| 126 | + req, err := http.NewRequestWithContext(context.Background(), http.MethodPost, "/api/v1/amt/boot/rpe/valid-guid", bytes.NewBuffer(reqBody)) |
| 127 | + require.NoError(t, err) |
| 128 | + |
| 129 | + w := httptest.NewRecorder() |
| 130 | + engine.ServeHTTP(w, req) |
| 131 | + |
| 132 | + require.Equal(t, tc.expectedCode, w.Code) |
| 133 | + }) |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +func TestSendRemoteErase(t *testing.T) { |
| 138 | + t.Parallel() |
| 139 | + |
| 140 | + tests := []struct { |
| 141 | + name string |
| 142 | + requestBody interface{} |
| 143 | + mock func(m *mocks.MockDeviceManagementFeature) |
| 144 | + expectedCode int |
| 145 | + }{ |
| 146 | + { |
| 147 | + name: "sendRemoteErase - successful", |
| 148 | + requestBody: dto.RemoteEraseRequest{EraseMask: 3}, |
| 149 | + mock: func(m *mocks.MockDeviceManagementFeature) { |
| 150 | + m.EXPECT().SendRemoteErase(context.Background(), "valid-guid", 3). |
| 151 | + Return(nil) |
| 152 | + }, |
| 153 | + expectedCode: http.StatusOK, |
| 154 | + }, |
| 155 | + { |
| 156 | + name: "sendRemoteErase - invalid JSON payload", |
| 157 | + requestBody: "invalid-json", |
| 158 | + mock: func(_ *mocks.MockDeviceManagementFeature) { |
| 159 | + }, |
| 160 | + expectedCode: http.StatusInternalServerError, |
| 161 | + }, |
| 162 | + { |
| 163 | + name: "sendRemoteErase - service failure", |
| 164 | + requestBody: dto.RemoteEraseRequest{EraseMask: 1}, |
| 165 | + mock: func(m *mocks.MockDeviceManagementFeature) { |
| 166 | + m.EXPECT().SendRemoteErase(context.Background(), "valid-guid", 1). |
| 167 | + Return(ErrGeneral) |
| 168 | + }, |
| 169 | + expectedCode: http.StatusInternalServerError, |
| 170 | + }, |
| 171 | + } |
| 172 | + |
| 173 | + for _, tc := range tests { |
| 174 | + tc := tc |
| 175 | + |
| 176 | + t.Run(tc.name, func(t *testing.T) { |
| 177 | + t.Parallel() |
| 178 | + |
| 179 | + deviceManagement, engine := deviceManagementTest(t) |
| 180 | + tc.mock(deviceManagement) |
| 181 | + |
| 182 | + reqBody, _ := json.Marshal(tc.requestBody) |
| 183 | + req, err := http.NewRequestWithContext(context.Background(), http.MethodPost, "/api/v1/amt/remoteErase/valid-guid", bytes.NewBuffer(reqBody)) |
| 184 | + require.NoError(t, err) |
| 185 | + |
| 186 | + w := httptest.NewRecorder() |
| 187 | + engine.ServeHTTP(w, req) |
| 188 | + |
| 189 | + require.Equal(t, tc.expectedCode, w.Code) |
| 190 | + }) |
| 191 | + } |
| 192 | +} |
0 commit comments