Skip to content

Commit

Permalink
Feat: Add e2e tests (#14)
Browse files Browse the repository at this point in the history
* Feat: Add e2e tests

* Apply suggestions from code review

Co-Authored-By: Sanketh Katta <sanketh@smartcar.com>
  • Loading branch information
adolfoportilla and sankethkatta committed Nov 1, 2019
1 parent 513702e commit 5935848
Show file tree
Hide file tree
Showing 5 changed files with 661 additions and 1 deletion.
99 changes: 99 additions & 0 deletions authentication_e2e_test.go
@@ -0,0 +1,99 @@
package smartcar

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"gopkg.in/h2non/gock.v1"
)

type AuthE2ETestSuite struct {
suite.Suite
auth auth
mockAge, mockRequestID string
mockUnitSystem UnitSystem
responseHeaders ResponseHeaders
}

func (s *AuthE2ETestSuite) SetupTest() {
s.auth = auth{
clientID: "client-id",
clientSecret: "client-secret",
redirectURI: "redirect-uri",
scope: []string{"read_odometer", "read_location"},
testMode: false,
sC: newBackend(),
}
}

func (s *AuthE2ETestSuite) TearDownTestSuite() {
gock.Off()
}

func mockAuthAPI(url, id, secret string, response interface{}) {
gock.New(url).
MatchHeader("Authorization", buildBasicAuthorization(id, secret)).
MatchType("x-www-form-urlencoded").
Reply(200).
JSON(response)
}

func (s *AuthE2ETestSuite) TestExchangeCodeE2E() {
mockAccess := "access"
mockRefresh := "refresh"
mockExpiresIn := 7200
expectedResponse := &Token{
Access: mockAccess,
ExpiresIn: mockExpiresIn,
Refresh: mockRefresh,
}
mockResponse := map[string]interface{}{
"access_token": mockAccess,
"token_type": "Bearer",
"expires_in": mockExpiresIn,
"refresh_token": mockRefresh,
}
mockAuthAPI(exchangeURL, s.auth.clientID, s.auth.clientSecret, mockResponse)

res, err := s.auth.ExchangeCode(context.TODO(), &ExchangeCodeParams{})

assert.Nil(s.T(), err)
assert.Equal(s.T(), expectedResponse.Access, res.Access)
assert.Equal(s.T(), expectedResponse.Refresh, res.Refresh)
assert.Equal(s.T(), expectedResponse.ExpiresIn, res.ExpiresIn)
assert.NotEmpty(s.T(), res.AccessExpiry)
assert.NotEmpty(s.T(), res.RefreshExpiry)
}

func (s *AuthE2ETestSuite) TestGetExchangeRefreshTokenE2E() {
mockAccess := "access"
mockRefresh := "refresh"
mockExpiresIn := 7200
expectedResponse := &Token{
Access: mockAccess,
ExpiresIn: mockExpiresIn,
Refresh: mockRefresh,
}
mockResponse := map[string]interface{}{
"access_token": mockAccess,
"token_type": "Bearer",
"expires_in": mockExpiresIn,
"refresh_token": mockRefresh,
}
mockAuthAPI(exchangeURL, s.auth.clientID, s.auth.clientSecret, mockResponse)

res, err := s.auth.ExchangeRefreshToken(context.TODO(), &ExchangeRefreshTokenParams{})

assert.Nil(s.T(), err)
assert.Equal(s.T(), expectedResponse.Access, res.Access)
assert.Equal(s.T(), expectedResponse.Refresh, res.Refresh)
assert.Equal(s.T(), expectedResponse.ExpiresIn, res.ExpiresIn)
assert.NotEmpty(s.T(), res.AccessExpiry)
assert.NotEmpty(s.T(), res.RefreshExpiry)
}

func TestAuthE2ETestSuite(t *testing.T) {
suite.Run(t, new(AuthE2ETestSuite))
}
1 change: 0 additions & 1 deletion go.mod
Expand Up @@ -17,7 +17,6 @@ require (
github.com/stretchr/testify v1.4.0
golang.org/x/lint v0.0.0-20190909230951-414d861bb4ac // indirect
golang.org/x/sys v0.0.0-20190922100055-0a153f010e69 // indirect
golang.org/x/text v0.3.2 // indirect
golang.org/x/tools v0.0.0-20190923213902-fe7d98e288ab // indirect
gopkg.in/h2non/gock.v1 v1.0.15
)

0 comments on commit 5935848

Please sign in to comment.