Skip to content

Commit

Permalink
Merge pull request #1776 from Shoaibdev7/Refactor-TestCreateConnectio…
Browse files Browse the repository at this point in the history
…nCode-UT

Refactor TestCreateConnectionCode To Use A Real Postgres DB For The Test
  • Loading branch information
elraphty committed Jun 27, 2024
2 parents cfc3003 + 7475724 commit 09299cd
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 17 deletions.
5 changes: 4 additions & 1 deletion db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -1570,9 +1570,12 @@ func (db database) GetPeopleListShort(count uint32) *[]PersonInShort {
}

func (db database) CreateConnectionCode(c []ConnectionCodes) ([]ConnectionCodes, error) {
if len(c) == 0 {
return nil, fmt.Errorf("no connection codes provided")
}
now := time.Now()
for _, code := range c {
if code.DateCreated.IsZero() {
if code.DateCreated == nil || code.DateCreated.IsZero() {
code.DateCreated = &now
}
}
Expand Down
46 changes: 30 additions & 16 deletions handlers/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"github.com/google/uuid"
"github.com/lib/pq"
mocks "github.com/stakwork/sphinx-tribes/mocks"
Expand Down Expand Up @@ -53,37 +52,56 @@ func TestGetAdminPubkeys(t *testing.T) {
}

func TestCreateConnectionCode(t *testing.T) {
mockDb := mocks.NewDatabase(t)
aHandler := NewAuthHandler(mockDb)
teardownSuite := SetupSuite(t)
defer teardownSuite(t)
aHandler := NewAuthHandler(db.TestDB)
t.Run("should create connection code successful", func(t *testing.T) {
codeToBeInserted := []string{"custom connection string", "custom connection string 2"}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(aHandler.CreateConnectionCode)
codeStrArr := []string{"sampleCode1"}

codeArr := []db.ConnectionCodes{}
for _, code := range codeToBeInserted {
now := time.Now()

for i, code := range codeStrArr {
code := db.ConnectionCodes{
ID: uint(i),
ConnectionString: code,
IsUsed: false,
DateCreated: &now,
}

codeArr = append(codeArr, code)
}

mockDb.On("CreateConnectionCode", codeArr).Return(codeArr, nil).Once()
codeShort := db.ConnectionCodesShort{
ConnectionString: codeArr[0].ConnectionString,
DateCreated: codeArr[0].DateCreated,
}

db.TestDB.CreateConnectionCode(codeArr)

body, _ := json.Marshal(codeToBeInserted)
body, _ := json.Marshal(codeStrArr)
req, err := http.NewRequest("POST", "/connectioncodes", bytes.NewBuffer(body))
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(aHandler.CreateConnectionCode)

codes := db.TestDB.GetConnectionCode()
handler.ServeHTTP(rr, req)
assert.Equal(t, http.StatusOK, rr.Code)
mockDb.AssertExpectations(t)

assert.EqualValues(t, codeShort.ConnectionString, codes.ConnectionString)
tolerance := time.Millisecond
timeDifference := codeShort.DateCreated.Sub(*codes.DateCreated)
if timeDifference < 0 {
timeDifference = -timeDifference
}
assert.True(t, timeDifference <= tolerance, "Expected DateCreated to be within tolerance")
})

t.Run("should return error if failed to add connection code", func(t *testing.T) {
codeToBeInserted := []string{"custom connection string", "custom connection string 2"}
codeToBeInserted := []string{}

codeArr := []db.ConnectionCodes{}
for _, code := range codeToBeInserted {
Expand All @@ -94,8 +112,6 @@ func TestCreateConnectionCode(t *testing.T) {
codeArr = append(codeArr, code)
}

mockDb.On("CreateConnectionCode", codeArr).Return(codeArr, errors.New("failed to create connection")).Once()

body, _ := json.Marshal(codeToBeInserted)
req, err := http.NewRequest("POST", "/connectioncodes", bytes.NewBuffer(body))
if err != nil {
Expand All @@ -106,7 +122,6 @@ func TestCreateConnectionCode(t *testing.T) {

handler.ServeHTTP(rr, req)
assert.Equal(t, http.StatusBadRequest, rr.Code)
mockDb.AssertExpectations(t)
})

t.Run("should return error for malformed request body", func(t *testing.T) {
Expand All @@ -120,7 +135,6 @@ func TestCreateConnectionCode(t *testing.T) {

handler.ServeHTTP(rr, req)
assert.Equal(t, http.StatusNotAcceptable, rr.Code)
mockDb.AssertExpectations(t)
})

t.Run("should return error for invalid json", func(t *testing.T) {
Expand All @@ -134,7 +148,6 @@ func TestCreateConnectionCode(t *testing.T) {

handler.ServeHTTP(rr, req)
assert.Equal(t, http.StatusNotAcceptable, rr.Code)
mockDb.AssertExpectations(t)
})
}

Expand Down Expand Up @@ -191,6 +204,7 @@ func TestGetConnectionCode(t *testing.T) {
assert.True(t, timeDifference <= tolerance, "Expected DateCreated to be within tolerance")

})

}

func TestGetIsAdmin(t *testing.T) {
Expand Down

0 comments on commit 09299cd

Please sign in to comment.