diff --git a/db/db.go b/db/db.go index dfe629082..018f49c4b 100644 --- a/db/db.go +++ b/db/db.go @@ -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 } } diff --git a/handlers/auth_test.go b/handlers/auth_test.go index b83968ca2..2c65a58ad 100644 --- a/handlers/auth_test.go +++ b/handlers/auth_test.go @@ -50,17 +50,15 @@ func TestGetAdminPubkeys(t *testing.T) { } }) } + func TestCreateConnectionCode(t *testing.T) { teardownSuite := SetupSuite(t) defer teardownSuite(t) aHandler := NewAuthHandler(db.TestDB) - t.Run("should create connection code successful", func(t *testing.T) { - rr := httptest.NewRecorder() handler := http.HandlerFunc(aHandler.CreateConnectionCode) - - codeStrArr := []string{"custom connection string"} + codeStrArr := []string{"sampleCode1"} codeArr := []db.ConnectionCodes{} now := time.Now() @@ -78,6 +76,7 @@ func TestCreateConnectionCode(t *testing.T) { codeShort := db.ConnectionCodesShort{ ConnectionString: codeArr[0].ConnectionString, + DateCreated: codeArr[0].DateCreated, } db.TestDB.CreateConnectionCode(codeArr) @@ -88,16 +87,21 @@ func TestCreateConnectionCode(t *testing.T) { t.Fatal(err) } + codes := db.TestDB.GetConnectionCode() handler.ServeHTTP(rr, req) assert.Equal(t, http.StatusOK, rr.Code) - fetchedCodes := db.TestDB.GetConnectionCode() - - assert.EqualValues(t, codeShort.ConnectionString, fetchedCodes.ConnectionString) + 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 { @@ -114,9 +118,7 @@ func TestCreateConnectionCode(t *testing.T) { t.Fatal(err) } rr := httptest.NewRecorder() - handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - http.Error(w, "failed to create connection", http.StatusBadRequest) - }) + handler := http.HandlerFunc(aHandler.CreateConnectionCode) handler.ServeHTTP(rr, req) assert.Equal(t, http.StatusBadRequest, rr.Code)