|
1 | 1 | package v1 |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/json" |
4 | 5 | "errors" |
| 6 | + "io" |
5 | 7 | "net/http" |
6 | 8 | "net/http/httptest" |
7 | 9 | "testing" |
| 10 | + "time" |
8 | 11 |
|
9 | 12 | "github.com/gin-gonic/gin" |
10 | 13 | "github.com/stretchr/testify/assert" |
11 | 14 |
|
| 15 | + "github.com/device-management-toolkit/console/internal/entity/dto/v1" |
12 | 16 | wsmanAPI "github.com/device-management-toolkit/console/internal/usecase/devices/wsman" |
| 17 | + "github.com/device-management-toolkit/console/internal/usecase/domains" |
13 | 18 | "github.com/device-management-toolkit/console/internal/usecase/profiles" |
| 19 | + "github.com/device-management-toolkit/console/pkg/consoleerrors" |
14 | 20 | ) |
15 | 21 |
|
16 | 22 | func TestMain(m *testing.M) { |
@@ -44,6 +50,70 @@ func TestErrorResponse_CIRADeviceNotConnected(t *testing.T) { |
44 | 50 | assert.Equal(t, http.StatusServiceUnavailable, w.Code) |
45 | 51 | } |
46 | 52 |
|
| 53 | +func TestErrorResponse_InvalidProvisioningCertificate(t *testing.T) { |
| 54 | + t.Parallel() |
| 55 | + |
| 56 | + err := domains.ErrCertFormat.Wrap("test", "base64.StdEncoding.DecodeString", errors.New("illegal base64 data")) |
| 57 | + w := runErrorResponse(t, err) |
| 58 | + |
| 59 | + assert.Equal(t, http.StatusBadRequest, w.Code) |
| 60 | + assert.JSONEq(t, `{"error":"invalid provisioning certificate","message":"invalid provisioning certificate"}`, w.Body.String()) |
| 61 | +} |
| 62 | + |
| 63 | +func TestErrorResponse_JSONBindingError(t *testing.T) { |
| 64 | + t.Parallel() |
| 65 | + |
| 66 | + for _, requestBody := range []string{ |
| 67 | + `"fuzzstring"`, |
| 68 | + `{"action":false}`, |
| 69 | + `{"action":"fuzzstring"}`, |
| 70 | + `{"bootPath":"\OemPba.efi"}`, |
| 71 | + } { |
| 72 | + var powerAction dto.PowerAction |
| 73 | + |
| 74 | + err := json.Unmarshal([]byte(requestBody), &powerAction) |
| 75 | + w := runErrorResponse(t, err) |
| 76 | + |
| 77 | + assert.Equal(t, http.StatusBadRequest, w.Code) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +func TestErrorResponse_WrappedJSONBindingError(t *testing.T) { |
| 82 | + t.Parallel() |
| 83 | + |
| 84 | + var device dto.Device |
| 85 | + |
| 86 | + err := json.Unmarshal([]byte(`{"tags":"test"}`), &device) |
| 87 | + err = dto.NotValidError{Console: consoleerrors.CreateConsoleError("ProfileAPI")}.Wrap("insert", "json.Unmarshal", err) |
| 88 | + w := runErrorResponse(t, err) |
| 89 | + |
| 90 | + assert.Equal(t, http.StatusBadRequest, w.Code) |
| 91 | + assert.JSONEq(t, `{"error":"Invalid input: json: cannot unmarshal string into Go struct field Device.tags of type []string","message":"Invalid input: json: cannot unmarshal string into Go struct field Device.tags of type []string"}`, w.Body.String()) |
| 92 | +} |
| 93 | + |
| 94 | +func TestErrorResponse_InvalidTimestamp(t *testing.T) { |
| 95 | + t.Parallel() |
| 96 | + |
| 97 | + var alarm dto.AlarmClockOccurrenceInput |
| 98 | + |
| 99 | + err := json.Unmarshal([]byte(`{"StartTime":"fuzzstring"}`), &alarm) |
| 100 | + w := runErrorResponse(t, err) |
| 101 | + |
| 102 | + var parseErr *time.ParseError |
| 103 | + assert.ErrorAs(t, err, &parseErr) |
| 104 | + assert.Equal(t, http.StatusBadRequest, w.Code) |
| 105 | +} |
| 106 | + |
| 107 | +func TestErrorResponse_EmptyRequestBody(t *testing.T) { |
| 108 | + t.Parallel() |
| 109 | + |
| 110 | + for _, err := range []error{io.EOF, io.ErrUnexpectedEOF} { |
| 111 | + w := runErrorResponse(t, err) |
| 112 | + |
| 113 | + assert.Equal(t, http.StatusBadRequest, w.Code) |
| 114 | + } |
| 115 | +} |
| 116 | + |
47 | 117 | func TestHandleSentinelErrors_CIRADeviceNotConnected(t *testing.T) { |
48 | 118 | t.Parallel() |
49 | 119 |
|
|
0 commit comments