|
| 1 | +package httpsms |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "github.com/google/uuid" |
| 7 | + "net/http" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/NdoleStudio/httpsms-go/internal/helpers" |
| 11 | + "github.com/NdoleStudio/httpsms-go/internal/stubs" |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | +) |
| 14 | + |
| 15 | +func TestMessageThreadService_Index(t *testing.T) { |
| 16 | + // Setup |
| 17 | + t.Parallel() |
| 18 | + |
| 19 | + // Arrange |
| 20 | + apiKey := "test-api-key" |
| 21 | + server := helpers.MakeTestServer(http.StatusOK, stubs.MessageThreadIndexResponse()) |
| 22 | + client := New(WithBaseURL(server.URL), WithAPIKey(apiKey)) |
| 23 | + |
| 24 | + indexParams := &MessageThreadIndexParams{ |
| 25 | + IsArchived: false, |
| 26 | + Skip: 0, |
| 27 | + Query: nil, |
| 28 | + Limit: 10, |
| 29 | + Owner: fromNumber, |
| 30 | + } |
| 31 | + |
| 32 | + // Act |
| 33 | + threads, response, err := client.MessageThreads.Index(context.Background(), indexParams) |
| 34 | + |
| 35 | + // Assert |
| 36 | + assert.Nil(t, err) |
| 37 | + |
| 38 | + assert.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode) |
| 39 | + |
| 40 | + jsonContent, _ := json.Marshal(threads) |
| 41 | + assert.JSONEq(t, string(stubs.MessageThreadIndexResponse()), string(jsonContent)) |
| 42 | + |
| 43 | + // Teardown |
| 44 | + server.Close() |
| 45 | +} |
| 46 | + |
| 47 | +func TestMessageThreadService_IndexWithError(t *testing.T) { |
| 48 | + // Setup |
| 49 | + t.Parallel() |
| 50 | + |
| 51 | + // Arrange |
| 52 | + apiKey := "test-api-key" |
| 53 | + server := helpers.MakeTestServer(http.StatusInternalServerError, stubs.HttpInternalServerErrorResponse()) |
| 54 | + client := New(WithBaseURL(server.URL), WithAPIKey(apiKey)) |
| 55 | + |
| 56 | + // Act |
| 57 | + _, response, err := client.MessageThreads.Index(context.Background(), &MessageThreadIndexParams{}) |
| 58 | + |
| 59 | + // Assert |
| 60 | + assert.NotNil(t, err) |
| 61 | + assert.Equal(t, http.StatusInternalServerError, response.HTTPResponse.StatusCode) |
| 62 | + assert.Equal(t, string(stubs.HttpInternalServerErrorResponse()), string(*response.Body)) |
| 63 | + |
| 64 | + // Teardown |
| 65 | + server.Close() |
| 66 | +} |
| 67 | + |
| 68 | +func TestMessageThreadService_Delete(t *testing.T) { |
| 69 | + // Setup |
| 70 | + t.Parallel() |
| 71 | + |
| 72 | + // Arrange |
| 73 | + apiKey := "test-api-key" |
| 74 | + server := helpers.MakeTestServer(http.StatusOK, nil) |
| 75 | + client := New(WithBaseURL(server.URL), WithAPIKey(apiKey)) |
| 76 | + |
| 77 | + // Act |
| 78 | + response, err := client.MessageThreads.Delete(context.Background(), uuid.New()) |
| 79 | + |
| 80 | + // Assert |
| 81 | + assert.Nil(t, err) |
| 82 | + assert.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode) |
| 83 | + |
| 84 | + // Teardown |
| 85 | + server.Close() |
| 86 | +} |
| 87 | + |
| 88 | +func TestMessageThreadService_DeleteWithError(t *testing.T) { |
| 89 | + // Setup |
| 90 | + t.Parallel() |
| 91 | + |
| 92 | + // Arrange |
| 93 | + apiKey := "test-api-key" |
| 94 | + server := helpers.MakeTestServer(http.StatusInternalServerError, stubs.HttpInternalServerErrorResponse()) |
| 95 | + client := New(WithBaseURL(server.URL), WithAPIKey(apiKey)) |
| 96 | + |
| 97 | + // Act |
| 98 | + response, err := client.MessageThreads.Delete(context.Background(), uuid.New()) |
| 99 | + |
| 100 | + // Assert |
| 101 | + assert.NotNil(t, err) |
| 102 | + assert.Equal(t, http.StatusInternalServerError, response.HTTPResponse.StatusCode) |
| 103 | + |
| 104 | + // Teardown |
| 105 | + server.Close() |
| 106 | +} |
0 commit comments