File size: 2,767 Bytes
83a59ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
//go:build integration

package handlers

import (
	"context"
	"slices"
	"strings"
	"testing"

	"github.com/NdoleStudio/httpsms/pkg/entities"
	"github.com/NdoleStudio/httpsms/pkg/requests"
	"github.com/NdoleStudio/httpsms/pkg/responses"
	"github.com/jaswdr/faker/v2"
	"github.com/stretchr/testify/assert"
)

func TestPhoneAPIKeyHandler_store(t *testing.T) {
	// Arrange
	fake := faker.New()
	payload := requests.PhoneAPIKeyStoreRequest{
		Name: fake.RandomStringWithLength(20),
	}
	response := new(responses.PhoneAPIKeyResponse)

	// Act
	err := testClient().
		Post().
		Path("/v1/phone-api-keys").
		BodyJSON(payload).
		ToJSON(response).
		Fetch(context.Background())

	// Assert
	assert.Nil(t, err)
	assert.NotEmpty(t, response.Data.ID)
	assert.True(t, strings.HasPrefix(response.Data.APIKey, "pk_"))
	assert.Equal(t, payload.Name, response.Data.Name)
	assert.True(t, len(response.Data.PhoneNumbers) == 0)
	assert.True(t, len(response.Data.PhoneIDs) == 0)

	// Teardown
	_ = testClient().
		Delete().
		Path("/v1/phone-api-keys/" + response.Data.ID.String()).
		Fetch(context.Background())
}

func TestPhoneAPIKeyHandler_delete(t *testing.T) {
	// Arrange
	fake := faker.New()
	payload := requests.PhoneAPIKeyStoreRequest{
		Name: fake.RandomStringWithLength(20),
	}

	// Act
	response := new(responses.PhoneAPIKeyResponse)
	_ = testClient().
		Post().
		Path("/v1/phone-api-keys").
		BodyJSON(payload).
		ToJSON(response).
		Fetch(context.Background())

	err := testClient().
		Delete().
		Path("/v1/phone-api-keys/" + response.Data.ID.String()).
		Fetch(context.Background())

	// Assert
	assert.Nil(t, err)

	keys := new(responses.PhoneAPIKeysResponse)
	_ = testClient().
		Path("/v1/phone-api-keys").
		ToJSON(response).
		Fetch(context.Background())

	assert.Equal(t, -1, slices.IndexFunc(keys.Data, func(key *entities.PhoneAPIKey) bool {
		return key.ID == response.Data.ID
	}))
}

func TestPhoneAPIKeyHandler_index(t *testing.T) {
	// Arrange
	fake := faker.New()
	createResponse := new(responses.PhoneAPIKeyResponse)
	response := new(responses.PhoneAPIKeysResponse)
	payload := requests.PhoneAPIKeyStoreRequest{
		Name: fake.RandomStringWithLength(20),
	}

	// Act
	_ = testClient().
		Post().
		Path("/v1/phone-api-keys").
		BodyJSON(payload).
		ToJSON(createResponse).
		Fetch(context.Background())

	err := testClient().
		Path("/v1/phone-api-keys").
		ToJSON(response).
		Fetch(context.Background())

	// Assert
	assert.Nil(t, err)
	assert.NotEmpty(t, response.Data)
	assert.NotEqual(t, -1, slices.IndexFunc(response.Data, func(key *entities.PhoneAPIKey) bool {
		return key.ID == createResponse.Data.ID
	}))

	// Teardown
	_ = testClient().
		Delete().
		Path("/v1/phone-api-keys/" + createResponse.Data.ID.String()).
		Fetch(context.Background())
}