Basic validation checks in error tests (just to make sure validation functions are called)
This commit is contained in:
parent
39015963e6
commit
e0f847a381
3 changed files with 34 additions and 3 deletions
|
@ -42,14 +42,26 @@ func TestServerAuthHandlerSuccess(t *testing.T) {
|
||||||
func TestServerAuthHandlerErrors(t *testing.T) {
|
func TestServerAuthHandlerErrors(t *testing.T) {
|
||||||
tt := []struct {
|
tt := []struct {
|
||||||
name string
|
name string
|
||||||
|
email string
|
||||||
expectedStatusCode int
|
expectedStatusCode int
|
||||||
expectedErrorString string
|
expectedErrorString string
|
||||||
|
|
||||||
storeErrors TestStoreFunctionsErrors
|
storeErrors TestStoreFunctionsErrors
|
||||||
authFailGenToken bool
|
authFailGenToken bool
|
||||||
}{
|
}{
|
||||||
|
{
|
||||||
|
name: "validation error", // missing email address
|
||||||
|
email: "",
|
||||||
|
expectedStatusCode: http.StatusBadRequest,
|
||||||
|
expectedErrorString: http.StatusText(http.StatusBadRequest) + ": Request failed validation",
|
||||||
|
|
||||||
|
// Just check one validation error (missing email address) to make sure the
|
||||||
|
// validate function is called. We'll check the rest of the validation
|
||||||
|
// errors in the other test below.
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: "login fail",
|
name: "login fail",
|
||||||
|
email: "abc@example.com",
|
||||||
expectedStatusCode: http.StatusUnauthorized,
|
expectedStatusCode: http.StatusUnauthorized,
|
||||||
expectedErrorString: http.StatusText(http.StatusUnauthorized) + ": No match for email and password",
|
expectedErrorString: http.StatusText(http.StatusUnauthorized) + ": No match for email and password",
|
||||||
|
|
||||||
|
@ -57,6 +69,7 @@ func TestServerAuthHandlerErrors(t *testing.T) {
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "generate token fail",
|
name: "generate token fail",
|
||||||
|
email: "abc@example.com",
|
||||||
expectedStatusCode: http.StatusInternalServerError,
|
expectedStatusCode: http.StatusInternalServerError,
|
||||||
expectedErrorString: http.StatusText(http.StatusInternalServerError),
|
expectedErrorString: http.StatusText(http.StatusInternalServerError),
|
||||||
|
|
||||||
|
@ -64,6 +77,7 @@ func TestServerAuthHandlerErrors(t *testing.T) {
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "save token fail",
|
name: "save token fail",
|
||||||
|
email: "abc@example.com",
|
||||||
expectedStatusCode: http.StatusInternalServerError,
|
expectedStatusCode: http.StatusInternalServerError,
|
||||||
expectedErrorString: http.StatusText(http.StatusInternalServerError),
|
expectedErrorString: http.StatusText(http.StatusInternalServerError),
|
||||||
|
|
||||||
|
@ -83,7 +97,7 @@ func TestServerAuthHandlerErrors(t *testing.T) {
|
||||||
|
|
||||||
// Make request
|
// Make request
|
||||||
// So long as the JSON is well-formed, the content doesn't matter here since the password check will be stubbed out
|
// So long as the JSON is well-formed, the content doesn't matter here since the password check will be stubbed out
|
||||||
requestBody := `{"deviceId": "dev-1", "email": "abc@example.com", "password": "123"}`
|
requestBody := fmt.Sprintf(`{"deviceId": "dev-1", "email": "%s", "password": "123"}`, tc.email)
|
||||||
req := httptest.NewRequest(http.MethodPost, PathAuthToken, bytes.NewBuffer([]byte(requestBody)))
|
req := httptest.NewRequest(http.MethodPost, PathAuthToken, bytes.NewBuffer([]byte(requestBody)))
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
|
|
||||||
|
|
|
@ -38,14 +38,26 @@ func TestServerRegisterSuccess(t *testing.T) {
|
||||||
func TestServerRegisterErrors(t *testing.T) {
|
func TestServerRegisterErrors(t *testing.T) {
|
||||||
tt := []struct {
|
tt := []struct {
|
||||||
name string
|
name string
|
||||||
|
email string
|
||||||
requestBody string
|
requestBody string
|
||||||
expectedStatusCode int
|
expectedStatusCode int
|
||||||
expectedErrorString string
|
expectedErrorString string
|
||||||
|
|
||||||
storeErrors TestStoreFunctionsErrors
|
storeErrors TestStoreFunctionsErrors
|
||||||
}{
|
}{
|
||||||
|
{
|
||||||
|
name: "validation error", // missing email address
|
||||||
|
email: "",
|
||||||
|
expectedStatusCode: http.StatusBadRequest,
|
||||||
|
expectedErrorString: http.StatusText(http.StatusBadRequest) + ": Request failed validation",
|
||||||
|
|
||||||
|
// Just check one validation error (missing email address) to make sure the
|
||||||
|
// validate function is called. We'll check the rest of the validation
|
||||||
|
// errors in the other test below.
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: "existing account",
|
name: "existing account",
|
||||||
|
email: "abc@example.com",
|
||||||
expectedStatusCode: http.StatusConflict,
|
expectedStatusCode: http.StatusConflict,
|
||||||
expectedErrorString: http.StatusText(http.StatusConflict) + ": Error registering",
|
expectedErrorString: http.StatusText(http.StatusConflict) + ": Error registering",
|
||||||
|
|
||||||
|
@ -53,6 +65,7 @@ func TestServerRegisterErrors(t *testing.T) {
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "unspecified account creation failure",
|
name: "unspecified account creation failure",
|
||||||
|
email: "abc@example.com",
|
||||||
expectedStatusCode: http.StatusInternalServerError,
|
expectedStatusCode: http.StatusInternalServerError,
|
||||||
expectedErrorString: http.StatusText(http.StatusInternalServerError),
|
expectedErrorString: http.StatusText(http.StatusInternalServerError),
|
||||||
|
|
||||||
|
@ -68,7 +81,7 @@ func TestServerRegisterErrors(t *testing.T) {
|
||||||
server := Server{&testAuth, &testStore}
|
server := Server{&testAuth, &testStore}
|
||||||
|
|
||||||
// Make request
|
// Make request
|
||||||
requestBody := `{ "email": "abc@example.com", "password": "123"}`
|
requestBody := fmt.Sprintf(`{"email": "%s", "password": "123"}`, tc.email)
|
||||||
req := httptest.NewRequest(http.MethodPost, PathAuthToken, bytes.NewBuffer([]byte(requestBody)))
|
req := httptest.NewRequest(http.MethodPost, PathAuthToken, bytes.NewBuffer([]byte(requestBody)))
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
|
|
||||||
|
|
|
@ -30,10 +30,14 @@ func TestServerGetWallet(t *testing.T) {
|
||||||
expectedStatusCode: http.StatusOK,
|
expectedStatusCode: http.StatusOK,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "validation error", // mising auth token
|
name: "validation error", // missing auth token
|
||||||
tokenString: auth.TokenString(""),
|
tokenString: auth.TokenString(""),
|
||||||
expectedStatusCode: http.StatusBadRequest,
|
expectedStatusCode: http.StatusBadRequest,
|
||||||
expectedErrorString: http.StatusText(http.StatusBadRequest) + ": Missing token parameter",
|
expectedErrorString: http.StatusText(http.StatusBadRequest) + ": Missing token parameter",
|
||||||
|
|
||||||
|
// Just check one validation error (missing auth token) to make sure the
|
||||||
|
// validate function is called. We'll check the rest of the validation
|
||||||
|
// errors in the other test below.
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "auth error",
|
name: "auth error",
|
||||||
|
|
Loading…
Reference in a new issue