Skip to content

Commit

Permalink
Merge pull request stakwork#1751 from MahtabBukhari/Refactor_TestGetC…
Browse files Browse the repository at this point in the history
…onnectionCode_To_Use_A_Real

Refactor TestGetConnectionCode To Use A Real Postgres DB For The Test
  • Loading branch information
elraphty authored Jun 25, 2024
2 parents b0c735b + 66ac6e8 commit c7019ee
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 17 deletions.
52 changes: 40 additions & 12 deletions handlers/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,30 +139,58 @@ func TestCreateConnectionCode(t *testing.T) {
}

func TestGetConnectionCode(t *testing.T) {
mockDb := mocks.NewDatabase(t)
aHandler := NewAuthHandler(mockDb)
teardownSuite := SetupSuite(t)
defer teardownSuite(t)
aHandler := NewAuthHandler(db.TestDB)

t.Run("should return connection code from db", func(t *testing.T) {
creationDate, _ := time.Parse(time.RFC3339, "2000-01-01T00:00:00Z")
existingConnectionCode := db.ConnectionCodesShort{
ConnectionString: "test",
DateCreated: &creationDate,

rr := httptest.NewRecorder()
handler := http.HandlerFunc(aHandler.GetConnectionCode)

codeStrArr := []string{"sampleCode1"}

codeArr := []db.ConnectionCodes{}
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("GetConnectionCode").Return(existingConnectionCode).Once()

// Ensure codeArr has at least one element
codeShort := db.ConnectionCodesShort{
ConnectionString: codeArr[0].ConnectionString,
DateCreated: codeArr[0].DateCreated,
}

db.TestDB.CreateConnectionCode(codeArr)

req, err := http.NewRequest("GET", "/connectioncodes", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(aHandler.GetConnectionCode)

fetchedCodes := db.TestDB.GetConnectionCode()

handler.ServeHTTP(rr, req)

assert.Equal(t, http.StatusOK, rr.Code)
expected := `{"connection_string":"test","date_created":"2000-01-01T00:00:00Z"}`
assert.EqualValues(t, expected, strings.TrimRight(rr.Body.String(), "\n"))
})
assert.EqualValues(t, codeShort.ConnectionString, fetchedCodes.ConnectionString)
tolerance := time.Millisecond
timeDifference := codeShort.DateCreated.Sub(*fetchedCodes.DateCreated)
if timeDifference < 0 {
timeDifference = -timeDifference
}
assert.True(t, timeDifference <= tolerance, "Expected DateCreated to be within tolerance")

})
}

func TestGetIsAdmin(t *testing.T) {
Expand Down
6 changes: 1 addition & 5 deletions handlers/bots_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ import (
"github.com/lib/pq"
"github.com/stakwork/sphinx-tribes/auth"
"github.com/stakwork/sphinx-tribes/db"
dbMocks "github.com/stakwork/sphinx-tribes/mocks"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)

func TestGetBotByUniqueName(t *testing.T) {
Expand Down Expand Up @@ -517,9 +516,6 @@ func TestGetBot(t *testing.T) {
err = json.Unmarshal(rr.Body.Bytes(), &returnedBot)
assert.Equal(t, http.StatusOK, rr.Code)

returnedBot.Tsv = ""
fetchedBot.Tsv = ""

assert.Equal(t, bot, returnedBot)
assert.Equal(t, bot, fetchedBot)
})
Expand Down

0 comments on commit c7019ee

Please sign in to comment.