Skip to content

Commit

Permalink
fixed connection errors
Browse files Browse the repository at this point in the history
  • Loading branch information
elraphty committed Mar 4, 2024
1 parent cc26173 commit b512751
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 15 deletions.
6 changes: 6 additions & 0 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -1496,6 +1496,12 @@ func (db database) GetPeopleListShort(count uint32) *[]PersonInShort {
}

func (db database) CreateConnectionCode(c []ConnectionCodes) ([]ConnectionCodes, error) {
now := time.Now()
for _, code := range c {
if code.DateCreated.IsZero() {
code.DateCreated = &now
}
}
db.db.Create(&c)
return c, nil
}
Expand Down
3 changes: 0 additions & 3 deletions handlers/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"io"
"net/http"
"time"

"github.com/form3tech-oss/jwt-go"
"github.com/stakwork/sphinx-tribes/auth"
Expand Down Expand Up @@ -56,7 +55,6 @@ func (ah *authHandler) GetIsAdmin(w http.ResponseWriter, r *http.Request) {
func (ah *authHandler) CreateConnectionCode(w http.ResponseWriter, r *http.Request) {
codeArr := []db.ConnectionCodes{}
codeStrArr := []string{}
now := time.Now()

body, err := io.ReadAll(r.Body)
r.Body.Close()
Expand All @@ -67,7 +65,6 @@ func (ah *authHandler) CreateConnectionCode(w http.ResponseWriter, r *http.Reque
code := db.ConnectionCodes{
ConnectionString: code,
IsUsed: false,
DateCreated: &now,
}
codeArr = append(codeArr, code)
}
Expand Down
34 changes: 22 additions & 12 deletions handlers/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/stakwork/sphinx-tribes/db"
mocks "github.com/stakwork/sphinx-tribes/mocks"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)

func TestGetAdminPubkeys(t *testing.T) {
Expand Down Expand Up @@ -52,16 +51,21 @@ func TestGetAdminPubkeys(t *testing.T) {
}

func TestCreateConnectionCode(t *testing.T) {

mockDb := mocks.NewDatabase(t)
aHandler := NewAuthHandler(mockDb)
t.Run("should create connection code successful", func(t *testing.T) {
codeToBeInserted := db.ConnectionCodes{
ConnectionString: "custom connection string",
codeToBeInserted := []string{"custom connection string", "custom connection string 2"}

codeArr := []db.ConnectionCodes{}
for _, code := range codeToBeInserted {
code := db.ConnectionCodes{
ConnectionString: code,
IsUsed: false,
}
codeArr = append(codeArr, code)
}
mockDb.On("CreateConnectionCode", mock.MatchedBy(func(code db.ConnectionCodes) bool {
return code.IsUsed == false && code.ConnectionString == codeToBeInserted.ConnectionString
})).Return(codeToBeInserted, nil).Once()

mockDb.On("CreateConnectionCode", codeArr).Return(codeArr, nil).Once()

body, _ := json.Marshal(codeToBeInserted)
req, err := http.NewRequest("POST", "/connectioncodes", bytes.NewBuffer(body))
Expand All @@ -77,12 +81,18 @@ func TestCreateConnectionCode(t *testing.T) {
})

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

codeArr := []db.ConnectionCodes{}
for _, code := range codeToBeInserted {
code := db.ConnectionCodes{
ConnectionString: code,
IsUsed: false,
}
codeArr = append(codeArr, code)
}
mockDb.On("CreateConnectionCode", mock.MatchedBy(func(code db.ConnectionCodes) bool {
return code.IsUsed == false && code.ConnectionString == codeToBeInserted.ConnectionString
})).Return(codeToBeInserted, errors.New("failed to create connection")).Once()

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))
Expand Down

0 comments on commit b512751

Please sign in to comment.