Exemplo n.º 1
0
func TestNewAuthorizationCode(t *testing.T) {
	var (
		client            = &models.OauthClient{Model: gorm.Model{ID: 1}}
		user              = &models.OauthUser{Model: gorm.Model{ID: 2}}
		authorizationCode *models.OauthAuthorizationCode
		v                 driver.Value
		err               error
	)

	// When user object is not nil
	authorizationCode = models.NewOauthAuthorizationCode(
		client, // client
		user,   // user
		3600,   // expires in
		"redirect URI doesn't matter", // redirect URI
		"scope doesn't matter",        // scope
	)

	// authorizationCode.ClientID.Valid should be true
	assert.True(t, authorizationCode.ClientID.Valid)

	// authorizationCode.ClientID.Value() should return the object id, in this case int64(1)
	v, err = authorizationCode.ClientID.Value()
	assert.Nil(t, err)
	assert.Equal(t, int64(1), v)

	// authorizationCode.UserID.Valid should be true
	assert.True(t, authorizationCode.UserID.Valid)

	// authorizationCode.UserID.Value() should return the object id, in this case int64(2)
	v, err = authorizationCode.UserID.Value()
	assert.Nil(t, err)
	assert.Equal(t, int64(2), v)
}
// GrantAuthorizationCode grants a new authorization code
func (s *Service) GrantAuthorizationCode(client *models.OauthClient, user *models.OauthUser, expiresIn int, redirectURI, scope string) (*models.OauthAuthorizationCode, error) {
	// Create a new authorization code
	authorizationCode := models.NewOauthAuthorizationCode(client, user, expiresIn, redirectURI, scope)
	if err := s.db.Create(authorizationCode).Error; err != nil {
		return nil, err
	}
	authorizationCode.Client = client
	authorizationCode.User = user

	return authorizationCode, nil
}