func TestGetUser(t *testing.T) { testProvider := new(test.TestProvider) creds := new(common.Credentials) testTripperFactory := new(test.TestTripperFactory) testTripper := new(test.TestTripper) testTripperFactory.On("NewTripper", creds, testProvider).Return(testTripper, nil) testResponse := new(http.Response) testResponse.Header = make(http.Header) testResponse.Header.Set("Content-Type", "application/json") testResponse.StatusCode = 200 testResponse.Body = ioutil.NopCloser(strings.NewReader(`{"name":"their-name","id":"uniqueid","login":"******","email":"*****@*****.**","avatar_url":"http://myface.com/","blog":"http://blog.com/"}`)) testTripper.On("RoundTrip", mock.Anything).Return(testResponse, nil) client := &http.Client{Transport: testTripper} testProvider.On("GetClient", creds).Return(client, nil) data, err := Get(testProvider, creds, "endpoint") if assert.NoError(t, err) && assert.NotNil(t, data) { assert.Equal(t, data["name"], "their-name") assert.Equal(t, data["id"], "uniqueid") assert.Equal(t, data["login"], "loginname") assert.Equal(t, data["email"], "*****@*****.**") assert.Equal(t, data["avatar_url"], "http://myface.com/") assert.Equal(t, data["blog"], "http://blog.com/") } }
func TestNewUser(t *testing.T) { testProvider := new(test.TestProvider) testProvider.On("Name").Return("providerName") data := objx.MSI( soundcloudKeyID, "123435467890", soundcloudKeyName, "Mathew", soundcloudKeyNickname, "mathew_testington", soundcloudKeyAvatarUrl, "http://myface.com/") creds := &common.Credentials{Map: objx.MSI(oauth2.OAuth2KeyAccessToken, "ABC123")} user := NewUser(data, creds, testProvider) if assert.NotNil(t, user) { assert.Equal(t, data, user.Data()) assert.Equal(t, "Mathew", user.Name()) assert.Equal(t, "mathew_testington", user.Nickname()) assert.Equal(t, "http://myface.com/", user.AvatarURL()) // check provider credentials creds := user.ProviderCredentials()[testProvider.Name()] if assert.NotNil(t, creds) { assert.Equal(t, "ABC123", creds.Get(oauth2.OAuth2KeyAccessToken).Str()) assert.Equal(t, "123435467890", creds.Get(common.CredentialsKeyID).Str()) } } mock.AssertExpectationsForObjects(t, testProvider.Mock) }
func TestProviderListProvider(t *testing.T) { prov1 := new(test.TestProvider) prov2 := new(test.TestProvider) prov1.On("Name").Return("prov1") prov2.On("Name").Return("prov2") // build a list list := WithProviders(prov1, prov2) returnedProv, err := list.Provider("prov1") if assert.NoError(t, err) { assert.Equal(t, returnedProv, prov1) } // check nonsense name returnedProv, err = list.Provider("no such provider") if assert.Nil(t, returnedProv) { assert.IsType(t, &common.MissingProviderError{}, err, "MissingProviderError expected") } mock.AssertExpectationsForObjects(t, prov1.Mock, prov2.Mock) }
func TestNewUser(t *testing.T) { testProvider := new(test.TestProvider) testProvider.On("Name").Return("providerName") data := objx.MSI( herokuKeyID, "123435467890", herokuKeyEmail, "*****@*****.**") creds := &common.Credentials{Map: objx.MSI(oauth2.OAuth2KeyAccessToken, "ABC123")} user := NewUser(data, creds, testProvider) if assert.NotNil(t, user) { assert.Equal(t, data, user.Data()) assert.Equal(t, "*****@*****.**", user.Email()) // check provider credentials creds := user.ProviderCredentials()[testProvider.Name()] if assert.NotNil(t, creds) { assert.Equal(t, "ABC123", creds.Get(oauth2.OAuth2KeyAccessToken).Str()) assert.Equal(t, "123435467890", creds.Get(common.CredentialsKeyID).Str()) } } mock.AssertExpectationsForObjects(t, testProvider.Mock) }
func TestProviderPublicData(t *testing.T) { provider := new(test.TestProvider) provider.On("Name").Return("TestName") provider.On("DisplayName").Return("TestDisplayName") publicData, _ := ProviderPublicData(provider, objx.MSI("loginpathFormat", "~auth/%s/login")) publicDataMap := publicData.(map[string]interface{}) assert.Equal(t, publicDataMap["name"], "TestName") assert.Equal(t, publicDataMap["display"], "TestDisplayName") assert.Equal(t, publicDataMap["loginpath"], "~auth/TestName/login") }