From 0cbca1cbf2a4df32466b0a2de70e141cea9dab38 Mon Sep 17 00:00:00 2001 From: Yuli Date: Thu, 20 Feb 2020 12:05:05 +0200 Subject: [PATCH] add new tests --- src/consent_api.go | 4 ++++ src/consent_db.go | 2 +- src/consent_test.go | 24 ++++++++++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/consent_api.go b/src/consent_api.go index 195dbdc..29e1e31 100644 --- a/src/consent_api.go +++ b/src/consent_api.go @@ -360,6 +360,10 @@ func (e mainEnv) consentUserRecord(w http.ResponseWriter, r *http.Request, ps ht returnError(w, r, "internal error", 405, err, event) return } + if resultJSON == nil { + returnError(w, r, "not found", 405, nil, event) + return + } w.Header().Set("Access-Control-Allow-Origin", "*") w.Header().Set("Content-Type", "application/json; charset=utf-8") w.WriteHeader(200) diff --git a/src/consent_db.go b/src/consent_db.go index ab809bd..ba9d398 100644 --- a/src/consent_db.go +++ b/src/consent_db.go @@ -163,7 +163,7 @@ func (dbobj dbcon) listConsentRecords(userTOKEN string) ([]byte, int, error) { func (dbobj dbcon) viewConsentRecord(userTOKEN string, brief string) ([]byte, error) { record, err := dbobj.getRecord2(TblName.Consent, "token", userTOKEN, "brief", brief) - if err != nil { + if record == nil || err != nil { return nil, err } resultJSON, err := json.Marshal(record) diff --git a/src/consent_test.go b/src/consent_test.go index 2e802a3..c50c7f3 100644 --- a/src/consent_test.go +++ b/src/consent_test.go @@ -110,6 +110,10 @@ func TestCreateWithdrawConsent(t *testing.T) { if _, ok := raw["status"]; !ok || raw["status"].(string) != "ok" { t.Fatalf("Failed to get user consent") } + raw, _ = helpGetUserConsent("email", "moshe@moshe-int.com", brief) + if _, ok := raw["status"]; !ok || raw["status"].(string) != "ok" { + t.Fatalf("Failed to get user consent") + } record := raw["data"].(map[string]interface{}) if record["brief"].(string) != brief { t.Fatalf("Wrong consent brief value") @@ -162,12 +166,32 @@ func TestGetFakeBrief(t *testing.T) { } } +func TestGetUserUnkConsent(t *testing.T) { + userJSON := `{"email":"moshe23@mosh23e-int.com","phone":"123586678"}` + raw, err := helpCreateUser(userJSON) + if err != nil { + t.Fatalf("Wrror in user creation: %s", err) + } + if _, ok := raw["status"]; !ok || raw["status"].(string) != "ok" { + t.Fatalf("Failed to create user") + } + userTOKEN := raw["token"].(string) + raw, _ = helpGetUserConsent("token", userTOKEN, "kolhoz") + if _, ok := raw["status"]; ok && raw["status"].(string) == "ok" { + t.Fatalf("Should fail to get user consent") + } +} + func TestGetFakeUserConsents(t *testing.T) { userTOKEN, _ := uuid.GenerateUUID() raw, _ := helpGetUserConsent("token", userTOKEN, "alibaba") if _, ok := raw["status"]; ok && raw["status"].(string) == "ok" { t.Fatalf("Should fail to get user consent") } + raw, _ = helpGetUserConsent("token", userTOKEN, "ali$baba") + if _, ok := raw["status"]; ok && raw["status"].(string) == "ok" { + t.Fatalf("Should fail to get user consent") + } raw, _ = helpGetUserConsent("fake", userTOKEN, "alibaba") if _, ok := raw["status"]; ok && raw["status"].(string) == "ok" { t.Fatalf("Should fail to get user consent")