From 503d6158636c5e30e447b94fb30424ee839d0383 Mon Sep 17 00:00:00 2001 From: Acho Arnold Date: Wed, 31 Jan 2024 20:52:04 +0200 Subject: [PATCH] Add ability to delete message threads --- README.md | 1 + heartbeat_service_test.go | 4 +- internal/stubs/message.go | 10 --- internal/stubs/message_thread.go | 27 ++++++++ internal/stubs/response.go | 11 ++++ message_service_test.go | 4 +- message_thread_service.go | 13 ++++ message_thread_service_test.go | 106 +++++++++++++++++++++++++++++++ 8 files changed, 162 insertions(+), 14 deletions(-) create mode 100644 internal/stubs/message_thread.go create mode 100644 internal/stubs/response.go create mode 100644 message_thread_service_test.go diff --git a/README.md b/README.md index d87b8e4..0bdefaa 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,7 @@ import "github.com/NdoleStudio/httpsms-go" - [x] `GET /v1/heartbeats`: Get the heartbeats of an Android Phone - [x] **Message Threads** - [x] `GET /v1/message-threads`: Get the message threads of a phone number + - [x] `DELETE v1/message-threads/:messageThreadID`: Delete a message thread - [x] **Cipher** - [x] `Encrypt`: Encrypt the content of a message to cipher text - [x] `Decrypt`: Decrypt an encrypted message content to plain text diff --git a/heartbeat_service_test.go b/heartbeat_service_test.go index 0bdf461..73527bb 100644 --- a/heartbeat_service_test.go +++ b/heartbeat_service_test.go @@ -45,7 +45,7 @@ func TestHeartbeatService_IndexWithError(t *testing.T) { // Arrange apiKey := "test-api-key" - server := helpers.MakeTestServer(http.StatusInternalServerError, stubs.MessagesSendErrorResponse()) + server := helpers.MakeTestServer(http.StatusInternalServerError, stubs.HttpInternalServerErrorResponse()) client := New(WithBaseURL(server.URL), WithAPIKey(apiKey)) // Act @@ -59,7 +59,7 @@ func TestHeartbeatService_IndexWithError(t *testing.T) { // Assert assert.NotNil(t, err) assert.Equal(t, http.StatusInternalServerError, response.HTTPResponse.StatusCode) - assert.Equal(t, string(stubs.MessagesSendErrorResponse()), string(*response.Body)) + assert.Equal(t, string(stubs.HttpInternalServerErrorResponse()), string(*response.Body)) // Teardown server.Close() diff --git a/internal/stubs/message.go b/internal/stubs/message.go index 868e708..a08a690 100644 --- a/internal/stubs/message.go +++ b/internal/stubs/message.go @@ -37,13 +37,3 @@ func MessagesSendResponse() []byte { } `) } - -// MessagesSendErrorResponse internal error response -func MessagesSendErrorResponse() []byte { - return []byte(` - { - "message": "We ran into an internal error while handling the request.", - "status": "error" - } -`) -} diff --git a/internal/stubs/message_thread.go b/internal/stubs/message_thread.go new file mode 100644 index 0000000..5fc2b83 --- /dev/null +++ b/internal/stubs/message_thread.go @@ -0,0 +1,27 @@ +package stubs + +// MessageThreadIndexResponse response from the /v1/message-threads endpoint +func MessageThreadIndexResponse() []byte { + return []byte(` +{ + "data": [ + { + "color": "indigo", + "contact": "+18005550100", + "created_at": "2022-06-05T14:26:09.527976+03:00", + "id": "32343a19-da5e-4b1b-a767-3298a73703ca", + "is_archived": false, + "last_message_content": "This is a sample message content", + "last_message_id": "32343a19-da5e-4b1b-a767-3298a73703ca", + "order_timestamp": "2022-06-05T14:26:09.527976+03:00", + "owner": "+18005550199", + "status": "PENDING", + "updated_at": "2022-06-05T14:26:09.527976+03:00", + "user_id": "WB7DRDWrJZRGbYrv2CKGkqbzvqdC" + } + ], + "message": "item created successfully", + "status": "success" +} +`) +} diff --git a/internal/stubs/response.go b/internal/stubs/response.go new file mode 100644 index 0000000..7967f7b --- /dev/null +++ b/internal/stubs/response.go @@ -0,0 +1,11 @@ +package stubs + +// HttpInternalServerErrorResponse internal error response +func HttpInternalServerErrorResponse() []byte { + return []byte(` + { + "message": "We ran into an internal error while handling the request.", + "status": "error" + } +`) +} diff --git a/message_service_test.go b/message_service_test.go index a4d339e..aeead23 100644 --- a/message_service_test.go +++ b/message_service_test.go @@ -53,7 +53,7 @@ func TestMessagesService_SendWithError(t *testing.T) { // Arrange apiKey := "test-api-key" - server := helpers.MakeTestServer(http.StatusInternalServerError, stubs.MessagesSendErrorResponse()) + server := helpers.MakeTestServer(http.StatusInternalServerError, stubs.HttpInternalServerErrorResponse()) client := New(WithBaseURL(server.URL), WithAPIKey(apiKey)) // Act @@ -62,7 +62,7 @@ func TestMessagesService_SendWithError(t *testing.T) { // Assert assert.NotNil(t, err) assert.Equal(t, http.StatusInternalServerError, response.HTTPResponse.StatusCode) - assert.Equal(t, string(stubs.MessagesSendErrorResponse()), string(*response.Body)) + assert.Equal(t, string(stubs.HttpInternalServerErrorResponse()), string(*response.Body)) // Teardown server.Close() diff --git a/message_thread_service.go b/message_thread_service.go index a96ce7c..962cc59 100644 --- a/message_thread_service.go +++ b/message_thread_service.go @@ -3,6 +3,7 @@ package httpsms import ( "context" "encoding/json" + "github.com/google/uuid" "net/http" "strconv" ) @@ -42,3 +43,15 @@ func (service *MessageThreadService) Index(ctx context.Context, params *MessageT return messageThreads, response, nil } + +// Delete a message thread from the database and also deletes all the messages in the thread. +// +// API Docs: https://api.httpsms.com/index.html#/MessageThreads/delete_message_threads__messageThreadID_ +func (service *MessageThreadService) Delete(ctx context.Context, messageThreadID uuid.UUID) (*Response, error) { + request, err := service.client.newRequest(ctx, http.MethodDelete, "/v1/message-threads/"+messageThreadID.String(), nil) + if err != nil { + return nil, err + } + + return service.client.do(request) +} diff --git a/message_thread_service_test.go b/message_thread_service_test.go new file mode 100644 index 0000000..6384e51 --- /dev/null +++ b/message_thread_service_test.go @@ -0,0 +1,106 @@ +package httpsms + +import ( + "context" + "encoding/json" + "github.com/google/uuid" + "net/http" + "testing" + + "github.com/NdoleStudio/httpsms-go/internal/helpers" + "github.com/NdoleStudio/httpsms-go/internal/stubs" + "github.com/stretchr/testify/assert" +) + +func TestMessageThreadService_Index(t *testing.T) { + // Setup + t.Parallel() + + // Arrange + apiKey := "test-api-key" + server := helpers.MakeTestServer(http.StatusOK, stubs.MessageThreadIndexResponse()) + client := New(WithBaseURL(server.URL), WithAPIKey(apiKey)) + + indexParams := &MessageThreadIndexParams{ + IsArchived: false, + Skip: 0, + Query: nil, + Limit: 10, + Owner: fromNumber, + } + + // Act + threads, response, err := client.MessageThreads.Index(context.Background(), indexParams) + + // Assert + assert.Nil(t, err) + + assert.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode) + + jsonContent, _ := json.Marshal(threads) + assert.JSONEq(t, string(stubs.MessageThreadIndexResponse()), string(jsonContent)) + + // Teardown + server.Close() +} + +func TestMessageThreadService_IndexWithError(t *testing.T) { + // Setup + t.Parallel() + + // Arrange + apiKey := "test-api-key" + server := helpers.MakeTestServer(http.StatusInternalServerError, stubs.HttpInternalServerErrorResponse()) + client := New(WithBaseURL(server.URL), WithAPIKey(apiKey)) + + // Act + _, response, err := client.MessageThreads.Index(context.Background(), &MessageThreadIndexParams{}) + + // Assert + assert.NotNil(t, err) + assert.Equal(t, http.StatusInternalServerError, response.HTTPResponse.StatusCode) + assert.Equal(t, string(stubs.HttpInternalServerErrorResponse()), string(*response.Body)) + + // Teardown + server.Close() +} + +func TestMessageThreadService_Delete(t *testing.T) { + // Setup + t.Parallel() + + // Arrange + apiKey := "test-api-key" + server := helpers.MakeTestServer(http.StatusOK, nil) + client := New(WithBaseURL(server.URL), WithAPIKey(apiKey)) + + // Act + response, err := client.MessageThreads.Delete(context.Background(), uuid.New()) + + // Assert + assert.Nil(t, err) + assert.Equal(t, http.StatusOK, response.HTTPResponse.StatusCode) + + // Teardown + server.Close() +} + +func TestMessageThreadService_DeleteWithError(t *testing.T) { + // Setup + t.Parallel() + + // Arrange + apiKey := "test-api-key" + server := helpers.MakeTestServer(http.StatusInternalServerError, stubs.HttpInternalServerErrorResponse()) + client := New(WithBaseURL(server.URL), WithAPIKey(apiKey)) + + // Act + response, err := client.MessageThreads.Delete(context.Background(), uuid.New()) + + // Assert + assert.NotNil(t, err) + assert.Equal(t, http.StatusInternalServerError, response.HTTPResponse.StatusCode) + + // Teardown + server.Close() +}