func TestAuthenticateTokenWithExpiredToken(t *testing.T) {

	r, err := http.NewRequest("", "", nil)
	if err != nil {
		t.Error(err)
	}

	c := &Context{auth.NewAuthContext(&MockTokenStore{IsStored: true}), nil, nil}

	refreshedToken, err := c.RefreshToken()
	if err != nil {
		t.Error(err)
	}

	r.Header.Set("Authorization", "BEARER:"+refreshedToken.SignedToken)
	w := httptest.NewRecorder()

	AuthenticateToken(c, func(c *Context, w http.ResponseWriter, r *http.Request) {})(w, r)

	if w.Code != http.StatusUnauthorized {
		t.Errorf("Expected the status code to be a 401, because AuthenticateToken recognized that the token is stored in Redis due to the mock IsTokenStored method always returning true, but recieved a status code of %d", w.Code)
	} else if w.Body.String() != "Token is no longer valid\n" {
		t.Errorf("Expected the responsewriter body to contain \"Token is no longer valid\", because AuthenticateToken recognized that the token is stored in Redis due to the mock IsTokenStored method always returning true, but the responsewriter contained %s", w.Body.String())
	}
}
Exemple #2
0
func main() {

	settings.SetPreproductionEnv() // Set GO_ENV to "preproduction"

	db := datastores.ConnectToPostgres()

	ac := auth.NewAuthContext(&datastores.JWTStore{datastores.ConnectToRedis()})
	c := &m.Context{ac, &datastores.RepStore{datastores.ConnectToMongoCol()}, nil}

	r := handlers.AssignHandlersToRoutes(c, db)
	http.Handle("/", &Server{r})

	fmt.Println("Listening on port 3030")
	http.ListenAndServe(":3030", nil)
}
func TestAuthenticateTokenWithNoToken(t *testing.T) {

	r, err := http.NewRequest("", "", nil)
	if err != nil {
		t.Error(err)
	}
	w := httptest.NewRecorder()

	ac := auth.NewAuthContext(&MockTokenStore{IsStored: false})
	AuthenticateToken(&Context{ac, nil, nil}, func(c *Context, w http.ResponseWriter, r *http.Request) {
		if (c.Exp == time.Time{}) && (c.UserID == "") {
			w.Write([]byte("Context has a nil value for both the UserID and Exp fields"))
		}
	})(w, r)

	if w.Body.String() != "Context has a nil value for both the UserID and Exp fields" {
		t.Errorf("Expected the responsewriter's body to contain a message of \"Context has a nil value for both the UserID and Exp fields\" because there was no token in the request")
	}
}
func TestAuthenticateTokenWithInvalidToken(t *testing.T) {

	r, err := http.NewRequest("", "", nil)
	if err != nil {
		t.Error(err)
	}

	r.Header.Set("Authorization", "BEARER:eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ")

	w := httptest.NewRecorder()

	ac := auth.NewAuthContext(&MockTokenStore{IsStored: false})
	AuthenticateToken(&Context{ac, nil, nil}, func(c *Context, w http.ResponseWriter, r *http.Request) {})(w, r)

	if w.Code != http.StatusUnauthorized {
		t.Errorf("Expected the status code to be 401, because of the request contained an invalid JWT, but instead recieved a status code of %d", w.Code)
	} else if w.Body.String() != "Unrecognized signing method: HS256\n" {
		t.Errorf("Expected the responsewriter body to be set to \"Unrecognized signing method: HS256\", but instead the responsewriter body is set to \"%s\"", w.Body.String())
	}
}
func TestServeSubmitQuestionWithExistingQuestion(t *testing.T) {

	existingQuestion := &models.Question{UserID: "0c1b2b91-9164-4d52-87b0-9c4b444ee62d", Username: "******", Title: "Where is the best sushi place?", Content: "I have cravings"}

	c := &m.Context{auth.NewAuthContext(nil), &MockRepStore{}, existingQuestion}

	r, err := http.NewRequest("POST", "api/question/TestCategory", nil)
	if err != nil {
		t.Error(err)
	}

	w := httptest.NewRecorder()

	ServeSubmitQuestion(&MockQuestionStore{ExistingID: "526c4576-0e49-4e90-b760-e6976c698574"})(c, w, r)

	if w.Code != http.StatusBadRequest {
		t.Errorf("Expected a status code of 400 due to the existence of a question with the same title as that of the question recieved in the request body, recieved a status code of %d", w.Code)

	} else if w.Body.String() != "The provided title is not unique\n" {
		t.Errorf("Expected the content of the responsewriter to be \"The provided title is not unique\", but instead the responsewriter contains %s", w.Body.String())
	}
}