Skip to content

Commit 2830472

Browse files
AchoArnoldCopilot
andcommitted
test: add tests for PhoneService, PhoneAPIKeyService, and WebhookService
Add success, error, and request verification tests for all new service methods introduced in this branch: - PhoneService.Upsert and PhoneService.UpsertFCMToken - PhoneAPIKeyService.Store - WebhookService.Store Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent f68ae42 commit 2830472

6 files changed

Lines changed: 441 additions & 0 deletions

File tree

internal/stubs/phone.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package stubs
2+
3+
// PhoneUpsertResponse response from the PUT /v1/phones endpoint
4+
func PhoneUpsertResponse() []byte {
5+
return []byte(`
6+
{
7+
"data": {
8+
"id": "9d484671-cac2-41de-9171-d9d2c1835d7b",
9+
"user_id": "hT5V2CmN5bbG81glMLmosxPV9Np2",
10+
"phone_number": "+18005550199",
11+
"fcm_token": "test-fcm-token",
12+
"messages_per_minute": 5,
13+
"max_send_attempts": 3,
14+
"message_expiration_seconds": 3600,
15+
"sim": "SIM1",
16+
"created_at": "2024-01-21T13:07:56.203538Z",
17+
"updated_at": "2024-01-21T13:07:56.203538Z"
18+
},
19+
"message": "phone upserted successfully",
20+
"status": "success"
21+
}
22+
`)
23+
}
24+
25+
// PhoneUpsertFCMTokenResponse response from the PUT /v1/phones/fcm-token endpoint
26+
func PhoneUpsertFCMTokenResponse() []byte {
27+
return []byte(`
28+
{
29+
"data": {
30+
"id": "9d484671-cac2-41de-9171-d9d2c1835d7b",
31+
"user_id": "hT5V2CmN5bbG81glMLmosxPV9Np2",
32+
"phone_number": "+18005550199",
33+
"fcm_token": "new-fcm-token",
34+
"messages_per_minute": 5,
35+
"max_send_attempts": 3,
36+
"message_expiration_seconds": 3600,
37+
"sim": "SIM1",
38+
"created_at": "2024-01-21T13:07:56.203538Z",
39+
"updated_at": "2024-01-21T13:07:56.203538Z"
40+
},
41+
"message": "phone FCM token updated successfully",
42+
"status": "success"
43+
}
44+
`)
45+
}

internal/stubs/phone_api_key.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package stubs
2+
3+
// PhoneAPIKeyStoreResponse response from the POST /v1/phone-api-keys/ endpoint
4+
func PhoneAPIKeyStoreResponse() []byte {
5+
return []byte(`
6+
{
7+
"data": {
8+
"id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
9+
"name": "test-key",
10+
"user_id": "hT5V2CmN5bbG81glMLmosxPV9Np2",
11+
"user_email": "test@example.com",
12+
"phone_numbers": ["+18005550199"],
13+
"phone_ids": ["9d484671-cac2-41de-9171-d9d2c1835d7b"],
14+
"api_key": "pk_test_1234567890abcdef",
15+
"created_at": "2024-01-21T13:07:56.203538Z",
16+
"updated_at": "2024-01-21T13:07:56.203538Z"
17+
},
18+
"message": "phone API key created successfully",
19+
"status": "success"
20+
}
21+
`)
22+
}

internal/stubs/webhook.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package stubs
2+
3+
// WebhookStoreResponse response from the POST /v1/webhooks endpoint
4+
func WebhookStoreResponse() []byte {
5+
return []byte(`
6+
{
7+
"data": {
8+
"id": "b2c3d4e5-f6a7-8901-bcde-f12345678901",
9+
"user_id": "hT5V2CmN5bbG81glMLmosxPV9Np2",
10+
"url": "https://example.com/webhook",
11+
"signing_key": "whsec_test_signing_key",
12+
"phone_numbers": ["+18005550199"],
13+
"events": ["message.received", "message.sent"],
14+
"created_at": "2024-01-21T13:07:56.203538Z",
15+
"updated_at": "2024-01-21T13:07:56.203538Z"
16+
},
17+
"message": "webhook created successfully",
18+
"status": "success"
19+
}
20+
`)
21+
}

phone_api_key_service_test.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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 TestPhoneAPIKeyService_Store(t *testing.T) {
15+
// Setup
16+
t.Parallel()
17+
18+
// Arrange
19+
apiKey := "test-api-key"
20+
server := helpers.MakeTestServer(http.StatusOK, stubs.PhoneAPIKeyStoreResponse())
21+
client := New(WithBaseURL(server.URL), WithAPIKey(apiKey))
22+
23+
// Act
24+
phoneAPIKey, response, err := client.PhoneAPIKeys.Store(context.Background(), &PhoneAPIKeyStoreParams{
25+
Name: "test-key",
26+
})
27+
28+
// Assert
29+
assert.Nil(t, err)
30+
assert.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode)
31+
32+
jsonContent, _ := json.Marshal(phoneAPIKey)
33+
assert.JSONEq(t, string(stubs.PhoneAPIKeyStoreResponse()), string(jsonContent))
34+
35+
// Teardown
36+
server.Close()
37+
}
38+
39+
func TestPhoneAPIKeyService_StoreWithError(t *testing.T) {
40+
// Setup
41+
t.Parallel()
42+
43+
// Arrange
44+
apiKey := "test-api-key"
45+
server := helpers.MakeTestServer(http.StatusInternalServerError, stubs.HttpInternalServerErrorResponse())
46+
client := New(WithBaseURL(server.URL), WithAPIKey(apiKey))
47+
48+
// Act
49+
_, response, err := client.PhoneAPIKeys.Store(context.Background(), &PhoneAPIKeyStoreParams{
50+
Name: "test-key",
51+
})
52+
53+
// Assert
54+
assert.NotNil(t, err)
55+
assert.Equal(t, http.StatusInternalServerError, response.HTTPResponse.StatusCode)
56+
assert.Equal(t, string(stubs.HttpInternalServerErrorResponse()), string(*response.Body))
57+
58+
// Teardown
59+
server.Close()
60+
}
61+
62+
func TestPhoneAPIKeyService_StoreRequest(t *testing.T) {
63+
// Setup
64+
t.Parallel()
65+
66+
// Arrange
67+
apiKey := "test-api-key"
68+
var capturedRequest http.Request
69+
server := helpers.MakeRequestCapturingTestServer(http.StatusOK, stubs.PhoneAPIKeyStoreResponse(), &capturedRequest)
70+
client := New(WithBaseURL(server.URL), WithAPIKey(apiKey))
71+
72+
// Act
73+
_, _, err := client.PhoneAPIKeys.Store(context.Background(), &PhoneAPIKeyStoreParams{
74+
Name: "test-key",
75+
})
76+
77+
// Assert
78+
assert.Nil(t, err)
79+
assert.Equal(t, http.MethodPost, capturedRequest.Method)
80+
assert.Equal(t, "/v1/phone-api-keys/", capturedRequest.URL.Path)
81+
assert.Equal(t, "application/json", capturedRequest.Header.Get("Content-Type"))
82+
assert.Equal(t, apiKey, capturedRequest.Header.Get("x-api-key"))
83+
84+
// Teardown
85+
server.Close()
86+
}

phone_service_test.go

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
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

Comments
 (0)