Closes #5 Reviewed-on: phoenix/textsender-api#7 Co-authored-by: phoenix <kundeng00@pm.me> Co-committed-by: phoenix <kundeng00@pm.me>
54 lines
1.3 KiB
Go
54 lines
1.3 KiB
Go
package handler
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"git.kundeng.us/phoenix/textsender-api/internal/db"
|
|
"git.kundeng.us/phoenix/textsender-api/internal/handler/endpoint"
|
|
)
|
|
|
|
type Request struct {
|
|
PhoneNumber string
|
|
UserId uuid.UUID
|
|
}
|
|
|
|
func TestCreateContactWithMock(t *testing.T) {
|
|
mockstore := NewMockContactStore()
|
|
handler := NewContactHandler(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 resetTestDB(t *testing.T) {
|
|
t.Helper()
|
|
_, err := db.Pool.Exec(context.Background(), "DELETE FROM contacts")
|
|
if err != nil {
|
|
t.Fatalf("Failed to reset test database: %v", err)
|
|
}
|
|
}
|