diff --git a/handlers/auth_test.go b/handlers/auth_test.go index f2e10ec9e..bd98c2745 100644 --- a/handlers/auth_test.go +++ b/handlers/auth_test.go @@ -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) { diff --git a/handlers/bots_test.go b/handlers/bots_test.go index d13d5f080..c48dec0ab 100644 --- a/handlers/bots_test.go +++ b/handlers/bots_test.go @@ -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) { @@ -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) })