Human-friendly output for verify account endpoint
This commit is contained in:
parent
ea3b04eff6
commit
aa1361a5fa
3 changed files with 15 additions and 20 deletions
|
@ -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.")
|
||||
}
|
||||
|
|
|
@ -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")
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Add table
Reference in a new issue