func (suite *AccountsTestSuite) TestFindInvitationByReference() { var ( testExpiredInvitation, testInvitation, invitation *accounts.Invitation err error ) // Insert test invitations testExpiredInvitation, err = accounts.NewInvitation( suite.users[0], suite.users[1], -10, // expires in ) assert.NoError(suite.T(), err, "Failed to create a new invitation object") err = suite.db.Create(testExpiredInvitation).Error assert.NoError(suite.T(), err, "Inserting test data failed") testInvitation, err = accounts.NewInvitation( suite.users[0], suite.users[1], suite.cnf.AppSpecific.InvitationLifetime, ) assert.NoError(suite.T(), err, "Failed to create a new invitation object") err = suite.db.Create(testInvitation).Error assert.NoError(suite.T(), err, "Inserting test data failed") // Let's try to find an expired invitation by a valid reference invitation, err = suite.service.FindInvitationByReference(testExpiredInvitation.Reference) // Invitation should be nil assert.Nil(suite.T(), invitation) // Correct error should be returned if assert.NotNil(suite.T(), err) { assert.Equal(suite.T(), accounts.ErrInvitationNotFound, err) } // Let's try to find an invitation by a bogus reference invitation, err = suite.service.FindInvitationByReference("bogus") // Invitation should be nil assert.Nil(suite.T(), invitation) // Correct error should be returned if assert.NotNil(suite.T(), err) { assert.Equal(suite.T(), accounts.ErrInvitationNotFound, err) } // Now let's pass a valid reference invitation, err = suite.service.FindInvitationByReference(testInvitation.Reference) // Error should be nil assert.Nil(suite.T(), err) // Correct invitation should be returned with preloaded data if assert.NotNil(suite.T(), invitation) { assert.Equal(suite.T(), suite.users[0].ID, invitation.InvitedUser.ID) assert.Equal(suite.T(), suite.users[1].ID, invitation.InvitedByUser.ID) assert.False(suite.T(), invitation.EmailSent) assert.Nil(suite.T(), invitation.EmailSentAt) } }
func (suite *AccountsTestSuite) TestConfirmInvitation() { var ( testOauthUser *oauth.User testUser *accounts.User testInvitation *accounts.Invitation err error ) // Insert a test user testOauthUser, err = suite.service.GetOauthService().CreateUser( roles.User, "harold@finch", "", // blank password ) assert.NoError(suite.T(), err, "Failed to insert a test oauth user") testUser, err = accounts.NewUser( suite.accounts[0], testOauthUser, "", //facebook ID false, // confirmed &accounts.UserRequest{ FirstName: "Harold", LastName: "Finch", }, ) assert.NoError(suite.T(), err, "Failed to create a new user object") err = suite.db.Create(testUser).Error assert.NoError(suite.T(), err, "Failed to insert a test user") testUser.Account = suite.accounts[0] testUser.OauthUser = testOauthUser // Insert a test invitation testInvitation, err = accounts.NewInvitation( testUser, suite.users[0], suite.cnf.AppSpecific.InvitationLifetime, ) assert.NoError(suite.T(), err, "Failed to create a new invitation object") err = suite.db.Create(testInvitation).Error assert.NoError(suite.T(), err, "Failed to insert a test invitation") testInvitation.InvitedUser = testUser testInvitation.InvitedByUser = suite.users[0] // Prepare a request payload, err := json.Marshal(&accounts.ConfirmInvitationRequest{ PasswordRequest: accounts.PasswordRequest{Password: "******"}, }) assert.NoError(suite.T(), err, "JSON marshalling failed") r, err := http.NewRequest( "POST", fmt.Sprintf("http://1.2.3.4/v1/invitations/%s", testInvitation.Reference), bytes.NewBuffer(payload), ) assert.NoError(suite.T(), err, "Request setup should not get an error") r.Header.Set( "Authorization", fmt.Sprintf( "Basic %s", b64.StdEncoding.EncodeToString([]byte("test_client_1:test_secret")), ), ) // Check the routing match := new(mux.RouteMatch) suite.router.Match(r, match) if assert.NotNil(suite.T(), match.Route) { assert.Equal(suite.T(), "confirm_invitation", match.Route.GetName()) } // Count before var countBefore int suite.db.Model(new(accounts.Invitation)).Count(&countBefore) // And serve the request w := httptest.NewRecorder() suite.router.ServeHTTP(w, r) // Count after var countAfter int suite.db.Model(new(accounts.Invitation)).Count(&countAfter) assert.Equal(suite.T(), countBefore-1, countAfter) // Fetch the updated user user := new(accounts.User) notFound := accounts.UserPreload(suite.db).First(user, testUser.ID).RecordNotFound() assert.False(suite.T(), notFound) // Invitation should have been soft deleted assert.True(suite.T(), suite.db.First(new(accounts.Invitation), testInvitation.ID).RecordNotFound()) // And correct data was saved assert.Nil(suite.T(), pass.VerifyPassword(user.OauthUser.Password.String, "test_password")) // Check the response expected, err := accounts.NewInvitationResponse(testInvitation) assert.NoError(suite.T(), err, "Failed to create expected response object") testutil.TestResponseObject(suite.T(), w, expected, 200) }