Skip to content

Commit 53c8670

Browse files
committed
Added tests for the phone api key endpoint
1 parent a9dc66b commit 53c8670

File tree

15 files changed

+175
-42
lines changed

15 files changed

+175
-42
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
[![Contributor Covenant](https://img.shields.io/badge/Contributor%20Covenant-2.1-4baaaa.svg)](CODE_OF_CONDUCT.md)
77
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/NdoleStudio/httpsms/badges/quality-score.png?b=main)](https://scrutinizer-ci.com/g/NdoleStudio/httpsms/?branch=main)
88
[![Better Stack Badge](https://uptime.betterstack.com/status-badges/v1/monitor/ldl5.svg)](https://uptime.betterstack.com/?utm_source=status_badge)
9+
[![Sponsors](https://img.shields.io/github/sponsors/ndolestudio?logo=github)](https://github.com/sponsors/ndolestudio)
910
[![Discord](https://img.shields.io/discord/1095778291488653372?label=Discord)](https://discord.gg/kGk8HVqeEZ)
1011

1112
[httpSMS](https://httpsms.com) is a service that lets you use your Android phone as an SMS Gateway to send and receive SMS messages.

api/docs/docs.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ const docTemplate = `{
4545
{
4646
"minimum": 0,
4747
"type": "integer",
48-
"description": "number of heartbeats to skip",
48+
"description": "number of api keys to skip",
4949
"name": "skip",
5050
"in": "query"
5151
},
@@ -59,7 +59,7 @@ const docTemplate = `{
5959
"maximum": 100,
6060
"minimum": 1,
6161
"type": "integer",
62-
"description": "number of phone api keys to return",
62+
"description": "number of api keys to return",
6363
"name": "limit",
6464
"in": "query"
6565
}
@@ -113,7 +113,7 @@ const docTemplate = `{
113113
"tags": [
114114
"PhoneAPIKeys"
115115
],
116-
"summary": "Store phone API key",
116+
"summary": "store phone API key",
117117
"parameters": [
118118
{
119119
"description": "Payload of new phone API key.",
@@ -3224,7 +3224,7 @@ const docTemplate = `{
32243224
"properties": {
32253225
"api_key": {
32263226
"type": "string",
3227-
"example": "pk_DGW8NwQp7mxKaSZ72Xq9v67SLqSbWQvckzzmK8D6rvd7NywSEkdMJtuxKyEkYnCY"
3227+
"example": "pk_DGW8NwQp7mxKaSZ72Xq9v6xxxxx"
32283228
},
32293229
"created_at": {
32303230
"type": "string",

api/docs/swagger.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
{
3434
"minimum": 0,
3535
"type": "integer",
36-
"description": "number of heartbeats to skip",
36+
"description": "number of api keys to skip",
3737
"name": "skip",
3838
"in": "query"
3939
},
@@ -47,7 +47,7 @@
4747
"maximum": 100,
4848
"minimum": 1,
4949
"type": "integer",
50-
"description": "number of phone api keys to return",
50+
"description": "number of api keys to return",
5151
"name": "limit",
5252
"in": "query"
5353
}
@@ -95,7 +95,7 @@
9595
"consumes": ["application/json"],
9696
"produces": ["application/json"],
9797
"tags": ["PhoneAPIKeys"],
98-
"summary": "Store phone API key",
98+
"summary": "store phone API key",
9999
"parameters": [
100100
{
101101
"description": "Payload of new phone API key.",
@@ -2964,7 +2964,7 @@
29642964
"properties": {
29652965
"api_key": {
29662966
"type": "string",
2967-
"example": "pk_DGW8NwQp7mxKaSZ72Xq9v67SLqSbWQvckzzmK8D6rvd7NywSEkdMJtuxKyEkYnCY"
2967+
"example": "pk_DGW8NwQp7mxKaSZ72Xq9v6xxxxx"
29682968
},
29692969
"created_at": {
29702970
"type": "string",

api/docs/swagger.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ definitions:
328328
entities.PhoneAPIKey:
329329
properties:
330330
api_key:
331-
example: pk_DGW8NwQp7mxKaSZ72Xq9v67SLqSbWQvckzzmK8D6rvd7NywSEkdMJtuxKyEkYnCY
331+
example: pk_DGW8NwQp7mxKaSZ72Xq9v6xxxxx
332332
type: string
333333
created_at:
334334
example: "2022-06-05T14:26:02.302718+03:00"
@@ -1203,7 +1203,7 @@ paths:
12031203
Get list phone API keys which a user has registered on the httpSMS
12041204
application
12051205
parameters:
1206-
- description: number of heartbeats to skip
1206+
- description: number of api keys to skip
12071207
in: query
12081208
minimum: 0
12091209
name: skip
@@ -1212,7 +1212,7 @@ paths:
12121212
in: query
12131213
name: query
12141214
type: string
1215-
- description: number of phone api keys to return
1215+
- description: number of api keys to return
12161216
in: query
12171217
maximum: 100
12181218
minimum: 1
@@ -1284,7 +1284,7 @@ paths:
12841284
$ref: "#/definitions/responses.InternalServerError"
12851285
security:
12861286
- ApiKeyAuth: []
1287-
summary: Store phone API key
1287+
summary: store phone API key
12881288
tags:
12891289
- PhoneAPIKeys
12901290
/api-keys/{phoneAPIKeyID}:

api/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ require (
2525
github.com/google/uuid v1.6.0
2626
github.com/hashicorp/go-retryablehttp v0.7.7
2727
github.com/hirosassa/zerodriver v0.1.4
28+
github.com/jaswdr/faker/v2 v2.3.3
2829
github.com/jinzhu/now v1.1.5
2930
github.com/joho/godotenv v1.5.1
3031
github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible

api/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,8 @@ github.com/jackc/pgx/v5 v5.7.2 h1:mLoDLV6sonKlvjIEsV56SkWNCnuNv531l94GaIzO+XI=
176176
github.com/jackc/pgx/v5 v5.7.2/go.mod h1:ncY89UGWxg82EykZUwSpUKEfccBGGYq1xjrOpsbsfGQ=
177177
github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo=
178178
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
179+
github.com/jaswdr/faker/v2 v2.3.3 h1:0mA+B5YGjqgpOPdDY/72d6pDv7Z/5t6F1XzIfkUfgC4=
180+
github.com/jaswdr/faker/v2 v2.3.3/go.mod h1:ROK8xwQV0hYOLDUtxCQgHGcl10jbVzIvqHxcIDdwY2Q=
179181
github.com/jaytaylor/html2text v0.0.0-20180606194806-57d518f124b0/go.mod h1:CVKlgaMiht+LXvHG173ujK6JUhZXKb2u/BQtjPDIvyk=
180182
github.com/jaytaylor/html2text v0.0.0-20230321000545-74c2419ad056 h1:iCHtR9CQyktQ5+f3dMVZfwD2KWJUgm7M0gdL9NGr8KA=
181183
github.com/jaytaylor/html2text v0.0.0-20230321000545-74c2419ad056/go.mod h1:CVKlgaMiht+LXvHG173ujK6JUhZXKb2u/BQtjPDIvyk=

api/pkg/di/container.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,7 @@ func (container *Container) PhoneAPIKeyRepository() (repository repositories.Pho
699699
container.Logger(),
700700
container.Tracer(),
701701
container.DB(),
702+
container.UserRistrettoCache(),
702703
)
703704
}
704705

api/pkg/entities/phone_api_key.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type PhoneAPIKey struct {
1515
UserEmail string `json:"user_email" example:"[email protected]"`
1616
PhoneNumbers pq.StringArray `json:"phone_numbers" example:"[+18005550199,+18005550100]" gorm:"type:text[]" swaggertype:"array,string"`
1717
PhoneIDs pq.StringArray `json:"phone_ids" example:"[32343a19-da5e-4b1b-a767-3298a73703cb,32343a19-da5e-4b1b-a767-3298a73703cc]" gorm:"type:text[]" swaggertype:"array,string"`
18-
APIKey string `json:"api_key" example:"pk_DGW8NwQp7mxKaSZ72Xq9v67SLqSbWQvckzzmK8D6rvd7NywSEkdMJtuxKyEkYnCY"`
18+
APIKey string `json:"api_key" gorm:"uniqueIndex:idx_phone_api_key__api_key" example:"pk_DGW8NwQp7mxKaSZ72Xq9v6xxxxx"`
1919
CreatedAt time.Time `json:"created_at" example:"2022-06-05T14:26:02.302718+03:00"`
2020
UpdatedAt time.Time `json:"updated_at" example:"2022-06-05T14:26:02.302718+03:00"`
2121
}

api/pkg/handlers/handler_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package handlers
2+
3+
import (
4+
"os"
5+
6+
"github.com/carlmjohnson/requests"
7+
_ "github.com/joho/godotenv/autoload" // import USER_API_KEY from .env file
8+
)
9+
10+
func testClient() *requests.Builder {
11+
return requests.URL("http://localhost:8000").
12+
Header("x-api-key", os.Getenv("USER_API_KEY"))
13+
}

api/pkg/handlers/phone_api_key_handler.go

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,30 +40,29 @@ func NewPhoneAPIKeyHandler(
4040

4141
// RegisterRoutes registers the routes for the PhoneAPIKeyHandler
4242
func (h *PhoneAPIKeyHandler) RegisterRoutes(app *fiber.App, middlewares ...fiber.Handler) {
43-
router := app.Group("/v1/api-keys/")
44-
router.Get("/", h.computeRoute(middlewares, h.Index)...)
45-
router.Post("/", h.computeRoute(middlewares, h.Store)...)
46-
router.Delete("/:phoneAPIKeyID", h.computeRoute(middlewares, h.Delete)...)
47-
router.Delete("/:phoneAPIKeyID/phones/:phoneID", h.computeRoute(middlewares, h.DeletePhone)...)
43+
router := app.Group("/v1/phone-api-keys/")
44+
router.Get("/", h.computeRoute(middlewares, h.index)...)
45+
router.Post("/", h.computeRoute(middlewares, h.store)...)
46+
router.Delete("/:phoneAPIKeyID", h.computeRoute(middlewares, h.delete)...)
47+
router.Delete("/:phoneAPIKeyID/phones/:phoneID", h.computeRoute(middlewares, h.deletePhone)...)
4848
}
4949

50-
// Index returns the phone API Keys of a user
5150
// @Summary Get the phone API keys of a user
5251
// @Description Get list phone API keys which a user has registered on the httpSMS application
5352
// @Security ApiKeyAuth
5453
// @Tags PhoneAPIKeys
5554
// @Accept json
5655
// @Produce json
57-
// @Param skip query int false "number of heartbeats to skip" minimum(0)
58-
// @Param query query string false "filter api keys with name containing query"
59-
// @Param limit query int false "number of phone api keys to return" minimum(1) maximum(100)
56+
// @Param skip query int false "number of phone api keys to skip" minimum(0)
57+
// @Param query query string false "filter phone api keys with name containing query"
58+
// @Param limit query int false "number of phone api keys to return" minimum(1) maximum(100)
6059
// @Success 200 {object} responses.PhoneAPIKeysResponse
6160
// @Failure 400 {object} responses.BadRequest
6261
// @Failure 401 {object} responses.Unauthorized
6362
// @Failure 422 {object} responses.UnprocessableEntity
6463
// @Failure 500 {object} responses.InternalServerError
6564
// @Router /api-keys [get]
66-
func (h *PhoneAPIKeyHandler) Index(c *fiber.Ctx) error {
65+
func (h *PhoneAPIKeyHandler) index(c *fiber.Ctx) error {
6766
ctx, span, ctxLogger := h.tracer.StartFromFiberCtxWithLogger(c, h.logger)
6867
defer span.End()
6968

@@ -90,7 +89,6 @@ func (h *PhoneAPIKeyHandler) Index(c *fiber.Ctx) error {
9089
return h.responseOK(c, fmt.Sprintf("fetched %d phone API %s", len(apiKeys), h.pluralize("key", len(apiKeys))), apiKeys)
9190
}
9291

93-
// Store a new Phone API key
9492
// @Summary Store phone API key
9593
// @Description Creates a new phone API key which can be used to log in to the httpSMS app on your Android phone
9694
// @Security ApiKeyAuth
@@ -104,7 +102,7 @@ func (h *PhoneAPIKeyHandler) Index(c *fiber.Ctx) error {
104102
// @Failure 422 {object} responses.UnprocessableEntity
105103
// @Failure 500 {object} responses.InternalServerError
106104
// @Router /api-keys [post]
107-
func (h *PhoneAPIKeyHandler) Store(c *fiber.Ctx) error {
105+
func (h *PhoneAPIKeyHandler) store(c *fiber.Ctx) error {
108106
ctx, span, ctxLogger := h.tracer.StartFromFiberCtxWithLogger(c, h.logger)
109107
defer span.End()
110108

@@ -121,32 +119,31 @@ func (h *PhoneAPIKeyHandler) Store(c *fiber.Ctx) error {
121119
return h.responseUnprocessableEntity(c, errors, "validation errors while updating phones")
122120
}
123121

124-
phone, err := h.service.Create(ctx, h.userFromContext(c), request.Name)
122+
phoneAPIKey, err := h.service.Create(ctx, h.userFromContext(c), request.Name)
125123
if err != nil {
126124
msg := fmt.Sprintf("cannot update phones with params [%+#v]", request)
127125
ctxLogger.Error(stacktrace.Propagate(err, msg))
128126
return h.responseInternalServerError(c)
129127
}
130128

131-
return h.responseOK(c, "phone updated successfully", phone)
129+
return h.responseOK(c, "phone API key created successfully", phoneAPIKey)
132130
}
133131

134-
// Delete a phone API Key
135132
// @Summary Delete a phone API key from the database.
136133
// @Description Delete a phone API Key from the database and cannot be used for authentication anymore.
137134
// @Security ApiKeyAuth
138135
// @Tags PhoneAPIKeys
139136
// @Accept json
140137
// @Produce json
141-
// @Param phoneAPIKeyID path string true "ID of the phone API key" default(32343a19-da5e-4b1b-a767-3298a73703ca)
138+
// @Param phoneAPIKeyID path string true "ID of the phone API key" default(32343a19-da5e-4b1b-a767-3298a73703ca)
142139
// @Success 204 {object} responses.NoContent
143140
// @Failure 400 {object} responses.BadRequest
144141
// @Failure 401 {object} responses.Unauthorized
145142
// @Failure 404 {object} responses.NotFound
146143
// @Failure 422 {object} responses.UnprocessableEntity
147144
// @Failure 500 {object} responses.InternalServerError
148145
// @Router /api-keys/{phoneAPIKeyID} [delete]
149-
func (h *PhoneAPIKeyHandler) Delete(c *fiber.Ctx) error {
146+
func (h *PhoneAPIKeyHandler) delete(c *fiber.Ctx) error {
150147
ctx, span, ctxLogger := h.tracer.StartFromFiberCtxWithLogger(c, h.logger)
151148
defer span.End()
152149

@@ -171,7 +168,6 @@ func (h *PhoneAPIKeyHandler) Delete(c *fiber.Ctx) error {
171168
return h.responseNoContent(c, "phone API key deleted successfully")
172169
}
173170

174-
// DeletePhone removes a phone from a phone API key
175171
// @Summary Remove the association of a phone from the phone API key.
176172
// @Description You will need to login again to the httpSMS app on your Android phone with a new phone API key.
177173
// @Security ApiKeyAuth
@@ -187,7 +183,7 @@ func (h *PhoneAPIKeyHandler) Delete(c *fiber.Ctx) error {
187183
// @Failure 422 {object} responses.UnprocessableEntity
188184
// @Failure 500 {object} responses.InternalServerError
189185
// @Router /api-keys/{phoneAPIKeyID}/phones/{phoneID} [delete]
190-
func (h *PhoneAPIKeyHandler) DeletePhone(c *fiber.Ctx) error {
186+
func (h *PhoneAPIKeyHandler) deletePhone(c *fiber.Ctx) error {
191187
ctx, span, ctxLogger := h.tracer.StartFromFiberCtxWithLogger(c, h.logger)
192188
defer span.End()
193189

0 commit comments

Comments
 (0)