Skip to content

Commit

Permalink
Merge pull request #96 from tigrisdata/main
Browse files Browse the repository at this point in the history
Beta release
  • Loading branch information
JigarJoshi authored May 11, 2023
2 parents e664add + b47e41f commit 3ad868f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
14 changes: 9 additions & 5 deletions api/Invitation.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ type DeleteInvitationsParam struct {
type VerifyInvitationParams struct {
Email string `json:"email"`
Code string `json:"code"`
// When this flag is used, invitation doesn't get marked as accepted.
Dry bool `json:"dry"`
}

type VerifyInvitationResponse struct {
Expand Down Expand Up @@ -194,11 +196,13 @@ func (a *API) VerifyInvitation(w http.ResponseWriter, r *http.Request) error {
var invitation models.Invitation
for itr.Next(&invitation) {
if invitation.Code == params.Code && time.Now().UnixMilli() <= invitation.ExpirationTime && invitation.Status == InvitationStatusPending {
// mark invitation as accepted
invitation.Status = InvitationStatusAccepted
_, err := tigris.GetCollection[models.Invitation](a.db).InsertOrReplace(ctx, &invitation)
if err != nil {
return internalServerError("Failed to verify invitation").WithInternalError(err).WithInternalMessage("Failed to update status on successful verification")
// mark invitation as accepted, if not dry
if !params.Dry {
invitation.Status = InvitationStatusAccepted
_, err := tigris.GetCollection[models.Invitation](a.db).InsertOrReplace(ctx, &invitation)
if err != nil {
return internalServerError("Failed to verify invitation").WithInternalError(err).WithInternalMessage("Failed to update status on successful verification")
}
}
return sendJSON(w, http.StatusOK, VerifyInvitationResponse{TigrisNamespace: invitation.TigrisNamespace, TigrisNamespaceName: invitation.TigrisNamespaceName, Role: invitation.Role})
}
Expand Down
32 changes: 29 additions & 3 deletions api/invitation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (ts *InvitationTestSuite) TestInvitationVerification() {
invitations := listInvitations(ts, "org_a")
require.Equal(ts.T(), 1, len(invitations))
code := invitations[0].Code
req := invitationVerificationRequest(ts, email, code)
req := invitationVerificationRequest(ts, email, code, false)

w1 := httptest.NewRecorder()

Expand All @@ -143,17 +143,43 @@ func (ts *InvitationTestSuite) TestInvitationVerification() {
// with invalid code
w2 := httptest.NewRecorder()

reqWithInvalidCode := invitationVerificationRequest(ts, email, "invalid")
reqWithInvalidCode := invitationVerificationRequest(ts, email, "invalid", false)
ts.API.handler.ServeHTTP(w2, reqWithInvalidCode)
require.Equal(ts.T(), http.StatusUnauthorized, w2.Code)
}

func invitationVerificationRequest(ts *InvitationTestSuite, email string, code string) *http.Request {
// TestInvitationVerificationDry tests API /invitation/verify route
func (ts *InvitationTestSuite) TestInvitationVerificationDry() {
email := "[email protected]"
_ = createInvitation(ts, "[email protected]", "editor", "org_a", "org_a_display_name", "google2|123", "org_a admin username", time.Now().UnixMilli()+86400*1000)

// list invitations for org_a
invitations := listInvitations(ts, "org_a")
require.Equal(ts.T(), 1, len(invitations))
code := invitations[0].Code

// when dry flag is used, the invitation doesn't get marked as accepted.
for i := 0; i < 5; i++ {
req := invitationVerificationRequest(ts, email, code, true)

w1 := httptest.NewRecorder()

ts.API.handler.ServeHTTP(w1, req)
require.Equal(ts.T(), http.StatusOK, w1.Code)
data := VerifyInvitationResponse{}
require.NoError(ts.T(), json.NewDecoder(w1.Body).Decode(&data))
require.Equal(ts.T(), "org_a", data.TigrisNamespace)
require.Equal(ts.T(), "org_a_display_name", data.TigrisNamespaceName)
}
}

func invitationVerificationRequest(ts *InvitationTestSuite, email string, code string, dry bool) *http.Request {
// Request body
var buffer bytes.Buffer
require.NoError(ts.T(), json.NewEncoder(&buffer).Encode(map[string]interface{}{
"email": email,
"code": code,
"dry": dry,
}))
req := httptest.NewRequest(http.MethodPost, "/invitations/verify", &buffer)
req.Header.Set("Content-Type", "application/json")
Expand Down
2 changes: 1 addition & 1 deletion mailer/customerio.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (m *CustomerIOMailer) InviteMail(user *models.User, referrerURL string) err
}

func (m *CustomerIOMailer) TigrisInviteMail(email string, invitedByName string, code string, invitedOrgCode string, invitedOrgName string, role string, expirationTime int64) error {
invitationURL := fmt.Sprintf("%s/invitation?code=%s", m.Config.TigrisConsoleURL, code)
invitationURL := fmt.Sprintf("%s/invitation?code=%s&email=%s", m.Config.TigrisConsoleURL, code, email)
request := customerio.SendEmailRequest{
To: email,
TransactionalMessageID: m.templateId,
Expand Down

0 comments on commit 3ad868f

Please sign in to comment.