Пример #1
0
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/")

	}

}
Пример #2
0
func TestOAuth2Provider_Non200Response(t *testing.T) {

	config := &common.Config{
		Map: objx.MSI(
			OAuth2KeyRedirectUrl, OAuth2KeyRedirectUrl,
			OAuth2KeyScope, OAuth2KeyScope,
			OAuth2KeyClientID, OAuth2KeyClientID,
			OAuth2KeySecret, OAuth2KeySecret,
			OAuth2KeyAuthURL, OAuth2KeyAuthURL,
			OAuth2KeyTokenURL, OAuth2KeyTokenURL)}

	testTripperFactory := new(test.TestTripperFactory)
	testTripper := new(test.TestTripper)
	testProvider := new(test.TestProvider)

	testResponse := new(http.Response)
	testResponse.Header = make(http.Header)
	testResponse.Header.Set("Content-Type", "text/plain")
	testResponse.StatusCode = 401
	testResponse.Body = ioutil.NopCloser(strings.NewReader("No mate"))

	testTripperFactory.On("NewTripper", common.EmptyCredentials, mock.Anything).Return(testTripper, nil)
	testTripper.On("RoundTrip", mock.Anything).Return(testResponse, nil)

	data := objx.MSI(OAuth2KeyCode, []string{"123"})
	_, err := CompleteAuth(testTripperFactory, data, config, testProvider)

	if assert.Error(t, err) {
		assert.IsType(t, &common.AuthServerError{}, err)
	}

	mock.AssertExpectationsForObjects(t, testTripperFactory.Mock, testTripper.Mock, testProvider.Mock)

}
Пример #3
0
func TestGetUser(t *testing.T) {

	g := New("clientID", "secret", "http://myapp.com/")
	creds := &common.Credentials{Map: objx.MSI()}

	testTripperFactory := new(test.TestTripperFactory)
	testTripper := new(test.TestTripper)
	testTripperFactory.On("NewTripper", mock.Anything, g).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":"*****@*****.**","picture":"http://myface.com/","blog":"http://blog.com/"}`))
	testTripper.On("RoundTrip", mock.Anything).Return(testResponse, nil)

	g.tripperFactory = testTripperFactory

	user, err := g.GetUser(creds)

	if assert.NoError(t, err) && assert.NotNil(t, user) {

		assert.Equal(t, user.Name(), "their-name")
		assert.Equal(t, user.AuthCode(), "") // doesn't come from google
		assert.Equal(t, user.Email(), "*****@*****.**")
		assert.Equal(t, user.AvatarURL(), "http://myface.com/")
		assert.Equal(t, user.Data()["blog"], "http://blog.com/")

		googleCreds := user.ProviderCredentials()[googleName]
		if assert.NotNil(t, googleCreds) {
			assert.Equal(t, "uniqueid", googleCreds.Get(common.CredentialsKeyID).Str())
		}

	}

}
Пример #4
0
func TestGetUser(t *testing.T) {

	g := New("clientID", "secret", "http://myapp.com/")
	creds := &common.Credentials{Map: objx.MSI()}

	testTripperFactory := new(test.TestTripperFactory)
	testTripper := new(test.TestTripper)
	testTripperFactory.On("NewTripper", mock.Anything, g).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(`{"full_name":"their-name","id":"uniqueid","username":"******","avatar_url":"http://myface.com/","online":true}`))
	testTripper.On("RoundTrip", mock.Anything).Return(testResponse, nil)

	g.tripperFactory = testTripperFactory

	user, err := g.GetUser(creds)

	if assert.NoError(t, err) && assert.NotNil(t, user) {
		assert.Equal(t, user.Name(), "their-name")
		assert.Equal(t, user.AuthCode(), "")
		assert.Equal(t, user.Nickname(), "loginname")
		assert.Equal(t, user.Email(), "") // doesn't come from soundcloud
		assert.Equal(t, user.AvatarURL(), "http://myface.com/")
		assert.Equal(t, user.Data()["online"], true)
	}

}
Пример #5
0
func TestGetUser(t *testing.T) {

	g := New("clientID", "secret", "http://myapp.com/")
	creds := &common.Credentials{Map: objx.MSI()}

	testTripperFactory := new(test.TestTripperFactory)
	testTripper := new(test.TestTripper)
	testTripperFactory.On("NewTripper", mock.Anything, g).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(`{
  "id": "631819186",
  "name": "their-name",
  "first_name": "Mat",
  "last_name": "Ryer",
  "link": "https://www.facebook.com/matryer",
  "username": "******",
  "bio": "http://www.stretchr.com/",
  "gender": "male",
  "email": "*****@*****.**",
  "timezone": -6,
  "locale": "en_GB",
  "verified": true,
  "updated_time": "2013-10-03T19:55:28+0000",
  "picture": {
    "url": "http://www.myface.com/"
  }
  }`))
	testTripper.On("RoundTrip", mock.Anything).Return(testResponse, nil)

	g.tripperFactory = testTripperFactory

	user, err := g.GetUser(creds)

	if assert.NoError(t, err) && assert.NotNil(t, user) {

		assert.Equal(t, user.Name(), "their-name")
		assert.Equal(t, user.AuthCode(), "") // doesn't come from github
		assert.Equal(t, user.Nickname(), "loginname")
		assert.Equal(t, user.Email(), "*****@*****.**")
		assert.Equal(t, user.AvatarURL(), "http://www.myface.com/")
		assert.Equal(t, user.Data()["link"], "https://www.facebook.com/matryer")

		facebookCreds := user.ProviderCredentials()[facebookName]
		if assert.NotNil(t, facebookCreds) {
			assert.Equal(t, "631819186", facebookCreds.Get(common.CredentialsKeyID).Str())
		}

	}

}
Пример #6
0
func TestGetUser(t *testing.T) {

	g := New("clientID", "secret", "http://myapp.com/")
	creds := &common.Credentials{Map: objx.MSI()}

	testTripperFactory := new(test.TestTripperFactory)
	testTripper := new(test.TestTripper)
	testTripperFactory.On("NewTripper", mock.Anything, g).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(`{
  "meta":  {
    "code": 200
  },
  "data":  {
    "username": "******",
    "bio": "Programer who loves golang",
    "website": "http://website.com",
    "profile_picture": "http://myinsta.com",
    "full_name": "Raquel H",
    "counts":  {
      "media": 1232323,
      "followed_by": 123,
      "follows": 123
    },
    "id": "12345678"
  }
}`))
	testTripper.On("RoundTrip", mock.Anything).Return(testResponse, nil)

	g.tripperFactory = testTripperFactory

	user, err := g.GetUser(creds)

	if assert.NoError(t, err) && assert.NotNil(t, user) {

		assert.Equal(t, user.Name(), "Raquel H")
		assert.Equal(t, user.AuthCode(), "") // doesn't come from instagram
		assert.Equal(t, user.Nickname(), "maggit")
		assert.Equal(t, user.AvatarURL(), "http://myinsta.com")
		assert.Equal(t, user.Data()["website"], "http://website.com")

		instagramCreds := user.ProviderCredentials()[instagramName]
		if assert.NotNil(t, instagramCreds) {
			assert.Equal(t, "uniqueid", instagramCreds.Get(common.CredentialsKeyID).Str())
		}

	}

}
Пример #7
0
func TestGetUser(t *testing.T) {

	g := New("clientID", "secret", "http://myapp.com/")
	creds := &common.Credentials{Map: objx.MSI()}

	testTripperFactory := new(test.TestTripperFactory)
	testTripper := new(test.TestTripper)
	testTripperFactory.On("NewTripper", mock.Anything, g).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(`{
  "display_name":"JMWizzler",
  "email":"*****@*****.**",
  "external_urls":{
    "spotify":"https://open.spotify.com/user/wizzler"
  },
  "href":"https://api.spotify.com/v1/users/wizzler",
  "id":"wizzler",
  "images":[{
    "height":null,
    "url":"https://fbcdn.example.com/2330_n.jpg",
    "width":null
  }],
  "type":"user",
  "uri":"spotify:user:wizzler"
}`))
	testTripper.On("RoundTrip", mock.Anything).Return(testResponse, nil)

	g.tripperFactory = testTripperFactory

	user, err := g.GetUser(creds)

	if assert.NoError(t, err) && assert.NotNil(t, user) {

		assert.Equal(t, user.Name(), "JMWizzler")
		assert.Equal(t, user.AuthCode(), "") // doesn't come from spotify
		assert.Equal(t, user.Nickname(), "") // doesn't come from spotify
		assert.Equal(t, user.Email(), "*****@*****.**")
		assert.Equal(t, user.AvatarURL(), "https://fbcdn.example.com/2330_n.jpg")
		assert.Equal(t, user.Data()["href"], "https://api.spotify.com/v1/users/wizzler")

		spotifyCreds := user.ProviderCredentials()[spotifyName]
		if assert.NotNil(t, spotifyCreds) {
			assert.Equal(t, "wizzler", spotifyCreds.Get(common.CredentialsKeyID).Str())
		}

	}

}
Пример #8
0
func TestOAuth2Provider_CompleteAuth_JSON(t *testing.T) {

	config := &common.Config{
		Map: objx.MSI(
			OAuth2KeyRedirectUrl, OAuth2KeyRedirectUrl,
			OAuth2KeyScope, OAuth2KeyScope,
			OAuth2KeyClientID, OAuth2KeyClientID,
			OAuth2KeySecret, OAuth2KeySecret,
			OAuth2KeyAuthURL, OAuth2KeyAuthURL,
			OAuth2KeyTokenURL, OAuth2KeyTokenURL)}

	testTripperFactory := new(test.TestTripperFactory)
	testTripper := new(test.TestTripper)
	testProvider := new(test.TestProvider)

	creds := new(common.Credentials)

	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(`{"expires_in":20,"access_token":"ACCESSTOKEN","refresh_token":"REFRESHTOKEN"}`))

	testTripperFactory.On("NewTripper", common.EmptyCredentials, mock.Anything).Return(testTripper, nil)
	testTripper.On("RoundTrip", mock.Anything).Return(testResponse, nil)

	data := objx.MSI(OAuth2KeyCode, []string{"123"})
	creds, err := CompleteAuth(testTripperFactory, data, config, testProvider)

	if assert.NoError(t, err) {
		if assert.NotNil(t, creds, "Creds should be returned") {

			assert.Equal(t, creds.Get(OAuth2KeyAccessToken).Str(), "ACCESSTOKEN")
			assert.Equal(t, creds.Get(OAuth2KeyRefreshToken).Str(), "REFRESHTOKEN")
			assert.Equal(t, creds.Get(OAuth2KeyExpiresIn).Data().(time.Duration), 20000000000)

		}
	}

	mock.AssertExpectationsForObjects(t, testTripperFactory.Mock, testTripper.Mock, testProvider.Mock)

}
Пример #9
0
func TestGetUser(t *testing.T) {

	g := New("clientID", "secret", "http://myapp.com/")
	creds := &common.Credentials{Map: objx.MSI()}

	testTripperFactory := new(test.TestTripperFactory)
	testTripper := new(test.TestTripper)
	testTripperFactory.On("NewTripper", mock.Anything, g).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(`{
  "first_name": "Raquel",
  "last_name": "Hernandez",
  "email": "*****@*****.**",
  "profile_picture": "http://myuberpic.com",
  "promo_code": "somecode"
  "uuid": "imoqueid"
}`))
	testTripper.On("RoundTrip", mock.Anything).Return(testResponse, nil)

	g.tripperFactory = testTripperFactory

	user, err := g.GetUser(creds)

	if assert.NoError(t, err) && assert.NotNil(t, user) {

		assert.Equal(t, user.Name(), "Raquel")
		assert.Equal(t, user.AuthCode(), "") // doesn't come from uber
		assert.Equal(t, user.Nickname(), "*****@*****.**")
		assert.Equal(t, user.AvatarURL(), "http://myuberpic.com")
		assert.Equal(t, user.Data()["promo_code"], "somecode")

		uberCreds := user.ProviderCredentials()[uberName]
		if assert.NotNil(t, uberCreds) {
			assert.Equal(t, "uniqueid", uberCreds.Get(common.CredentialsKeyID).Str())
		}

	}

}
Пример #10
0
func TestOAuth2ProviderGetClient(t *testing.T) {

	testTripperFactory := new(test.TestTripperFactory)
	testTripper := new(test.TestTripper)
	testProvider := new(test.TestProvider)

	creds := new(common.Credentials)

	testTripperFactory.On("NewTripper", creds, mock.Anything).Return(testTripper, nil)

	client, clientErr := GetClient(testTripperFactory, creds, testProvider)

	if assert.NoError(t, clientErr) {
		if assert.NotNil(t, client) {
			assert.Equal(t, client.Transport, testTripper)
		}
	}

	mock.AssertExpectationsForObjects(t, testTripperFactory.Mock, testTripper.Mock, testProvider.Mock)

}