Human-friendly output for verify account endpoint

This commit is contained in:
Daniel Krol 2022-07-31 12:59:11 -04:00
parent ea3b04eff6
commit aa1361a5fa
3 changed files with 15 additions and 20 deletions

View file

@ -3,6 +3,7 @@ package server
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"log"
"net/http" "net/http"
"lbryio/lbry-id/auth" "lbryio/lbry-id/auth"
@ -210,27 +211,20 @@ func (s *Server) verify(w http.ResponseWriter, req *http.Request) {
if paramsErr != nil { if paramsErr != nil {
// In this specific case, the error is limited to values that are safe to // In this specific case, the error is limited to values that are safe to
// give to the user. // 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 return
} }
err := s.store.VerifyAccount(token) err := s.store.VerifyAccount(token)
if err == store.ErrNoTokenForUser { 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 return
} else if err != nil { } 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 return
} }
var verifyResponse struct{} fmt.Fprintf(w, "Your account has been verified.")
response, err := json.Marshal(verifyResponse)
if err != nil {
internalServiceErrorJson(w, err, "Error generating verify response")
return
}
fmt.Fprintf(w, string(response))
} }

View file

@ -480,7 +480,7 @@ func TestServerVerifyAccountSuccess(t *testing.T) {
expectStatusCode(t, w, http.StatusOK) 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)) t.Errorf("Expected register response to be \"{}\": result: %+v", string(body))
} }
@ -494,7 +494,7 @@ func TestServerVerifyAccountErrors(t *testing.T) {
name string name string
token string token string
expectedStatusCode int expectedStatusCode int
expectedErrorString string expectedBody string
expectedCallVerifyAccount bool expectedCallVerifyAccount bool
storeErrors TestStoreFunctionsErrors storeErrors TestStoreFunctionsErrors
@ -503,14 +503,14 @@ func TestServerVerifyAccountErrors(t *testing.T) {
name: "missing token", name: "missing token",
token: "", token: "",
expectedStatusCode: http.StatusBadRequest, 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, expectedCallVerifyAccount: false,
}, },
{ {
name: "token not found", // including expired name: "token not found", // including expired
token: "abcd1234abcd1234abcd1234abcd1234", token: "abcd1234abcd1234abcd1234abcd1234",
expectedStatusCode: http.StatusForbidden, 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}, storeErrors: TestStoreFunctionsErrors{VerifyAccount: store.ErrNoTokenForUser},
expectedCallVerifyAccount: true, expectedCallVerifyAccount: true,
}, },
@ -518,7 +518,7 @@ func TestServerVerifyAccountErrors(t *testing.T) {
name: "assorted db error", name: "assorted db error",
token: "abcd1234abcd1234abcd1234abcd1234", token: "abcd1234abcd1234abcd1234abcd1234",
expectedStatusCode: http.StatusInternalServerError, 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")}, storeErrors: TestStoreFunctionsErrors{VerifyAccount: fmt.Errorf("TestStore.VerifyAccount fail")},
expectedCallVerifyAccount: true, expectedCallVerifyAccount: true,
}, },
@ -541,7 +541,9 @@ func TestServerVerifyAccountErrors(t *testing.T) {
body, _ := ioutil.ReadAll(w.Body) body, _ := ioutil.ReadAll(w.Body)
expectStatusCode(t, w, tc.expectedStatusCode) 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 { if tc.expectedCallVerifyAccount && !testStore.Called.VerifyAccount {
t.Errorf("Expected Store.VerifyAccount to be called") t.Errorf("Expected Store.VerifyAccount to be called")

View file

@ -321,7 +321,6 @@ func TestStoreUpdateVerifyTokenStringAccountNotExists(t *testing.T) {
} }
} }
// Test VerifyAccount for existing account // Test VerifyAccount for existing account
func TestUpdateVerifyAccountSuccess(t *testing.T) { func TestUpdateVerifyAccountSuccess(t *testing.T) {
s, sqliteTmpFile := StoreTestInit(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 // we're not testing normalization features so we'll just use this here
normEmail := email.Normalize() 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) t.Fatalf("Unexpected error in VerifyAccount: err: %+v", err)
} }
expectAccountMatch(t, &s, normEmail, email, password, createdSeed, "", nil) expectAccountMatch(t, &s, normEmail, email, password, createdSeed, "", nil)