Skip to content

Commit

Permalink
Add ability to delete message threads
Browse files Browse the repository at this point in the history
  • Loading branch information
AchoArnold committed Jan 31, 2024
1 parent f2fad94 commit 503d615
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 14 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions heartbeat_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down
10 changes: 0 additions & 10 deletions internal/stubs/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
`)
}
27 changes: 27 additions & 0 deletions internal/stubs/message_thread.go
Original file line number Diff line number Diff line change
@@ -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"
}
`)
}
11 changes: 11 additions & 0 deletions internal/stubs/response.go
Original file line number Diff line number Diff line change
@@ -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"
}
`)
}
4 changes: 2 additions & 2 deletions message_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down
13 changes: 13 additions & 0 deletions message_thread_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package httpsms
import (
"context"
"encoding/json"
"github.com/google/uuid"
"net/http"
"strconv"
)
Expand Down Expand Up @@ -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)
}
106 changes: 106 additions & 0 deletions message_thread_service_test.go
Original file line number Diff line number Diff line change
@@ -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()
}

0 comments on commit 503d615

Please sign in to comment.