1
|
|
|
package handlers |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"context" |
5
|
|
|
"slices" |
6
|
|
|
"strings" |
7
|
|
|
"testing" |
8
|
|
|
|
9
|
|
|
"github.com/NdoleStudio/httpsms/pkg/entities" |
10
|
|
|
"github.com/NdoleStudio/httpsms/pkg/requests" |
11
|
|
|
"github.com/NdoleStudio/httpsms/pkg/responses" |
12
|
|
|
"github.com/jaswdr/faker/v2" |
13
|
|
|
"github.com/stretchr/testify/assert" |
14
|
|
|
) |
15
|
|
|
|
16
|
|
|
func TestPhoneAPIKeyHandler_store(t *testing.T) { |
17
|
|
|
// Arrange |
18
|
|
|
fake := faker.New() |
19
|
|
|
payload := requests.PhoneAPIKeyStoreRequest{ |
20
|
|
|
Name: fake.RandomStringWithLength(20), |
21
|
|
|
} |
22
|
|
|
response := new(responses.PhoneAPIKeyResponse) |
23
|
|
|
|
24
|
|
|
// Act |
25
|
|
|
err := testClient(). |
26
|
|
|
Post(). |
27
|
|
|
Path("/v1/phone-api-keys"). |
28
|
|
|
BodyJSON(payload). |
29
|
|
|
ToJSON(response). |
30
|
|
|
Fetch(context.Background()) |
31
|
|
|
|
32
|
|
|
// Assert |
33
|
|
|
assert.Nil(t, err) |
34
|
|
|
assert.NotEmpty(t, response.Data.ID) |
35
|
|
|
assert.True(t, strings.HasPrefix(response.Data.APIKey, "pk_")) |
36
|
|
|
assert.Equal(t, payload.Name, response.Data.Name) |
37
|
|
|
assert.True(t, len(response.Data.PhoneNumbers) == 0) |
38
|
|
|
assert.True(t, len(response.Data.PhoneIDs) == 0) |
39
|
|
|
|
40
|
|
|
// Teardown |
41
|
|
|
_ = testClient(). |
42
|
|
|
Delete(). |
43
|
|
|
Path("/v1/phone-api-keys/" + response.Data.ID.String()). |
44
|
|
|
Fetch(context.Background()) |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
func TestPhoneAPIKeyHandler_delete(t *testing.T) { |
48
|
|
|
// Arrange |
49
|
|
|
fake := faker.New() |
50
|
|
|
payload := requests.PhoneAPIKeyStoreRequest{ |
51
|
|
|
Name: fake.RandomStringWithLength(20), |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
// Act |
55
|
|
|
response := new(responses.PhoneAPIKeyResponse) |
56
|
|
|
_ = testClient(). |
57
|
|
|
Post(). |
58
|
|
|
Path("/v1/phone-api-keys"). |
59
|
|
|
BodyJSON(payload). |
60
|
|
|
ToJSON(response). |
61
|
|
|
Fetch(context.Background()) |
62
|
|
|
|
63
|
|
|
err := testClient(). |
64
|
|
|
Delete(). |
65
|
|
|
Path("/v1/phone-api-keys/" + response.Data.ID.String()). |
66
|
|
|
Fetch(context.Background()) |
67
|
|
|
|
68
|
|
|
// Assert |
69
|
|
|
assert.Nil(t, err) |
70
|
|
|
|
71
|
|
|
keys := new(responses.PhoneAPIKeysResponse) |
72
|
|
|
_ = testClient(). |
73
|
|
|
Path("/v1/phone-api-keys"). |
74
|
|
|
ToJSON(response). |
75
|
|
|
Fetch(context.Background()) |
76
|
|
|
|
77
|
|
|
assert.Equal(t, -1, slices.IndexFunc(keys.Data, func(key *entities.PhoneAPIKey) bool { |
78
|
|
|
return key.ID == response.Data.ID |
79
|
|
|
})) |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
func TestPhoneAPIKeyHandler_index(t *testing.T) { |
83
|
|
|
// Arrange |
84
|
|
|
fake := faker.New() |
85
|
|
|
createResponse := new(responses.PhoneAPIKeyResponse) |
86
|
|
|
response := new(responses.PhoneAPIKeysResponse) |
87
|
|
|
payload := requests.PhoneAPIKeyStoreRequest{ |
88
|
|
|
Name: fake.RandomStringWithLength(20), |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
// Act |
92
|
|
|
_ = testClient(). |
93
|
|
|
Post(). |
94
|
|
|
Path("/v1/phone-api-keys"). |
95
|
|
|
BodyJSON(payload). |
96
|
|
|
ToJSON(createResponse). |
97
|
|
|
Fetch(context.Background()) |
98
|
|
|
|
99
|
|
|
err := testClient(). |
100
|
|
|
Path("/v1/phone-api-keys"). |
101
|
|
|
ToJSON(response). |
102
|
|
|
Fetch(context.Background()) |
103
|
|
|
|
104
|
|
|
// Assert |
105
|
|
|
assert.Nil(t, err) |
106
|
|
|
assert.NotEmpty(t, response.Data) |
107
|
|
|
assert.NotEqual(t, -1, slices.IndexFunc(response.Data, func(key *entities.PhoneAPIKey) bool { |
108
|
|
|
return key.ID == createResponse.Data.ID |
109
|
|
|
})) |
110
|
|
|
|
111
|
|
|
// Teardown |
112
|
|
|
_ = testClient(). |
113
|
|
|
Delete(). |
114
|
|
|
Path("/v1/phone-api-keys/" + createResponse.Data.ID.String()). |
115
|
|
|
Fetch(context.Background()) |
116
|
|
|
} |
117
|
|
|
|