|
| 1 | +package httpsms |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "net/http" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/NdoleStudio/httpsms-go/internal/helpers" |
| 10 | + "github.com/NdoleStudio/httpsms-go/internal/stubs" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | +) |
| 13 | + |
| 14 | +func TestPhoneService_Upsert(t *testing.T) { |
| 15 | + // Setup |
| 16 | + t.Parallel() |
| 17 | + |
| 18 | + // Arrange |
| 19 | + apiKey := "test-api-key" |
| 20 | + server := helpers.MakeTestServer(http.StatusOK, stubs.PhoneUpsertResponse()) |
| 21 | + client := New(WithBaseURL(server.URL), WithAPIKey(apiKey)) |
| 22 | + |
| 23 | + // Act |
| 24 | + phone, response, err := client.Phones.Upsert(context.Background(), &PhoneUpsertParams{ |
| 25 | + PhoneNumber: "+18005550199", |
| 26 | + FcmToken: "test-fcm-token", |
| 27 | + MessagesPerMinute: 5, |
| 28 | + MaxSendAttempts: 3, |
| 29 | + MessageExpirationSeconds: 3600, |
| 30 | + SIM: "SIM1", |
| 31 | + }) |
| 32 | + |
| 33 | + // Assert |
| 34 | + assert.Nil(t, err) |
| 35 | + assert.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode) |
| 36 | + |
| 37 | + jsonContent, _ := json.Marshal(phone) |
| 38 | + assert.JSONEq(t, string(stubs.PhoneUpsertResponse()), string(jsonContent)) |
| 39 | + |
| 40 | + // Teardown |
| 41 | + server.Close() |
| 42 | +} |
| 43 | + |
| 44 | +func TestPhoneService_UpsertWithError(t *testing.T) { |
| 45 | + // Setup |
| 46 | + t.Parallel() |
| 47 | + |
| 48 | + // Arrange |
| 49 | + apiKey := "test-api-key" |
| 50 | + server := helpers.MakeTestServer(http.StatusInternalServerError, stubs.HttpInternalServerErrorResponse()) |
| 51 | + client := New(WithBaseURL(server.URL), WithAPIKey(apiKey)) |
| 52 | + |
| 53 | + // Act |
| 54 | + _, response, err := client.Phones.Upsert(context.Background(), &PhoneUpsertParams{ |
| 55 | + PhoneNumber: "+18005550199", |
| 56 | + }) |
| 57 | + |
| 58 | + // Assert |
| 59 | + assert.NotNil(t, err) |
| 60 | + assert.Equal(t, http.StatusInternalServerError, response.HTTPResponse.StatusCode) |
| 61 | + assert.Equal(t, string(stubs.HttpInternalServerErrorResponse()), string(*response.Body)) |
| 62 | + |
| 63 | + // Teardown |
| 64 | + server.Close() |
| 65 | +} |
| 66 | + |
| 67 | +func TestPhoneService_UpsertRequest(t *testing.T) { |
| 68 | + // Setup |
| 69 | + t.Parallel() |
| 70 | + |
| 71 | + // Arrange |
| 72 | + apiKey := "test-api-key" |
| 73 | + var capturedRequest http.Request |
| 74 | + server := helpers.MakeRequestCapturingTestServer(http.StatusOK, stubs.PhoneUpsertResponse(), &capturedRequest) |
| 75 | + client := New(WithBaseURL(server.URL), WithAPIKey(apiKey)) |
| 76 | + |
| 77 | + // Act |
| 78 | + _, _, err := client.Phones.Upsert(context.Background(), &PhoneUpsertParams{ |
| 79 | + PhoneNumber: "+18005550199", |
| 80 | + FcmToken: "test-fcm-token", |
| 81 | + MessagesPerMinute: 5, |
| 82 | + }) |
| 83 | + |
| 84 | + // Assert |
| 85 | + assert.Nil(t, err) |
| 86 | + assert.Equal(t, http.MethodPut, capturedRequest.Method) |
| 87 | + assert.Equal(t, "/v1/phones", capturedRequest.URL.Path) |
| 88 | + assert.Equal(t, "application/json", capturedRequest.Header.Get("Content-Type")) |
| 89 | + assert.Equal(t, apiKey, capturedRequest.Header.Get("x-api-key")) |
| 90 | + |
| 91 | + // Teardown |
| 92 | + server.Close() |
| 93 | +} |
| 94 | + |
| 95 | +func TestPhoneService_UpsertFCMToken(t *testing.T) { |
| 96 | + // Setup |
| 97 | + t.Parallel() |
| 98 | + |
| 99 | + // Arrange |
| 100 | + apiKey := "test-api-key" |
| 101 | + server := helpers.MakeTestServer(http.StatusOK, stubs.PhoneUpsertFCMTokenResponse()) |
| 102 | + client := New(WithBaseURL(server.URL), WithAPIKey(apiKey)) |
| 103 | + |
| 104 | + // Act |
| 105 | + phone, response, err := client.Phones.UpsertFCMToken(context.Background(), &PhoneFCMTokenParams{ |
| 106 | + PhoneNumber: "+18005550199", |
| 107 | + FcmToken: "new-fcm-token", |
| 108 | + SIM: "SIM1", |
| 109 | + }) |
| 110 | + |
| 111 | + // Assert |
| 112 | + assert.Nil(t, err) |
| 113 | + assert.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode) |
| 114 | + |
| 115 | + jsonContent, _ := json.Marshal(phone) |
| 116 | + assert.JSONEq(t, string(stubs.PhoneUpsertFCMTokenResponse()), string(jsonContent)) |
| 117 | + |
| 118 | + // Teardown |
| 119 | + server.Close() |
| 120 | +} |
| 121 | + |
| 122 | +func TestPhoneService_UpsertFCMTokenWithError(t *testing.T) { |
| 123 | + // Setup |
| 124 | + t.Parallel() |
| 125 | + |
| 126 | + // Arrange |
| 127 | + apiKey := "test-api-key" |
| 128 | + server := helpers.MakeTestServer(http.StatusInternalServerError, stubs.HttpInternalServerErrorResponse()) |
| 129 | + client := New(WithBaseURL(server.URL), WithAPIKey(apiKey)) |
| 130 | + |
| 131 | + // Act |
| 132 | + _, response, err := client.Phones.UpsertFCMToken(context.Background(), &PhoneFCMTokenParams{ |
| 133 | + PhoneNumber: "+18005550199", |
| 134 | + FcmToken: "new-fcm-token", |
| 135 | + }) |
| 136 | + |
| 137 | + // Assert |
| 138 | + assert.NotNil(t, err) |
| 139 | + assert.Equal(t, http.StatusInternalServerError, response.HTTPResponse.StatusCode) |
| 140 | + assert.Equal(t, string(stubs.HttpInternalServerErrorResponse()), string(*response.Body)) |
| 141 | + |
| 142 | + // Teardown |
| 143 | + server.Close() |
| 144 | +} |
| 145 | + |
| 146 | +func TestPhoneService_UpsertFCMTokenRequest(t *testing.T) { |
| 147 | + // Setup |
| 148 | + t.Parallel() |
| 149 | + |
| 150 | + // Arrange |
| 151 | + apiKey := "test-api-key" |
| 152 | + var capturedRequest http.Request |
| 153 | + server := helpers.MakeRequestCapturingTestServer(http.StatusOK, stubs.PhoneUpsertFCMTokenResponse(), &capturedRequest) |
| 154 | + client := New(WithBaseURL(server.URL), WithAPIKey(apiKey)) |
| 155 | + |
| 156 | + // Act |
| 157 | + _, _, err := client.Phones.UpsertFCMToken(context.Background(), &PhoneFCMTokenParams{ |
| 158 | + PhoneNumber: "+18005550199", |
| 159 | + FcmToken: "new-fcm-token", |
| 160 | + SIM: "SIM1", |
| 161 | + }) |
| 162 | + |
| 163 | + // Assert |
| 164 | + assert.Nil(t, err) |
| 165 | + assert.Equal(t, http.MethodPut, capturedRequest.Method) |
| 166 | + assert.Equal(t, "/v1/phones/fcm-token", capturedRequest.URL.Path) |
| 167 | + assert.Equal(t, "application/json", capturedRequest.Header.Get("Content-Type")) |
| 168 | + assert.Equal(t, apiKey, capturedRequest.Header.Get("x-api-key")) |
| 169 | + |
| 170 | + // Teardown |
| 171 | + server.Close() |
| 172 | +} |
0 commit comments