|
| 1 | +package e2e |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/go-resty/resty/v2" |
| 8 | +) |
| 9 | + |
| 10 | +type messageState struct { |
| 11 | + ID string `json:"id"` |
| 12 | + DeviceID string `json:"deviceId"` |
| 13 | + State string `json:"state"` |
| 14 | + IsHashed bool `json:"isHashed"` |
| 15 | + IsEncrypted bool `json:"isEncrypted"` |
| 16 | + Recipients []string `json:"recipients"` |
| 17 | + States []state `json:"states"` |
| 18 | +} |
| 19 | + |
| 20 | +type state struct { |
| 21 | + PhoneNumber string `json:"phoneNumber"` |
| 22 | + State string `json:"state"` |
| 23 | +} |
| 24 | + |
| 25 | +type errorResponse struct { |
| 26 | + Message string `json:"message"` |
| 27 | +} |
| 28 | + |
| 29 | +func TestMessages_GetMessages(t *testing.T) { |
| 30 | + credentials := mobileDeviceRegister(t, publicMobileClient) |
| 31 | + authorizedClient := publicUserClient.Clone().SetBasicAuth(credentials.Login, credentials.Password) |
| 32 | + |
| 33 | + cases := []struct { |
| 34 | + name string |
| 35 | + setup func() |
| 36 | + expectedStatusCode int |
| 37 | + request func() *resty.Request |
| 38 | + validate func(t *testing.T, response *resty.Response) |
| 39 | + }{ |
| 40 | + { |
| 41 | + name: "Successful retrieval with default parameters", |
| 42 | + setup: func() { |
| 43 | + // Test data is populated by the test infrastructure |
| 44 | + }, |
| 45 | + expectedStatusCode: 200, |
| 46 | + request: func() *resty.Request { |
| 47 | + return authorizedClient.R() |
| 48 | + }, |
| 49 | + validate: func(t *testing.T, response *resty.Response) { |
| 50 | + if response.StatusCode() != 200 { |
| 51 | + t.Fatal(response.StatusCode(), response.String()) |
| 52 | + } |
| 53 | + |
| 54 | + var result []messageState |
| 55 | + if err := json.Unmarshal(response.Body(), &result); err != nil { |
| 56 | + t.Fatal(err) |
| 57 | + } |
| 58 | + |
| 59 | + // Verify response structure |
| 60 | + if len(result) > 0 { |
| 61 | + msg := result[0] |
| 62 | + if msg.ID == "" { |
| 63 | + t.Error("message ID is empty") |
| 64 | + } |
| 65 | + if msg.DeviceID == "" { |
| 66 | + t.Error("device ID is empty") |
| 67 | + } |
| 68 | + if msg.State == "" { |
| 69 | + t.Error("message state is empty") |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + // Verify response headers |
| 74 | + if response.Header().Get("Content-Type") != "application/json" { |
| 75 | + t.Error("expected Content-Type to be application/json") |
| 76 | + } |
| 77 | + }, |
| 78 | + }, |
| 79 | + { |
| 80 | + name: "Pagination with limit=10, offset=5", |
| 81 | + setup: func() { |
| 82 | + // Test data is populated by the test infrastructure |
| 83 | + }, |
| 84 | + expectedStatusCode: 200, |
| 85 | + request: func() *resty.Request { |
| 86 | + return authorizedClient.R(). |
| 87 | + SetQueryParams(map[string]string{ |
| 88 | + "limit": "10", |
| 89 | + "offset": "5", |
| 90 | + }) |
| 91 | + }, |
| 92 | + validate: func(t *testing.T, response *resty.Response) { |
| 93 | + if response.StatusCode() != 200 { |
| 94 | + t.Fatal(response.StatusCode(), response.String()) |
| 95 | + } |
| 96 | + |
| 97 | + // Verify X-Total-Count header |
| 98 | + totalCount := response.Header().Get("X-Total-Count") |
| 99 | + if totalCount == "" { |
| 100 | + t.Error("expected X-Total-Count header") |
| 101 | + } |
| 102 | + |
| 103 | + var result []messageState |
| 104 | + if err := json.Unmarshal(response.Body(), &result); err != nil { |
| 105 | + t.Fatal(err) |
| 106 | + } |
| 107 | + |
| 108 | + // Verify pagination limits |
| 109 | + if len(result) > 10 { |
| 110 | + t.Errorf("expected at most 10 messages, got %d", len(result)) |
| 111 | + } |
| 112 | + }, |
| 113 | + }, |
| 114 | + { |
| 115 | + name: "Date range filter", |
| 116 | + setup: func() { |
| 117 | + // Test data is populated by the test infrastructure |
| 118 | + }, |
| 119 | + expectedStatusCode: 200, |
| 120 | + request: func() *resty.Request { |
| 121 | + return authorizedClient.R(). |
| 122 | + SetQueryParams(map[string]string{ |
| 123 | + "from": "2025-07-01T00:00:00Z", |
| 124 | + "to": "2025-07-31T23:59:59Z", |
| 125 | + }) |
| 126 | + }, |
| 127 | + validate: func(t *testing.T, response *resty.Response) { |
| 128 | + if response.StatusCode() != 200 { |
| 129 | + t.Fatal(response.StatusCode(), response.String()) |
| 130 | + } |
| 131 | + |
| 132 | + var result []messageState |
| 133 | + if err := json.Unmarshal(response.Body(), &result); err != nil { |
| 134 | + t.Fatal(err) |
| 135 | + } |
| 136 | + |
| 137 | + // Verify response structure |
| 138 | + for _, msg := range result { |
| 139 | + if msg.ID == "" { |
| 140 | + t.Error("message ID is empty") |
| 141 | + } |
| 142 | + if msg.DeviceID == "" { |
| 143 | + t.Error("device ID is empty") |
| 144 | + } |
| 145 | + } |
| 146 | + }, |
| 147 | + }, |
| 148 | + { |
| 149 | + name: "State filter (Sent)", |
| 150 | + setup: func() { |
| 151 | + // Test data is populated by the test infrastructure |
| 152 | + }, |
| 153 | + expectedStatusCode: 200, |
| 154 | + request: func() *resty.Request { |
| 155 | + return authorizedClient.R(). |
| 156 | + SetQueryParam("state", "Sent") |
| 157 | + }, |
| 158 | + validate: func(t *testing.T, response *resty.Response) { |
| 159 | + if response.StatusCode() != 200 { |
| 160 | + t.Fatal(response.StatusCode(), response.String()) |
| 161 | + } |
| 162 | + |
| 163 | + var result []messageState |
| 164 | + if err := json.Unmarshal(response.Body(), &result); err != nil { |
| 165 | + t.Fatal(err) |
| 166 | + } |
| 167 | + |
| 168 | + // Verify all messages have Sent state |
| 169 | + for _, msg := range result { |
| 170 | + if msg.State != "Sent" { |
| 171 | + t.Errorf("expected state 'Sent', got '%s'", msg.State) |
| 172 | + } |
| 173 | + } |
| 174 | + }, |
| 175 | + }, |
| 176 | + { |
| 177 | + name: "Device ID filter", |
| 178 | + setup: func() { |
| 179 | + // Test data is populated by the test infrastructure |
| 180 | + }, |
| 181 | + expectedStatusCode: 200, |
| 182 | + request: func() *resty.Request { |
| 183 | + return authorizedClient.R(). |
| 184 | + SetQueryParam("deviceId", credentials.ID) |
| 185 | + }, |
| 186 | + validate: func(t *testing.T, response *resty.Response) { |
| 187 | + if response.StatusCode() != 200 { |
| 188 | + t.Fatal(response.StatusCode(), response.String()) |
| 189 | + } |
| 190 | + |
| 191 | + var result []messageState |
| 192 | + if err := json.Unmarshal(response.Body(), &result); err != nil { |
| 193 | + t.Fatal(err) |
| 194 | + } |
| 195 | + |
| 196 | + // Verify response structure |
| 197 | + for _, msg := range result { |
| 198 | + if msg.ID == "" { |
| 199 | + t.Error("message ID is empty") |
| 200 | + } |
| 201 | + if msg.DeviceID == "" { |
| 202 | + t.Error("device ID is empty") |
| 203 | + } |
| 204 | + } |
| 205 | + }, |
| 206 | + }, |
| 207 | + { |
| 208 | + name: "Invalid date format", |
| 209 | + setup: func() { |
| 210 | + // Test data is populated by the test infrastructure |
| 211 | + }, |
| 212 | + expectedStatusCode: 400, |
| 213 | + request: func() *resty.Request { |
| 214 | + return authorizedClient.R(). |
| 215 | + SetQueryParam("from", "invalid") |
| 216 | + }, |
| 217 | + validate: func(t *testing.T, response *resty.Response) { |
| 218 | + if response.StatusCode() != 400 { |
| 219 | + t.Fatal(response.StatusCode(), response.String()) |
| 220 | + } |
| 221 | + |
| 222 | + var err errorResponse |
| 223 | + if err := json.Unmarshal(response.Body(), &err); err != nil { |
| 224 | + t.Fatal(err) |
| 225 | + } |
| 226 | + |
| 227 | + if err.Message == "" { |
| 228 | + t.Error("expected error message in response") |
| 229 | + } |
| 230 | + }, |
| 231 | + }, |
| 232 | + { |
| 233 | + name: "Invalid state value", |
| 234 | + setup: func() { |
| 235 | + // Test data is populated by the test infrastructure |
| 236 | + }, |
| 237 | + expectedStatusCode: 400, |
| 238 | + request: func() *resty.Request { |
| 239 | + return authorizedClient.R(). |
| 240 | + SetQueryParam("state", "InvalidState") |
| 241 | + }, |
| 242 | + validate: func(t *testing.T, response *resty.Response) { |
| 243 | + if response.StatusCode() != 400 { |
| 244 | + t.Fatal(response.StatusCode(), response.String()) |
| 245 | + } |
| 246 | + |
| 247 | + var err errorResponse |
| 248 | + if err := json.Unmarshal(response.Body(), &err); err != nil { |
| 249 | + t.Fatal(err) |
| 250 | + } |
| 251 | + |
| 252 | + if err.Message == "" { |
| 253 | + t.Error("expected error message in response") |
| 254 | + } |
| 255 | + }, |
| 256 | + }, |
| 257 | + { |
| 258 | + name: "Invalid device ID length", |
| 259 | + setup: func() { |
| 260 | + // Test data is populated by the test infrastructure |
| 261 | + }, |
| 262 | + expectedStatusCode: 400, |
| 263 | + request: func() *resty.Request { |
| 264 | + return authorizedClient.R(). |
| 265 | + SetQueryParam("deviceId", "invalid_length_device_id") |
| 266 | + }, |
| 267 | + validate: func(t *testing.T, response *resty.Response) { |
| 268 | + if response.StatusCode() != 400 { |
| 269 | + t.Fatal(response.StatusCode(), response.String()) |
| 270 | + } |
| 271 | + |
| 272 | + var err errorResponse |
| 273 | + if err := json.Unmarshal(response.Body(), &err); err != nil { |
| 274 | + t.Fatal(err) |
| 275 | + } |
| 276 | + |
| 277 | + if err.Message == "" { |
| 278 | + t.Error("expected error message in response") |
| 279 | + } |
| 280 | + }, |
| 281 | + }, |
| 282 | + { |
| 283 | + name: "Missing authentication", |
| 284 | + setup: func() { |
| 285 | + // Test data is populated by the test infrastructure |
| 286 | + }, |
| 287 | + expectedStatusCode: 401, |
| 288 | + request: func() *resty.Request { |
| 289 | + return publicUserClient.R() |
| 290 | + }, |
| 291 | + validate: func(t *testing.T, response *resty.Response) { |
| 292 | + if response.StatusCode() != 401 { |
| 293 | + t.Fatal(response.StatusCode(), response.String()) |
| 294 | + } |
| 295 | + |
| 296 | + var err errorResponse |
| 297 | + if err := json.Unmarshal(response.Body(), &err); err != nil { |
| 298 | + t.Fatal(err) |
| 299 | + } |
| 300 | + |
| 301 | + if err.Message == "" { |
| 302 | + t.Error("expected error message in response") |
| 303 | + } |
| 304 | + }, |
| 305 | + }, |
| 306 | + { |
| 307 | + name: "Invalid credentials", |
| 308 | + setup: func() { |
| 309 | + // Test data is populated by the test infrastructure |
| 310 | + }, |
| 311 | + expectedStatusCode: 401, |
| 312 | + request: func() *resty.Request { |
| 313 | + return publicUserClient.R().SetBasicAuth("invalid", "credentials") |
| 314 | + }, |
| 315 | + validate: func(t *testing.T, response *resty.Response) { |
| 316 | + if response.StatusCode() != 401 { |
| 317 | + t.Fatal(response.StatusCode(), response.String()) |
| 318 | + } |
| 319 | + |
| 320 | + var err errorResponse |
| 321 | + if err := json.Unmarshal(response.Body(), &err); err != nil { |
| 322 | + t.Fatal(err) |
| 323 | + } |
| 324 | + |
| 325 | + if err.Message == "" { |
| 326 | + t.Error("expected error message in response") |
| 327 | + } |
| 328 | + }, |
| 329 | + }, |
| 330 | + } |
| 331 | + |
| 332 | + for _, c := range cases { |
| 333 | + t.Run(c.name, func(t *testing.T) { |
| 334 | + c.setup() |
| 335 | + |
| 336 | + res, err := c.request().Get("messages") |
| 337 | + if err != nil { |
| 338 | + t.Fatal(err) |
| 339 | + } |
| 340 | + |
| 341 | + if res.StatusCode() != c.expectedStatusCode { |
| 342 | + t.Fatal(res.StatusCode(), res.String()) |
| 343 | + } |
| 344 | + |
| 345 | + if c.validate != nil { |
| 346 | + c.validate(t, res) |
| 347 | + } |
| 348 | + }) |
| 349 | + } |
| 350 | +} |
0 commit comments