server: fix reset password test

TestResetPasswordHandler depended on makeToken begin called twice
during the initialization of a single test case and later assuming
the result would match. Because the token has a timestamp accurate
to the second, occasionally the timestamps would be slightly off
within a single test case and cause the test to fail.

Adding a sleep statement to makeToken would cause the test to fail
reliably.

Define a single token for each test case outside of the struct
initializer so test cases compare the same token.

Closes #274

Additionally remove logging statements that dump entire HTML pages.
This commit is contained in:
Eric Chiang 2016-01-19 16:45:59 -08:00
parent b5c7f1978e
commit 4da143ca2d

View file

@ -392,6 +392,12 @@ func TestResetPasswordHandler(t *testing.T) {
user.PasswordHasher = user.DefaultPasswordHasher user.PasswordHasher = user.DefaultPasswordHasher
}() }()
tokenForCase := map[int]string{
0: makeToken("ID-1", "password", testClientID, testRedirectURL, time.Hour*1, goodSigner),
2: makeToken("ID-1", "password", testClientID, url.URL{}, time.Hour*1, goodSigner),
5: makeToken("ID-1", "password", testClientID, url.URL{}, time.Hour*1, goodSigner),
}
tests := []struct { tests := []struct {
query url.Values query url.Values
@ -405,14 +411,14 @@ func TestResetPasswordHandler(t *testing.T) {
{ // Case 0 { // Case 0
// Step 1.1 - User clicks link in email, has valid token. // Step 1.1 - User clicks link in email, has valid token.
query: url.Values{ query: url.Values{
"token": str(makeToken("ID-1", "password", testClientID, testRedirectURL, time.Hour*1, goodSigner)), "token": str(tokenForCase[0]),
}, },
method: "GET", method: "GET",
wantCode: http.StatusOK, wantCode: http.StatusOK,
wantFormValues: &url.Values{ wantFormValues: &url.Values{
"password": str(""), "password": str(""),
"token": str(makeToken("ID-1", "password", testClientID, testRedirectURL, time.Hour*1, goodSigner)), "token": str(tokenForCase[0]),
}, },
wantPassword: "password", wantPassword: "password",
}, },
@ -432,14 +438,14 @@ func TestResetPasswordHandler(t *testing.T) {
{ // Case 2 { // Case 2
// Step 2.1 - User clicks link in email, has valid token. // Step 2.1 - User clicks link in email, has valid token.
query: url.Values{ query: url.Values{
"token": str(makeToken("ID-1", "password", testClientID, url.URL{}, time.Hour*1, goodSigner)), "token": str(tokenForCase[2]),
}, },
method: "GET", method: "GET",
wantCode: http.StatusOK, wantCode: http.StatusOK,
wantFormValues: &url.Values{ wantFormValues: &url.Values{
"password": str(""), "password": str(""),
"token": str(makeToken("ID-1", "password", testClientID, url.URL{}, time.Hour*1, goodSigner)), "token": str(tokenForCase[2]),
}, },
wantPassword: "password", wantPassword: "password",
}, },
@ -472,7 +478,7 @@ func TestResetPasswordHandler(t *testing.T) {
{ // Case 5 { // Case 5
// Step 2.2.1 - User enters in new valid password, password is changed, no redirect // Step 2.2.1 - User enters in new valid password, password is changed, no redirect
query: url.Values{ query: url.Values{
"token": str(makeToken("ID-1", "password", testClientID, url.URL{}, time.Hour*1, goodSigner)), "token": str(tokenForCase[5]),
"password": str("shrt"), "password": str("shrt"),
}, },
method: "POST", method: "POST",
@ -481,7 +487,7 @@ func TestResetPasswordHandler(t *testing.T) {
wantCode: http.StatusBadRequest, wantCode: http.StatusBadRequest,
wantFormValues: &url.Values{ wantFormValues: &url.Values{
"password": str(""), "password": str(""),
"token": str(makeToken("ID-1", "password", testClientID, url.URL{}, time.Hour*1, goodSigner)), "token": str(tokenForCase[5]),
}, },
wantPassword: "password", wantPassword: "password",
}, },
@ -553,7 +559,6 @@ func TestResetPasswordHandler(t *testing.T) {
if tt.wantCode != w.Code { if tt.wantCode != w.Code {
t.Errorf("case %d: wantCode=%v, got=%v", i, tt.wantCode, w.Code) t.Errorf("case %d: wantCode=%v, got=%v", i, tt.wantCode, w.Code)
t.Logf("case %d: Body: %v ", i, w.Body)
continue continue
} }
@ -565,7 +570,6 @@ func TestResetPasswordHandler(t *testing.T) {
if tt.wantFormValues != nil { if tt.wantFormValues != nil {
if diff := pretty.Compare(*tt.wantFormValues, values); diff != "" { if diff := pretty.Compare(*tt.wantFormValues, values); diff != "" {
t.Errorf("case %d: Compare(wantFormValues, got) = %v", i, diff) t.Errorf("case %d: Compare(wantFormValues, got) = %v", i, diff)
t.Logf("case %d: Body: %v ", i, w.Body)
} }
} }
pwi, err := f.srv.PasswordInfoRepo.Get(nil, "ID-1") pwi, err := f.srv.PasswordInfoRepo.Get(nil, "ID-1")