add more tests
This commit is contained in:
		@ -574,6 +574,64 @@ func Test_BookRoom(t *testing.T) {
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// }}}
 | 
			
		||||
// {{{ Login
 | 
			
		||||
 | 
			
		||||
var loginTests = []struct {
 | 
			
		||||
	name               string
 | 
			
		||||
	email              string
 | 
			
		||||
	expectedStatusCode int
 | 
			
		||||
	expectedHTML       string
 | 
			
		||||
	expectedLocation   string
 | 
			
		||||
}{
 | 
			
		||||
	{"valid-credentials", "a@b.c", http.StatusSeeOther, "", "/admin/dashboard"},
 | 
			
		||||
	{"invalid-credentials", "invalid@b.c", http.StatusSeeOther, "", "/user/login"},
 | 
			
		||||
	{"invalid-data", "a@b", http.StatusOK, `action="/user/login"`, ""},
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestLogin(t *testing.T) {
 | 
			
		||||
	// range through all tests
 | 
			
		||||
	for _, e := range loginTests {
 | 
			
		||||
		postedData := url.Values{}
 | 
			
		||||
		postedData.Add("email", e.email)
 | 
			
		||||
		postedData.Add("password", "password")
 | 
			
		||||
 | 
			
		||||
		// create request
 | 
			
		||||
		req, _ := http.NewRequest("POST", "/user/login", strings.NewReader(postedData.Encode()))
 | 
			
		||||
		ctx := getCtx(req)
 | 
			
		||||
		req = req.WithContext(ctx)
 | 
			
		||||
 | 
			
		||||
		// set the header
 | 
			
		||||
		req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
 | 
			
		||||
		rr := httptest.NewRecorder()
 | 
			
		||||
 | 
			
		||||
		// call the handler
 | 
			
		||||
		handler := http.HandlerFunc(Repo.PostShowLogin)
 | 
			
		||||
		handler.ServeHTTP(rr, req)
 | 
			
		||||
 | 
			
		||||
		if rr.Code != e.expectedStatusCode {
 | 
			
		||||
			t.Errorf("failed %s: expected code %d, but got %d", e.name, e.expectedStatusCode, rr.Code)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if e.expectedLocation != "" {
 | 
			
		||||
			// get the URL from test
 | 
			
		||||
			actualLoc, _ := rr.Result().Location()
 | 
			
		||||
			if actualLoc.String() != e.expectedLocation {
 | 
			
		||||
				t.Errorf("failed %s: expected location %s, but got location %s", e.name, e.expectedLocation, actualLoc.String())
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// checking for expected values in HTML
 | 
			
		||||
		if e.expectedHTML != "" {
 | 
			
		||||
			// read the response body into a string
 | 
			
		||||
			html := rr.Body.String()
 | 
			
		||||
			if !strings.Contains(html, e.expectedHTML) {
 | 
			
		||||
				t.Errorf("failed %s: expected html contains %s, but got html %s", e.name, e.expectedHTML, html)
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// }}}
 | 
			
		||||
// {{{ Test Helpers
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -79,7 +79,10 @@ func (m *testDBRepo) UpdateUser(u models.User) error {
 | 
			
		||||
 | 
			
		||||
// Authenticate authenticates a user
 | 
			
		||||
func (m *testDBRepo) Authenticate(email, testPassword string) (int, string, error) {
 | 
			
		||||
	if email == "a@b.c" {
 | 
			
		||||
		return 1, "", nil
 | 
			
		||||
	}
 | 
			
		||||
	return 0, "", errors.New("deliberate errors")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// AllReservations returns a slice of all reservations
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user