func (suite *AccountsTestSuite) TestGetMyUser() { // Prepare a request r, err := http.NewRequest("GET", "http://1.2.3.4/v1/me", nil) assert.NoError(suite.T(), err, "Request setup should not get an error") r.Header.Set("Authorization", "Bearer test_user_token") // Check the routing match := new(mux.RouteMatch) suite.router.Match(r, match) if assert.NotNil(suite.T(), match.Route) { assert.Equal(suite.T(), "get_my_user", match.Route.GetName()) } // And serve the request w := httptest.NewRecorder() suite.router.ServeHTTP(w, r) // Check that the mock object expectations were met suite.assertMockExpectations() // Check the status code if !assert.Equal(suite.T(), 200, w.Code) { log.Print(w.Body.String()) } // Fetch the user user := new(accounts.User) notFound := accounts.UserPreload(suite.db).First(user, suite.users[1].ID).RecordNotFound() assert.False(suite.T(), notFound) // Check the response expected := &accounts.UserResponse{ Hal: jsonhal.Hal{ Links: map[string]*jsonhal.Link{ "self": &jsonhal.Link{ Href: fmt.Sprintf("/v1/users/%d", user.ID), }, }, }, ID: user.ID, Email: user.OauthUser.Username, FirstName: user.FirstName.String, LastName: user.LastName.String, Role: roles.User, Confirmed: user.Confirmed, CreatedAt: util.FormatTime(&user.CreatedAt), UpdatedAt: util.FormatTime(&user.UpdatedAt), } testutil.TestResponseObject(suite.T(), w, expected, 200) }
func TestFormatTime(t *testing.T) { var ( timestamp time.Time expected, actual string ) // UTC timestamp = time.Date(2012, 12, 11, 8, 52, 31, 493729031, time.UTC) expected = "2012-12-11T08:52:31.493Z" actual = util.FormatTime(×tamp) assert.Equal(t, expected, actual) // UTC timestamp = time.Date(2012, 12, 11, 8, 52, 31, 493729031, time.FixedZone("HKT", 8*3600)) expected = "2012-12-11T00:52:31.493Z" actual = util.FormatTime(×tamp) assert.Equal(t, expected, actual) }
// NewUserResponse creates new UserResponse instance func NewUserResponse(o *User) (*UserResponse, error) { response := &UserResponse{ ID: o.ID, Email: o.OauthUser.Username, FirstName: o.FirstName.String, LastName: o.LastName.String, Role: o.OauthUser.RoleID.String, Confirmed: o.Confirmed, CreatedAt: util.FormatTime(&o.CreatedAt), UpdatedAt: util.FormatTime(&o.UpdatedAt), } // Set the self link response.SetLink( "self", // name fmt.Sprintf("/v1/users/%d", o.ID), // href "", // title ) return response, nil }
// NewConfirmationResponse creates new ConfirmationResponse instance func NewConfirmationResponse(o *Confirmation) (*ConfirmationResponse, error) { response := &ConfirmationResponse{ EmailTokenResponse: EmailTokenResponse{ ID: o.ID, Reference: o.Reference, EmailSent: o.EmailSent, EmailSentAt: util.FormatTime(o.EmailSentAt), ExpiresAt: util.FormatTime(&o.ExpiresAt), CreatedAt: util.FormatTime(&o.CreatedAt), UpdatedAt: util.FormatTime(&o.UpdatedAt), }, UserID: o.User.ID, } // Set the self link response.SetLink( "self", // name fmt.Sprintf("/v1/confirmations/%d", o.ID), // href "", // title ) return response, nil }
func (suite *AccountsTestSuite) TestCreateUserOnlyRequiredFields() { // Prepare a request payload, err := json.Marshal(&accounts.UserRequest{ Email: "test@newuser", Password: "******", }) assert.NoError(suite.T(), err, "JSON marshalling failed") r, err := http.NewRequest( "POST", "http://1.2.3.4/v1/users", 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")), ), ) suite.service.WaitForNotifications(1) // Mock confirmation email suite.mockConfirmationEmail() // Check the routing match := new(mux.RouteMatch) suite.router.Match(r, match) if assert.NotNil(suite.T(), match.Route) { assert.Equal(suite.T(), "create_user", match.Route.GetName()) } // Count before var ( countBefore int confirmationsCountBefore int ) suite.db.Model(new(accounts.User)).Count(&countBefore) suite.db.Model(new(accounts.Confirmation)).Count(&confirmationsCountBefore) // And serve the request w := httptest.NewRecorder() suite.router.ServeHTTP(w, r) // block until email goroutine has finished assert.True(suite.T(), <-suite.service.GetNotifications(), "The email goroutine should have run") // Count after var ( countAfter int confirmationsCountAfter int ) suite.db.Model(new(accounts.User)).Count(&countAfter) suite.db.Model(new(accounts.Confirmation)).Count(&confirmationsCountAfter) assert.Equal(suite.T(), countBefore+1, countAfter) assert.Equal(suite.T(), confirmationsCountBefore+1, confirmationsCountAfter) // Fetch the created user user := new(accounts.User) notFound := accounts.UserPreload(suite.db).Last(user).RecordNotFound() assert.False(suite.T(), notFound) // Fetch the created confirmation confirmation := new(accounts.Confirmation) assert.False(suite.T(), suite.db.Preload("User.OauthUser"). Last(confirmation).RecordNotFound()) // And correct data was saved assert.Equal(suite.T(), user.ID, user.OauthUser.MetaUserID) assert.Equal(suite.T(), "test@newuser", user.OauthUser.Username) assert.False(suite.T(), user.FirstName.Valid) assert.False(suite.T(), user.LastName.Valid) assert.Equal(suite.T(), roles.User, user.OauthUser.RoleID.String) assert.False(suite.T(), user.Confirmed) assert.Equal(suite.T(), "test@newuser", confirmation.User.OauthUser.Username) // Check the Location header assert.Equal( suite.T(), fmt.Sprintf("/v1/users/%d", user.ID), w.Header().Get("Location"), ) // Check the response expected := &accounts.UserResponse{ Hal: jsonhal.Hal{ Links: map[string]*jsonhal.Link{ "self": &jsonhal.Link{ Href: fmt.Sprintf("/v1/users/%d", user.ID), }, }, }, ID: user.ID, Email: "test@newuser", Role: roles.User, Confirmed: false, CreatedAt: util.FormatTime(&user.CreatedAt), UpdatedAt: util.FormatTime(&user.UpdatedAt), } testutil.TestResponseObject(suite.T(), w, expected, 201) // Check that the mock object expectations were met suite.assertMockExpectations() }
func (suite *AccountsTestSuite) TestUpdateUser() { testUser, testAccessToken, err := suite.insertTestUser( "harold@finch", "test_password", "Harold", "Finch") assert.NoError(suite.T(), err, "Failed to insert a test user") payload, err := json.Marshal(&accounts.UserRequest{ FirstName: "John", LastName: "Reese", }) assert.NoError(suite.T(), err, "JSON marshalling failed") r, err := http.NewRequest( "PUT", fmt.Sprintf("http://1.2.3.4/v1/users/%d", testUser.ID), bytes.NewBuffer(payload), ) assert.NoError(suite.T(), err, "Request setup should not get an error") r.Header.Set("Authorization", fmt.Sprintf("Bearer %s", testAccessToken.Token)) // Check the routing match := new(mux.RouteMatch) suite.router.Match(r, match) if assert.NotNil(suite.T(), match.Route) { assert.Equal(suite.T(), "update_user", match.Route.GetName()) } // Count before var countBefore int suite.db.Model(new(accounts.User)).Count(&countBefore) // And serve the request w := httptest.NewRecorder() suite.router.ServeHTTP(w, r) // Check that the mock object expectations were met suite.assertMockExpectations() // Count after var countAfter int suite.db.Model(new(accounts.User)).Count(&countAfter) assert.Equal(suite.T(), countBefore, countAfter) // Fetch the updated user user := new(accounts.User) notFound := accounts.UserPreload(suite.db).First(user, testUser.ID).RecordNotFound() assert.False(suite.T(), notFound) // Check that the password has NOT changed assert.NoError(suite.T(), password.VerifyPassword( user.OauthUser.Password.String, "test_password", )) // And correct data was saved assert.Equal(suite.T(), "harold@finch", user.OauthUser.Username) assert.Equal(suite.T(), "John", user.FirstName.String) assert.Equal(suite.T(), "Reese", user.LastName.String) assert.Equal(suite.T(), roles.User, user.OauthUser.RoleID.String) assert.False(suite.T(), user.Confirmed) // Check the response expected := &accounts.UserResponse{ Hal: jsonhal.Hal{ Links: map[string]*jsonhal.Link{ "self": &jsonhal.Link{ Href: fmt.Sprintf("/v1/users/%d", user.ID), }, }, }, ID: user.ID, Email: "harold@finch", FirstName: "John", LastName: "Reese", Role: roles.User, Confirmed: false, CreatedAt: util.FormatTime(&user.CreatedAt), UpdatedAt: util.FormatTime(&user.UpdatedAt), } testutil.TestResponseObject(suite.T(), w, expected, 200) }
func (suite *AccountsTestSuite) TestUpdateUserPassword() { var ( testOauthUser *oauth.User testUser *accounts.User testAccessToken *oauth.AccessToken err error ) // Insert a test user testOauthUser, err = suite.service.GetOauthService().CreateUser( roles.User, "harold@finch", "test_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 // Login the test user testAccessToken, _, err = suite.service.GetOauthService().Login( suite.accounts[0].OauthClient, testUser.OauthUser, "read_write", // scope ) assert.NoError(suite.T(), err, "Failed to login the test user") payload, err := json.Marshal(&accounts.UserRequest{ Password: "******", NewPassword: "******", }) assert.NoError(suite.T(), err, "JSON marshalling failed") r, err := http.NewRequest( "PUT", fmt.Sprintf("http://1.2.3.4/v1/users/%d", testUser.ID), bytes.NewBuffer(payload), ) assert.NoError(suite.T(), err, "Request setup should not get an error") r.Header.Set("Authorization", fmt.Sprintf("Bearer %s", testAccessToken.Token)) // Check the routing match := new(mux.RouteMatch) suite.router.Match(r, match) if assert.NotNil(suite.T(), match.Route) { assert.Equal(suite.T(), "update_user", match.Route.GetName()) } // Count before var countBefore int suite.db.Model(new(accounts.User)).Count(&countBefore) // And serve the request w := httptest.NewRecorder() suite.router.ServeHTTP(w, r) // Check that the mock object expectations were met suite.assertMockExpectations() // Count after var countAfter int suite.db.Model(new(accounts.User)).Count(&countAfter) assert.Equal(suite.T(), countBefore, countAfter) // Fetch the updated user user := new(accounts.User) notFound := accounts.UserPreload(suite.db).First(user, testUser.ID).RecordNotFound() assert.False(suite.T(), notFound) // Check that the password has changed assert.Error(suite.T(), password.VerifyPassword( user.OauthUser.Password.String, "test_password", )) assert.NoError(suite.T(), password.VerifyPassword( user.OauthUser.Password.String, "some_new_password", )) // And the user meta data is unchanged assert.Equal(suite.T(), "harold@finch", user.OauthUser.Username) assert.Equal(suite.T(), "Harold", user.FirstName.String) assert.Equal(suite.T(), "Finch", user.LastName.String) assert.Equal(suite.T(), roles.User, user.OauthUser.RoleID.String) assert.False(suite.T(), user.Confirmed) // Check the response expected := &accounts.UserResponse{ Hal: jsonhal.Hal{ Links: map[string]*jsonhal.Link{ "self": &jsonhal.Link{ Href: fmt.Sprintf("/v1/users/%d", user.ID), }, }, }, ID: user.ID, Email: "harold@finch", FirstName: "Harold", LastName: "Finch", Role: roles.User, Confirmed: false, CreatedAt: util.FormatTime(&user.CreatedAt), UpdatedAt: util.FormatTime(&user.UpdatedAt), } testutil.TestResponseObject(suite.T(), w, expected, 200) }