Files
schedtxt_api/internal/handler/contact_test.go
T
phoenixandphoenix a51979e724 tsk-58: Update names of Contact endpoint (#67)
Closes #58

Reviewed-on: phoenix/textsender-api#67
Co-authored-by: phoenix <kundeng00@pm.me>
Co-committed-by: phoenix <kundeng00@pm.me>
2026-01-07 20:24:30 +00:00

124 lines
3.6 KiB
Go

package handler
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
"git.kundeng.us/phoenix/textsender-models/tx0/contact"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"git.kundeng.us/phoenix/textsender-api/internal/handler/endpoint"
"git.kundeng.us/phoenix/textsender-api/internal/store/mock"
)
type Request struct {
PhoneNumber string
UserId uuid.UUID
}
func TestCreateContactWithMock(t *testing.T) {
mockstore := mock.NewMockContactStore()
apiApp, err := GetApp()
assert.NoError(t, err, "Error getting app")
handler := NewContactHandler(apiApp, mockstore)
testUserId := uuid.New()
testBody := Request{PhoneNumber: "+12335403383", UserId: testUserId}
jsonValue, _ := json.Marshal(testBody)
req, _ := http.NewRequest("POST", endpoint.ADD_CONTACT_ENDPOINT, strings.NewReader(string(jsonValue)))
rr := httptest.NewRecorder()
handler.AddContact(rr, req)
assert.Equal(t, http.StatusCreated, rr.Code)
var response AddContactResponse
err = json.Unmarshal(rr.Body.Bytes(), &response)
assert.NoError(t, err, "Error Creating contact %v", err)
assert.NotEmpty(t, response.Data, "No Contact created")
assert.NotNil(t, response.Data[0].Id, "Id should not be nil")
}
func TestGetContactWithMock(t *testing.T) {
mockstore := mock.NewMockContactStore()
testUserId := uuid.New()
testCon := contact.Contact{PhoneNumber: "+12335403383", UserId: &testUserId}
ctx := t.Context()
if err := mockstore.CreateContact(ctx, &testCon); err != nil {
assert.NoError(t, err, "Error creating contact")
return
}
url := fmt.Sprintf("%s?user_id=%s", endpoint.GET_CONTACT, testCon.UserId)
req, _ := http.NewRequest("GET", url, nil)
rr := httptest.NewRecorder()
apiApp, err := GetApp()
assert.NoError(t, err, "Error getting app")
contactHandler := NewContactHandler(apiApp, mockstore)
contactHandler.GetContact(rr, req)
assert.Equal(t, http.StatusOK, rr.Code)
var response GetContactResponse
err = json.Unmarshal(rr.Body.Bytes(), &response)
assert.NoError(t, err, "Error getting contact %v", err)
assert.NotEmpty(t, response.Data, "No Contact retrieved")
assert.NotNil(t, response.Data[0].Id, "Id should not be nil")
}
func TestUpdateContactNamesWithMock(t *testing.T) {
mockstore := mock.NewMockContactStore()
testUserId := uuid.New()
testCon := contact.Contact{PhoneNumber: "+12335403383", UserId: &testUserId}
ctx := t.Context()
var firstname, lastname, nickname string
firstname = "Bob"
lastname = "De-Buildor"
nickname = "With The Tool"
if err := mockstore.CreateContact(ctx, &testCon); err != nil {
assert.NoError(t, err, "Error creating contact")
return
}
var requestBody UpdateNameRequest
requestBody.Firstname = &firstname
requestBody.Lastname = &lastname
requestBody.Nickname = &nickname
requestBody.ContactId = *testCon.Id
requestBody.UserId = testUserId
jsonValue, _ := json.Marshal(requestBody)
req, _ := http.NewRequest("PATCH", endpoint.Update_Names_Endpoint, strings.NewReader(string(jsonValue)))
rr := httptest.NewRecorder()
apiApp, err := GetApp()
assert.NoError(t, err, "Error getting app")
contactHandler := NewContactHandler(apiApp, mockstore)
contactHandler.UpdateName(rr, req)
assert.Equal(t, http.StatusOK, rr.Code)
var response UpdateNameResponse
err = json.Unmarshal(rr.Body.Bytes(), &response)
assert.NoError(t, err, "Error updating names of contact %v", err)
assert.NotEmpty(t, response.Data, "No Contact updated")
assert.NotNil(t, response.Data[0].Firstname, "Firstname should not be nil")
assert.NotNil(t, response.Data[0].Lastname, "Lastname should not be nil")
assert.NotNil(t, response.Data[0].Nickname, "Nickname should not be nil")
}