github.com/RichardKnop/go-oauth2-server@v1.0.5-0.20201019163316-d02a401490d0/oauth/authorization_code_test.go (about) 1 package oauth_test 2 3 import ( 4 "github.com/RichardKnop/go-oauth2-server/models" 5 "github.com/stretchr/testify/assert" 6 ) 7 8 func (suite *OauthTestSuite) TestGrantAuthorizationCode() { 9 var ( 10 authorizationCode *models.OauthAuthorizationCode 11 err error 12 codes []*models.OauthAuthorizationCode 13 ) 14 15 // Grant an authorization code 16 authorizationCode, err = suite.service.GrantAuthorizationCode( 17 suite.clients[0], // client 18 suite.users[0], // user 19 3600, // expires in 20 "redirect URI doesn't matter", // redirect URI 21 "scope doesn't matter", // scope 22 ) 23 24 // Error should be Nil 25 assert.Nil(suite.T(), err) 26 27 // Correct authorization code object should be returned 28 if assert.NotNil(suite.T(), authorizationCode) { 29 // Fetch all auth codes 30 models.OauthAuthorizationCodePreload(suite.db).Order("created_at").Find(&codes) 31 32 // There should be just one right now 33 assert.Equal(suite.T(), 1, len(codes)) 34 35 // And the code should match the one returned by the grant method 36 assert.Equal(suite.T(), codes[0].Code, authorizationCode.Code) 37 38 // Client ID should be set 39 assert.True(suite.T(), codes[0].ClientID.Valid) 40 assert.Equal(suite.T(), string(suite.clients[0].ID), codes[0].ClientID.String) 41 42 // User ID should be set 43 assert.True(suite.T(), codes[0].UserID.Valid) 44 assert.Equal(suite.T(), string(suite.users[0].ID), codes[0].UserID.String) 45 } 46 }