github.com/resonatecoop/id@v1.1.0-43/oauth/client_test.go (about)

     1  package oauth_test
     2  
     3  import (
     4  	"github.com/resonatecoop/id/oauth"
     5  	"github.com/resonatecoop/user-api/model"
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  func (suite *OauthTestSuite) TestFindClientByClientID() {
    10  	var (
    11  		client *model.Client
    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 *model.Client
    41  		err    error
    42  	)
    43  
    44  	// We try to insert a non unique 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  		"",
    52  	)
    53  
    54  	// Client object should be nil
    55  	assert.Nil(suite.T(), client)
    56  
    57  	// Correct error should be returned
    58  	if assert.NotNil(suite.T(), err) {
    59  		assert.Equal(suite.T(), oauth.ErrClientIDTaken, err)
    60  	}
    61  
    62  	// We try to insert a unique client
    63  	client, err = suite.service.CreateClient(
    64  		"test_client_3",           // client ID
    65  		"test_secret",             // secret
    66  		"https://www.example.com", // redirect URI
    67  		"",
    68  		"",
    69  		"",
    70  	)
    71  
    72  	// Error should be nil
    73  	assert.Nil(suite.T(), err)
    74  
    75  	// Correct client object should be returned
    76  	if assert.NotNil(suite.T(), client) {
    77  		assert.Equal(suite.T(), "test_client_3", client.Key)
    78  	}
    79  }
    80  
    81  func (suite *OauthTestSuite) TestAuthClient() {
    82  	var (
    83  		client *model.Client
    84  		err    error
    85  	)
    86  
    87  	// When we try to authenticate with a bogus client ID
    88  	client, err = suite.service.AuthClient("bogus", "test_secret")
    89  
    90  	// Client object should be nil
    91  	assert.Nil(suite.T(), client)
    92  
    93  	// Correct error should be returned
    94  	if assert.NotNil(suite.T(), err) {
    95  		assert.Equal(suite.T(), oauth.ErrClientNotFound, err)
    96  	}
    97  
    98  	// When we try to authenticate with an invalid secret
    99  	client, err = suite.service.AuthClient("test_client_1", "bogus")
   100  
   101  	// Client object should be nil
   102  	assert.Nil(suite.T(), client)
   103  
   104  	// Correct error should be returned
   105  	if assert.NotNil(suite.T(), err) {
   106  		assert.Equal(suite.T(), oauth.ErrInvalidClientSecret, err)
   107  	}
   108  
   109  	// When we try to authenticate with valid client ID and secret
   110  	client, err = suite.service.AuthClient("test_client_1", "test_secret")
   111  
   112  	// Error should be nil
   113  	assert.Nil(suite.T(), err)
   114  
   115  	// Correct client object should be returned
   116  	if assert.NotNil(suite.T(), client) {
   117  		assert.Equal(suite.T(), "test_client_1", client.Key)
   118  	}
   119  }