github.com/wanliu/go-oauth2-server@v0.0.0-20180817021415-f928fa1580df/oauth/client_test.go (about)

     1  package oauth_test
     2  
     3  import (
     4  	"github.com/stretchr/testify/assert"
     5  	"github.com/wanliu/go-oauth2-server/models"
     6  	"github.com/wanliu/go-oauth2-server/oauth"
     7  )
     8  
     9  func (suite *OauthTestSuite) TestFindClientByClientID() {
    10  	var (
    11  		client *models.OauthClient
    12  		err    error
    13  	)
    14  
    15  	// When we try to find a client with a bogus client ID
    16  	client, err = suite.service.FindClientByClientID("bogus")
    17  
    18  	// Client object should be nil
    19  	assert.Nil(suite.T(), client)
    20  
    21  	// Correct error should be returned
    22  	if assert.NotNil(suite.T(), err) {
    23  		assert.Equal(suite.T(), oauth.ErrClientNotFound, err)
    24  	}
    25  
    26  	// When we try to find a client with a valid cliend ID
    27  	client, err = suite.service.FindClientByClientID("test_client_1")
    28  
    29  	// Error should be nil
    30  	assert.Nil(suite.T(), err)
    31  
    32  	// Correct client object should be returned
    33  	if assert.NotNil(suite.T(), client) {
    34  		assert.Equal(suite.T(), "test_client_1", client.Key)
    35  	}
    36  }
    37  
    38  func (suite *OauthTestSuite) TestCreateClient() {
    39  	var (
    40  		client *models.OauthClient
    41  		err    error
    42  	)
    43  
    44  	// We try to insert a non uniqie client
    45  	client, err = suite.service.CreateClient(
    46  		"test_client_1",           // client ID
    47  		"test_secret",             // secret
    48  		"https://www.example.com", // redirect URI
    49  	)
    50  
    51  	// Client object should be nil
    52  	assert.Nil(suite.T(), client)
    53  
    54  	// Correct error should be returned
    55  	if assert.NotNil(suite.T(), err) {
    56  		assert.Equal(suite.T(), oauth.ErrClientIDTaken, err)
    57  	}
    58  
    59  	// We try to insert a unique client
    60  	client, err = suite.service.CreateClient(
    61  		"test_client_3",           // client ID
    62  		"test_secret",             // secret
    63  		"https://www.example.com", // redirect URI
    64  	)
    65  
    66  	// Error should be nil
    67  	assert.Nil(suite.T(), err)
    68  
    69  	// Correct client object should be returned
    70  	if assert.NotNil(suite.T(), client) {
    71  		assert.Equal(suite.T(), "test_client_3", client.Key)
    72  	}
    73  }
    74  
    75  func (suite *OauthTestSuite) TestAuthClient() {
    76  	var (
    77  		client *models.OauthClient
    78  		err    error
    79  	)
    80  
    81  	// When we try to authenticate with a bogus client ID
    82  	client, err = suite.service.AuthClient("bogus", "test_secret")
    83  
    84  	// Client object should be nil
    85  	assert.Nil(suite.T(), client)
    86  
    87  	// Correct error should be returned
    88  	if assert.NotNil(suite.T(), err) {
    89  		assert.Equal(suite.T(), oauth.ErrClientNotFound, err)
    90  	}
    91  
    92  	// When we try to authenticate with an invalid secret
    93  	client, err = suite.service.AuthClient("test_client_1", "bogus")
    94  
    95  	// Client object should be nil
    96  	assert.Nil(suite.T(), client)
    97  
    98  	// Correct error should be returned
    99  	if assert.NotNil(suite.T(), err) {
   100  		assert.Equal(suite.T(), oauth.ErrInvalidClientSecret, err)
   101  	}
   102  
   103  	// When we try to authenticate with valid client ID and secret
   104  	client, err = suite.service.AuthClient("test_client_1", "test_secret")
   105  
   106  	// Error should be nil
   107  	assert.Nil(suite.T(), err)
   108  
   109  	// Correct client object should be returned
   110  	if assert.NotNil(suite.T(), client) {
   111  		assert.Equal(suite.T(), "test_client_1", client.Key)
   112  	}
   113  }