From aa1361a5fa2a780d8c153f80281287aa71a6da7a Mon Sep 17 00:00:00 2001 From: Daniel Krol Date: Sun, 31 Jul 2022 12:59:11 -0400 Subject: [PATCH] Human-friendly output for verify account endpoint --- server/account.go | 18 ++++++------------ server/account_test.go | 14 ++++++++------ store/account_test.go | 3 +-- 3 files changed, 15 insertions(+), 20 deletions(-) diff --git a/server/account.go b/server/account.go index 22f2ee4..a1472f8 100644 --- a/server/account.go +++ b/server/account.go @@ -3,6 +3,7 @@ package server import ( "encoding/json" "fmt" + "log" "net/http" "lbryio/lbry-id/auth" @@ -210,27 +211,20 @@ func (s *Server) verify(w http.ResponseWriter, req *http.Request) { if paramsErr != nil { // In this specific case, the error is limited to values that are safe to // give to the user. - errorJson(w, http.StatusBadRequest, paramsErr.Error()) + http.Error(w, "There seems to be a problem with this URL: "+paramsErr.Error(), http.StatusBadRequest) return } err := s.store.VerifyAccount(token) if err == store.ErrNoTokenForUser { - errorJson(w, http.StatusForbidden, "Verification token not found or expired") + http.Error(w, "The verification token was not found, or it expired. Try generating a new one from your app.", http.StatusForbidden) return } else if err != nil { - internalServiceErrorJson(w, err, "Error verifying account") + http.Error(w, "Something went wrong trying to verify your account.", http.StatusInternalServerError) + log.Printf("%s: %+v\n", "Error verifying account", err) return } - var verifyResponse struct{} - response, err := json.Marshal(verifyResponse) - - if err != nil { - internalServiceErrorJson(w, err, "Error generating verify response") - return - } - - fmt.Fprintf(w, string(response)) + fmt.Fprintf(w, "Your account has been verified.") } diff --git a/server/account_test.go b/server/account_test.go index 1be1ff6..6c333bf 100644 --- a/server/account_test.go +++ b/server/account_test.go @@ -480,7 +480,7 @@ func TestServerVerifyAccountSuccess(t *testing.T) { expectStatusCode(t, w, http.StatusOK) - if string(body) != "{}" { + if string(body) != "Your account has been verified." { t.Errorf("Expected register response to be \"{}\": result: %+v", string(body)) } @@ -494,7 +494,7 @@ func TestServerVerifyAccountErrors(t *testing.T) { name string token string expectedStatusCode int - expectedErrorString string + expectedBody string expectedCallVerifyAccount bool storeErrors TestStoreFunctionsErrors @@ -503,14 +503,14 @@ func TestServerVerifyAccountErrors(t *testing.T) { name: "missing token", token: "", expectedStatusCode: http.StatusBadRequest, - expectedErrorString: http.StatusText(http.StatusBadRequest) + ": Missing verifyToken parameter", + expectedBody: "There seems to be a problem with this URL: Missing verifyToken parameter", expectedCallVerifyAccount: false, }, { name: "token not found", // including expired token: "abcd1234abcd1234abcd1234abcd1234", expectedStatusCode: http.StatusForbidden, - expectedErrorString: http.StatusText(http.StatusForbidden) + ": Verification token not found or expired", + expectedBody: "The verification token was not found, or it expired. Try generating a new one from your app.", storeErrors: TestStoreFunctionsErrors{VerifyAccount: store.ErrNoTokenForUser}, expectedCallVerifyAccount: true, }, @@ -518,7 +518,7 @@ func TestServerVerifyAccountErrors(t *testing.T) { name: "assorted db error", token: "abcd1234abcd1234abcd1234abcd1234", expectedStatusCode: http.StatusInternalServerError, - expectedErrorString: http.StatusText(http.StatusInternalServerError), + expectedBody: "Something went wrong trying to verify your account.", storeErrors: TestStoreFunctionsErrors{VerifyAccount: fmt.Errorf("TestStore.VerifyAccount fail")}, expectedCallVerifyAccount: true, }, @@ -541,7 +541,9 @@ func TestServerVerifyAccountErrors(t *testing.T) { body, _ := ioutil.ReadAll(w.Body) expectStatusCode(t, w, tc.expectedStatusCode) - expectErrorString(t, body, tc.expectedErrorString) + if want, got := tc.expectedBody, strings.TrimSpace(string(body)); want != got { + t.Errorf("Body: expected `%s`, got `%s`", want, got) + } if tc.expectedCallVerifyAccount && !testStore.Called.VerifyAccount { t.Errorf("Expected Store.VerifyAccount to be called") diff --git a/store/account_test.go b/store/account_test.go index 5a6a4cf..63ffb43 100644 --- a/store/account_test.go +++ b/store/account_test.go @@ -321,7 +321,6 @@ func TestStoreUpdateVerifyTokenStringAccountNotExists(t *testing.T) { } } - // Test VerifyAccount for existing account func TestUpdateVerifyAccountSuccess(t *testing.T) { s, sqliteTmpFile := StoreTestInit(t) @@ -335,7 +334,7 @@ func TestUpdateVerifyAccountSuccess(t *testing.T) { // we're not testing normalization features so we'll just use this here normEmail := email.Normalize() - if err := s.VerifyAccount( verifyTokenString); err != nil { + if err := s.VerifyAccount(verifyTokenString); err != nil { t.Fatalf("Unexpected error in VerifyAccount: err: %+v", err) } expectAccountMatch(t, &s, normEmail, email, password, createdSeed, "", nil)