Adds tests for context errors
This commit is contained in:
parent
28325c8d7b
commit
63a9e2ff58
@ -1,6 +1,7 @@
|
|||||||
package routes_test
|
package routes_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"gitea.deepak.science/deepak/gogmagog/models"
|
"gitea.deepak.science/deepak/gogmagog/models"
|
||||||
"gitea.deepak.science/deepak/gogmagog/routes"
|
"gitea.deepak.science/deepak/gogmagog/routes"
|
||||||
"gitea.deepak.science/deepak/gogmagog/tokens"
|
"gitea.deepak.science/deepak/gogmagog/tokens"
|
||||||
@ -60,3 +61,73 @@ func TestSingleUser(t *testing.T) {
|
|||||||
assert.Equal("application/json", contentType)
|
assert.Equal("application/json", contentType)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSingleUserEmptyContext(t *testing.T) {
|
||||||
|
// set up
|
||||||
|
assert := assert.New(t)
|
||||||
|
m := getEmptyModel()
|
||||||
|
|
||||||
|
username := "testing_username"
|
||||||
|
displayName := "testing_name"
|
||||||
|
password := "pass"
|
||||||
|
m.CreateUser(&models.CreateUserRequest{Username: username, DisplayName: displayName, Password: password})
|
||||||
|
|
||||||
|
router := routes.NewCurrentUserRouter(m)
|
||||||
|
req, _ := http.NewRequestWithContext(context.Background(), "GET", "/", nil)
|
||||||
|
|
||||||
|
rr := httptest.NewRecorder()
|
||||||
|
|
||||||
|
// function under test
|
||||||
|
router.ServeHTTP(rr, req)
|
||||||
|
|
||||||
|
// check results
|
||||||
|
status := rr.Code
|
||||||
|
assert.Equal(http.StatusUnauthorized, status)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSingleUserContextNoUserID(t *testing.T) {
|
||||||
|
// set up
|
||||||
|
assert := assert.New(t)
|
||||||
|
m := getEmptyModel()
|
||||||
|
|
||||||
|
idToUse := 1
|
||||||
|
username := "testing_username"
|
||||||
|
displayName := "testing_name"
|
||||||
|
password := "pass"
|
||||||
|
m.CreateUser(&models.CreateUserRequest{Username: username, DisplayName: displayName, Password: password})
|
||||||
|
|
||||||
|
router := routes.NewCurrentUserRouter(m)
|
||||||
|
req, _ := http.NewRequestWithContext(tokens.SetUserID(context.Background(), idToUse), "GET", "/", nil)
|
||||||
|
|
||||||
|
rr := httptest.NewRecorder()
|
||||||
|
|
||||||
|
// function under test
|
||||||
|
router.ServeHTTP(rr, req)
|
||||||
|
|
||||||
|
// check results
|
||||||
|
status := rr.Code
|
||||||
|
assert.Equal(http.StatusUnauthorized, status)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestErrorUserContextNoUserID(t *testing.T) {
|
||||||
|
// set up
|
||||||
|
assert := assert.New(t)
|
||||||
|
m := getErrorModel("Here's an error.")
|
||||||
|
|
||||||
|
idToUse := 1
|
||||||
|
|
||||||
|
router := routes.NewCurrentUserRouter(m)
|
||||||
|
req, _ := http.NewRequestWithContext(tokens.GetContextForUserValues(idToUse, "username"), "GET", "/", nil)
|
||||||
|
|
||||||
|
rr := httptest.NewRecorder()
|
||||||
|
|
||||||
|
// function under test
|
||||||
|
router.ServeHTTP(rr, req)
|
||||||
|
|
||||||
|
// check results
|
||||||
|
status := rr.Code
|
||||||
|
assert.Equal(http.StatusInternalServerError, status)
|
||||||
|
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user