Updated unit tests to reflect handler wrapper.

This commit is contained in:
Nick Gerakines 2020-01-02 16:29:23 -05:00
parent 2aea9560bc
commit 6823f10821

View file

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"github.com/gorilla/sessions" "github.com/gorilla/sessions"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/writeas/impart"
"github.com/writeas/nerds/store" "github.com/writeas/nerds/store"
"github.com/writeas/writefreely/config" "github.com/writeas/writefreely/config"
"net/http" "net/http"
@ -139,7 +140,7 @@ func TestViewOauthInit(t *testing.T) {
Config: app.Config(), Config: app.Config(),
DB: app.DB(), DB: app.DB(),
Store: app.SessionStore(), Store: app.SessionStore(),
oauthClient:writeAsOauthClient{ oauthClient: writeAsOauthClient{
ClientID: app.Config().WriteAsOauth.ClientID, ClientID: app.Config().WriteAsOauth.ClientID,
ClientSecret: app.Config().WriteAsOauth.ClientSecret, ClientSecret: app.Config().WriteAsOauth.ClientSecret,
ExchangeLocation: app.Config().WriteAsOauth.TokenLocation, ExchangeLocation: app.Config().WriteAsOauth.TokenLocation,
@ -152,9 +153,13 @@ func TestViewOauthInit(t *testing.T) {
req, err := http.NewRequest("GET", "/oauth/client", nil) req, err := http.NewRequest("GET", "/oauth/client", nil)
assert.NoError(t, err) assert.NoError(t, err)
rr := httptest.NewRecorder() rr := httptest.NewRecorder()
h.viewOauthInit(nil, rr, req) err = h.viewOauthInit(nil, rr, req)
assert.Equal(t, http.StatusTemporaryRedirect, rr.Code) assert.NotNil(t, err)
locURI, err := url.Parse(rr.Header().Get("Location")) httpErr, ok := err.(impart.HTTPError)
assert.True(t, ok)
assert.Equal(t, http.StatusTemporaryRedirect, httpErr.Status)
assert.NotEmpty(t, httpErr.Message)
locURI, err := url.Parse(httpErr.Message)
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, "/oauth/login", locURI.Path) assert.Equal(t, "/oauth/login", locURI.Path)
assert.Equal(t, "development", locURI.Query().Get("client_id")) assert.Equal(t, "development", locURI.Query().Get("client_id"))
@ -177,7 +182,7 @@ func TestViewOauthInit(t *testing.T) {
Config: app.Config(), Config: app.Config(),
DB: app.DB(), DB: app.DB(),
Store: app.SessionStore(), Store: app.SessionStore(),
oauthClient:writeAsOauthClient{ oauthClient: writeAsOauthClient{
ClientID: app.Config().WriteAsOauth.ClientID, ClientID: app.Config().WriteAsOauth.ClientID,
ClientSecret: app.Config().WriteAsOauth.ClientSecret, ClientSecret: app.Config().WriteAsOauth.ClientSecret,
ExchangeLocation: app.Config().WriteAsOauth.TokenLocation, ExchangeLocation: app.Config().WriteAsOauth.TokenLocation,
@ -190,10 +195,12 @@ func TestViewOauthInit(t *testing.T) {
req, err := http.NewRequest("GET", "/oauth/client", nil) req, err := http.NewRequest("GET", "/oauth/client", nil)
assert.NoError(t, err) assert.NoError(t, err)
rr := httptest.NewRecorder() rr := httptest.NewRecorder()
h.viewOauthInit(nil, rr, req) err = h.viewOauthInit(nil, rr, req)
assert.Equal(t, http.StatusInternalServerError, rr.Code) httpErr, ok := err.(impart.HTTPError)
expected := `{"error":"could not prepare oauth redirect url"}` + "\n" assert.True(t, ok)
assert.Equal(t, expected, rr.Body.String()) assert.NotEmpty(t, httpErr.Message)
assert.Equal(t, http.StatusInternalServerError, httpErr.Status)
assert.Equal(t, "could not prepare oauth redirect url", httpErr.Message)
}) })
} }