github.com/resonatecoop/id@v1.1.0-43/oauth/grant_type_client_credentials_test.go (about) 1 package oauth_test 2 3 import ( 4 "context" 5 "net/http" 6 "net/http/httptest" 7 "net/url" 8 9 "github.com/resonatecoop/id/oauth" 10 "github.com/resonatecoop/id/oauth/tokentypes" 11 testutil "github.com/resonatecoop/id/test-util" 12 "github.com/resonatecoop/user-api/model" 13 "github.com/stretchr/testify/assert" 14 ) 15 16 func (suite *OauthTestSuite) TestClientCredentialsGrant() { 17 // Prepare a request 18 r, err := http.NewRequest("POST", "http://1.2.3.4/v1/oauth/tokens", nil) 19 assert.NoError(suite.T(), err, "Request setup should not get an error") 20 r.SetBasicAuth("test_client_1", "test_secret") 21 r.PostForm = url.Values{ 22 "grant_type": {"client_credentials"}, 23 "scope": {"read_write"}, 24 } 25 26 // Serve the request 27 w := httptest.NewRecorder() 28 suite.router.ServeHTTP(w, r) 29 30 // Fetch data 31 ctx := context.Background() 32 accessToken := new(model.AccessToken) 33 34 err = suite.db.NewSelect(). 35 Model(accessToken). 36 Limit(1). 37 Scan(ctx) 38 39 // A record is found 40 assert.Nil(suite.T(), err) 41 42 // Check the response 43 expected := &oauth.AccessTokenResponse{ 44 AccessToken: accessToken.Token, 45 ExpiresIn: 3600, 46 TokenType: tokentypes.Bearer, 47 Scope: "read_write", 48 } 49 testutil.TestResponseObject(suite.T(), w, expected, 200) 50 51 // Client credentials grant does not produce refresh token 52 err = suite.db.NewSelect(). 53 Model(new(model.RefreshToken)). 54 Limit(1). 55 Scan(ctx) 56 57 // Error raised as no record found 58 assert.NotNil(suite.T(), err) 59 }