本文整理匯總了Golang中github.com/RichardKnop/example-api/accounts.User.Account方法的典型用法代碼示例。如果您正苦於以下問題:Golang User.Account方法的具體用法?Golang User.Account怎麽用?Golang User.Account使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/RichardKnop/example-api/accounts.User
的用法示例。
在下文中一共展示了User.Account方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: insertTestUser
func (suite *AccountsTestSuite) insertTestUser(email, password, firstName, lastName string) (*accounts.User, *oauth.AccessToken, error) {
var (
testOauthUser *oauth.User
testUser *accounts.User
testAccessToken *oauth.AccessToken
err error
)
// Insert a test user
testOauthUser, err = suite.service.GetOauthService().CreateUser(
roles.User,
email,
password,
)
if err != nil {
return nil, nil, err
}
testUser, err = accounts.NewUser(
suite.accounts[0],
testOauthUser,
"", //facebook ID
false, // confirmed
&accounts.UserRequest{
FirstName: firstName,
LastName: lastName,
},
)
if err != nil {
return nil, nil, err
}
err = suite.db.Create(testUser).Error
testUser.Account = suite.accounts[0]
testUser.OauthUser = testOauthUser
if err != nil {
return nil, nil, err
}
// Login the test user
testAccessToken, _, err = suite.service.GetOauthService().Login(
suite.accounts[0].OauthClient,
testUser.OauthUser,
"read_write", // scope
)
if err != nil {
return nil, nil, err
}
return testUser, testAccessToken, nil
}
示例2: TestConfirmEmail
func (suite *AccountsTestSuite) TestConfirmEmail() {
var (
testOauthUser *oauth.User
testUser *accounts.User
testConfirmation *accounts.Confirmation
err error
)
// Insert a test user
testOauthUser, err = suite.service.GetOauthService().CreateUser(
roles.User,
"[email protected]",
"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
// Insert a test confirmation
testConfirmation, err = accounts.NewConfirmation(
testUser,
suite.cnf.AppSpecific.ConfirmationLifetime,
)
assert.NoError(suite.T(), err, "Failed to create a new confirmation object")
err = suite.db.Create(testConfirmation).Error
assert.NoError(suite.T(), err, "Failed to insert a test confirmation")
testConfirmation.User = testUser
// Prepare a request
r, err := http.NewRequest(
"GET",
fmt.Sprintf(
"http://1.2.3.4/v1/confirmations/%s",
testConfirmation.Reference,
),
nil,
)
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_email", match.Route.GetName())
}
// Count before
var (
countBefore int
accessTokensCountBefore int
refreshTokensCountBefore int
)
suite.db.Model(new(accounts.Confirmation)).Count(&countBefore)
suite.db.Model(new(oauth.AccessToken)).Count(&accessTokensCountBefore)
suite.db.Model(new(oauth.RefreshToken)).Count(&refreshTokensCountBefore)
// And serve the request
w := httptest.NewRecorder()
suite.router.ServeHTTP(w, r)
// Count after
var (
countAfter int
accessTokensCountAfter int
refreshTokensCountAfter int
)
suite.db.Model(new(accounts.Confirmation)).Count(&countAfter)
suite.db.Model(new(oauth.AccessToken)).Count(&accessTokensCountAfter)
suite.db.Model(new(oauth.RefreshToken)).Count(&refreshTokensCountAfter)
assert.Equal(suite.T(), countBefore-1, countAfter)
assert.Equal(suite.T(), accessTokensCountBefore, accessTokensCountAfter)
assert.Equal(suite.T(), refreshTokensCountBefore, refreshTokensCountAfter)
// Fetch the updated user
user := new(accounts.User)
notFound := accounts.UserPreload(suite.db).First(user, testUser.ID).RecordNotFound()
assert.False(suite.T(), notFound)
// Confirmation should have been soft deleteted
assert.True(suite.T(), suite.db.Last(new(accounts.Confirmation)).RecordNotFound())
//.........這裏部分代碼省略.........
示例3: TestUpdateUserPasswordFailsWithPaswordlessUser
func (suite *AccountsTestSuite) TestUpdateUserPasswordFailsWithPaswordlessUser() {
var (
testOauthUser *oauth.User
testUser *accounts.User
testAccessToken *oauth.AccessToken
err error
)
// Insert a test user
testOauthUser, err = suite.service.GetOauthService().CreateUser(
roles.User,
"[email protected]",
"", // empty 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: "some_new_password",
})
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())
}
// And serve the request
w := httptest.NewRecorder()
suite.router.ServeHTTP(w, r)
// Check that the mock object expectations were met
suite.assertMockExpectations()
// Check the response
testutil.TestResponseForError(suite.T(), w, oauth.ErrUserPasswordNotSet.Error(), 400)
}
示例4: TestConfirmInvitation
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,
"[email protected]",
"", // 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: "test_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)
}
示例5: TestUpdateUserPassword
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,
"[email protected]",
"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: "test_password",
NewPassword: "some_new_password",
})
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(), "[email protected]", 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{
//.........這裏部分代碼省略.........