func TestTokenHandler(t *testing.T) { proxyClient, _, server := newTwitterTestServer(testTwitterUserJSON) defer server.Close() // oauth1 Client will use the proxy client's base Transport ctx := context.WithValue(context.Background(), oauth1.HTTPClient, proxyClient) config := &oauth1.Config{} success := func(ctx context.Context, w http.ResponseWriter, req *http.Request) { accessToken, accessSecret, err := oauth1Login.AccessTokenFromContext(ctx) assert.Nil(t, err) assert.Equal(t, testTwitterToken, accessToken) assert.Equal(t, testTwitterTokenSecret, accessSecret) user, err := UserFromContext(ctx) assert.Nil(t, err) assert.Equal(t, expectedUserID, user.ID) assert.Equal(t, "1234", user.IDStr) } handler := TokenHandler(config, ctxh.ContextHandlerFunc(success), assertFailureNotCalled(t)) ts := httptest.NewServer(ctxh.NewHandlerWithContext(ctx, handler)) // POST token to server under test resp, err := http.PostForm(ts.URL, url.Values{accessTokenField: {testTwitterToken}, accessTokenSecretField: {testTwitterTokenSecret}}) assert.Nil(t, err) if assert.NotNil(t, resp) { assert.Equal(t, resp.StatusCode, http.StatusOK) } }
func TestTokenHandler(t *testing.T) { proxyClient, _, server := newDigitsTestServer(testAccountJSON) defer server.Close() // oauth1 Client will use the proxy client's base Transport ctx := context.WithValue(context.Background(), oauth1.HTTPClient, proxyClient) config := &oauth1.Config{} success := func(ctx context.Context, w http.ResponseWriter, req *http.Request) { account, err := AccountFromContext(ctx) assert.Nil(t, err) assert.Equal(t, testDigitsToken, account.AccessToken.Token) assert.Equal(t, testDigitsSecret, account.AccessToken.Secret) assert.Equal(t, "0123456789", account.PhoneNumber) accessToken, accessSecret, err := oauth1Login.AccessTokenFromContext(ctx) assert.Nil(t, err) assert.Equal(t, testDigitsToken, accessToken) assert.Equal(t, testDigitsSecret, accessSecret) } handler := TokenHandler(config, ctxh.ContextHandlerFunc(success), testutils.AssertFailureNotCalled(t)) ts := httptest.NewServer(ctxh.NewHandlerWithContext(ctx, handler)) // POST Digits access token to server under test resp, err := http.PostForm(ts.URL, url.Values{accessTokenField: {testDigitsToken}, accessTokenSecretField: {testDigitsSecret}}) assert.Nil(t, err) if assert.NotNil(t, resp) { assert.Equal(t, resp.StatusCode, http.StatusOK) } }
func TestTokenHandler_Unauthorized(t *testing.T) { proxyClient, server := testutils.UnauthorizedTestServer() defer server.Close() // oauth1 Client will use the proxy client's base Transport ctx := context.WithValue(context.Background(), oauth1.HTTPClient, proxyClient) config := &oauth1.Config{} handler := TokenHandler(config, assertSuccessNotCalled(t), nil) ts := httptest.NewServer(ctxh.NewHandlerWithContext(ctx, handler)) // assert that error occurs indicating the Twitter User could not be confirmed resp, _ := http.PostForm(ts.URL, url.Values{accessTokenField: {testTwitterToken}, accessTokenSecretField: {testTwitterTokenSecret}}) testutils.AssertBodyString(t, resp.Body, ErrUnableToGetTwitterUser.Error()+"\n") }
func TestTokenHandler_ErrorVerifyingToken(t *testing.T) { proxyClient, server := testutils.NewErrorServer("Digits Account Endpoint Down", http.StatusInternalServerError) defer server.Close() // oauth1 Client will use the proxy client's base Transport ctx := context.WithValue(context.Background(), oauth1.HTTPClient, proxyClient) config := &oauth1.Config{} handler := TokenHandler(config, testutils.AssertSuccessNotCalled(t), nil) ts := httptest.NewServer(ctxh.NewHandlerWithContext(ctx, handler)) // assert that error occurs indicating the Digits Account could not be confirmed resp, _ := http.PostForm(ts.URL, url.Values{accessTokenField: {testDigitsToken}, accessTokenSecretField: {testDigitsSecret}}) testutils.AssertBodyString(t, resp.Body, ErrUnableToGetDigitsAccount.Error()+"\n") }
func TestTokenHandler_UnauthorizedPassesError(t *testing.T) { proxyClient, server := testutils.UnauthorizedTestServer() defer server.Close() // oauth1 Client will use the proxy client's base Transport ctx := context.WithValue(context.Background(), oauth1.HTTPClient, proxyClient) config := &oauth1.Config{} failure := func(ctx context.Context, w http.ResponseWriter, req *http.Request) { // assert that error passed through ctx err := gologin.ErrorFromContext(ctx) if assert.Error(t, err) { assert.Equal(t, err, ErrUnableToGetTwitterUser) } } handler := TokenHandler(config, assertSuccessNotCalled(t), ctxh.ContextHandlerFunc(failure)) ts := httptest.NewServer(ctxh.NewHandlerWithContext(ctx, handler)) http.PostForm(ts.URL, url.Values{accessTokenField: {testTwitterToken}, accessTokenSecretField: {testTwitterTokenSecret}}) }