...

Source file src/github.com/ory/fosite/access_response_writer_test.go

Documentation: github.com/ory/fosite

     1  /*
     2   * Copyright © 2015-2018 Aeneas Rekkas <aeneas+oss@aeneas.io>
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   *
    16   * @author		Aeneas Rekkas <aeneas+oss@aeneas.io>
    17   * @copyright 	2015-2018 Aeneas Rekkas <aeneas+oss@aeneas.io>
    18   * @license 	Apache-2.0
    19   *
    20   */
    21  
    22  package fosite_test
    23  
    24  import (
    25  	"context"
    26  	"fmt"
    27  	"testing"
    28  
    29  	"github.com/golang/mock/gomock"
    30  	"github.com/stretchr/testify/assert"
    31  	"github.com/stretchr/testify/require"
    32  
    33  	. "github.com/ory/fosite"
    34  	"github.com/ory/fosite/internal"
    35  )
    36  
    37  func TestNewAccessResponse(t *testing.T) {
    38  	ctrl := gomock.NewController(t)
    39  	handler := internal.NewMockTokenEndpointHandler(ctrl)
    40  	defer ctrl.Finish()
    41  
    42  	f := &Fosite{}
    43  	for k, c := range []struct {
    44  		handlers  TokenEndpointHandlers
    45  		mock      func()
    46  		expectErr error
    47  		expect    AccessResponder
    48  	}{
    49  		{
    50  			mock:      func() {},
    51  			handlers:  TokenEndpointHandlers{},
    52  			expectErr: ErrServerError,
    53  		},
    54  		{
    55  			mock: func() {
    56  				handler.EXPECT().PopulateTokenEndpointResponse(gomock.Any(), gomock.Any(), gomock.Any()).Return(ErrServerError)
    57  			},
    58  			handlers:  TokenEndpointHandlers{handler},
    59  			expectErr: ErrServerError,
    60  		},
    61  		{
    62  			mock: func() {
    63  				handler.EXPECT().PopulateTokenEndpointResponse(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil)
    64  			},
    65  			handlers:  TokenEndpointHandlers{handler},
    66  			expectErr: ErrServerError,
    67  		},
    68  		{
    69  			mock: func() {
    70  				handler.EXPECT().PopulateTokenEndpointResponse(gomock.Any(), gomock.Any(), gomock.Any()).Do(func(_ context.Context, _ AccessRequester, resp AccessResponder) {
    71  					resp.SetAccessToken("foo")
    72  				}).Return(nil)
    73  			},
    74  			handlers:  TokenEndpointHandlers{handler},
    75  			expectErr: ErrServerError,
    76  		},
    77  		{
    78  			mock: func() {
    79  				handler.EXPECT().PopulateTokenEndpointResponse(gomock.Any(), gomock.Any(), gomock.Any()).Do(func(_ context.Context, _ AccessRequester, resp AccessResponder) {
    80  					resp.SetAccessToken("foo")
    81  					resp.SetTokenType("bar")
    82  				}).Return(nil)
    83  			},
    84  			handlers: TokenEndpointHandlers{handler},
    85  			expect: &AccessResponse{
    86  				Extra:       map[string]interface{}{},
    87  				AccessToken: "foo",
    88  				TokenType:   "bar",
    89  			},
    90  		},
    91  	} {
    92  		t.Run(fmt.Sprintf("case=%d", k), func(t *testing.T) {
    93  			f.TokenEndpointHandlers = c.handlers
    94  			c.mock()
    95  			ar, err := f.NewAccessResponse(context.TODO(), nil)
    96  
    97  			if c.expectErr != nil {
    98  				assert.EqualError(t, err, c.expectErr.Error())
    99  			} else {
   100  				require.NoError(t, err)
   101  				assert.Equal(t, ar, c.expect)
   102  			}
   103  		})
   104  	}
   105  }
   106  

View as plain text