func TestProvider(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 = 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( googleKeyID, "123435467890", googleKeyName, "Mathew", googleKeyEmail, "*****@*****.**", googleKeyAvatarUrl, "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, "*****@*****.**", user.Email()) 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 TestNewUser(t *testing.T) { testProvider := new(test.TestProvider) testProvider.On("Name").Return("providerName") data := objx.MSI( instagramKeyID, "123435467890", instagramKeyName, "Raquel", instagramKeyNickname, "maggit", instagramKeyAvatarUrl, "http://instagram.com/") creds := &common.Credentials{Map: objx.MSI(oauth2.OAuth2KeyAccessToken, "ABC12345")} user := NewUser(data, creds, testProvider) if assert.NotNil(t, user) { assert.Equal(t, data, user.Data()) assert.Equal(t, "Raquel", user.Name()) assert.Equal(t, "maggit", user.Nickname()) assert.Equal(t, "http://instagram.com/", user.AvatarURL()) // check provider credentials creds := user.ProviderCredentials()[testProvider.Name()] if assert.NotNil(t, creds) { assert.Equal(t, "ABC12345", creds.Get(oauth2.OAuth2KeyAccessToken).Str()) assert.Equal(t, "123435467890", creds.Get(common.CredentialsKeyID).Str()) } } mock.AssertExpectationsForObjects(t, testProvider.Mock) }
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 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") }