diff --git a/src/consent_test.go b/src/consent_test.go new file mode 100644 index 0000000..2f69741 --- /dev/null +++ b/src/consent_test.go @@ -0,0 +1,76 @@ +package main + +import ( + "encoding/json" + "fmt" + "net/http/httptest" + "strings" + "testing" + //uuid "github.com/hashicorp/go-uuid" +) + +func helpAcceptConsent(mode string, address string, brief string, dataJSON string) (map[string]interface{}, error) { + url := "http://localhost:3000/v1/consent/" + mode + "/" + address + "/" + brief + request := httptest.NewRequest("POST", url, strings.NewReader(dataJSON)) + rr := httptest.NewRecorder() + request.Header.Set("Content-Type", "application/json") + request.Header.Set("X-Bunker-Token", rootToken) + + router.ServeHTTP(rr, request) + var raw map[string]interface{} + fmt.Printf("Got: %s\n", rr.Body.Bytes()) + err := json.Unmarshal(rr.Body.Bytes(), &raw) + return raw, err +} + +func helpWithdrawConsent(mode string, address string, brief string) (map[string]interface{}, error) { + url := "http://localhost:3000/v1/consent/" + mode + "/" + address + "/" + brief + request := httptest.NewRequest("DELETE", url, nil) + rr := httptest.NewRecorder() + request.Header.Set("Content-Type", "application/json") + request.Header.Set("X-Bunker-Token", rootToken) + + router.ServeHTTP(rr, request) + var raw map[string]interface{} + fmt.Printf("Got: %s\n", rr.Body.Bytes()) + err := json.Unmarshal(rr.Body.Bytes(), &raw) + return raw, err +} + +func helpGetUserConsent(mode string, address string, brief string) (map[string]interface{}, error) { + url := "http://localhost:3000/v1/consent/" + mode + "/" + address + "/" + brief + request := httptest.NewRequest("GET", url, nil) + rr := httptest.NewRecorder() + request.Header.Set("X-Bunker-Token", rootToken) + + router.ServeHTTP(rr, request) + var raw map[string]interface{} + fmt.Printf("Got: %s\n", rr.Body.Bytes()) + err := json.Unmarshal(rr.Body.Bytes(), &raw) + return raw, err +} + +func TestCreateWithdrawConsent(t *testing.T) { + userJSON := `{"login":"moshe", "email":"moshe@moshe-int.com"}` + raw, err := helpCreateUser(userJSON) + if err != nil { + t.Fatalf("error: %s", err) + } + if _, found := raw["status"]; !found || raw["status"].(string) != "ok" { + t.Fatalf("failed to create user") + } + userTOKEN := raw["token"].(string) + bief := "test1" + raw, _ = helpAcceptConsent("email", "moshe@moshe-int.com", bief, "") + if _, ok := raw["status"]; !ok || raw["status"].(string) != "ok" { + t.Fatalf("failed to create session") + } + raw, _ = helpGetUserConsent("token", userTOKEN, bief) + if _, ok := raw["status"]; !ok || raw["status"].(string) != "ok" { + t.Fatalf("failed to create session") + } + record := raw["data"].(map[string]interface{}) + if record["brief"].(string) != bief { + t.Fatalf("wrong concent brief value") + } +} diff --git a/src/qldb.go b/src/qldb.go index 2c6c9b6..e11ac06 100644 --- a/src/qldb.go +++ b/src/qldb.go @@ -917,7 +917,7 @@ func initSharedRecords(db *sql.DB) error { token STRING, record STRING, partner STRING, - sesion STRING, + session STRING, app STRING, fields STRING, endtime int, diff --git a/src/sessions_test.go b/src/sessions_test.go index 04cafcb..54bcf9b 100644 --- a/src/sessions_test.go +++ b/src/sessions_test.go @@ -104,6 +104,32 @@ func TestCreateSessionRecord(t *testing.T) { } } +func TestCreateSessionAndSharedRecord(t *testing.T) { + userJSON := `{"login":"dima"}` + raw, err := helpCreateUser(userJSON) + if err != nil { + t.Fatalf("error: %s", err) + } + if _, found := raw["status"]; !found || raw["status"].(string) != "ok" { + t.Fatalf("failed to create user") + } + userTOKEN := raw["token"].(string) + data := `{"expiration":"1m","cookie":"abcdefg","secret":"value"}` + raw, _ = helpCreateSession(userTOKEN, data) + if _, ok := raw["status"]; !ok || raw["status"].(string) != "ok" { + t.Fatalf("failed to create session") + } + sessionTOKEN := raw["session"].(string) + data = fmt.Sprintf(`{"expiration":"1d","session":"%s","fields":"cookie,missing"}`, sessionTOKEN) + raw, _ = helpCreateSharedRecord(userTOKEN, data) + recordTOKEN := raw["record"].(string) + fmt.Printf("User record token: %s\n", recordTOKEN) + raw, _ = helpGetSharedRecord(recordTOKEN) + if _, ok := raw["status"]; !ok || raw["status"].(string) != "ok" { + t.Fatalf("Failed to get shared record: %s\n", raw["message"]) + } +} + func TestFailCreateSession(t *testing.T) { userTOKEN, _ := uuid.GenerateUUID() data := `{"expiration":"1d","cookie":"12345"}` diff --git a/src/sharedrecords_api.go b/src/sharedrecords_api.go index 9d85b71..c6269f1 100644 --- a/src/sharedrecords_api.go +++ b/src/sharedrecords_api.go @@ -153,7 +153,7 @@ func (e mainEnv) getRecord(w http.ResponseWriter, r *http.Request, ps httprouter recordInfo.appName, resultJSON) } else if len(recordInfo.session) > 0 { str = fmt.Sprintf(`{"status":"ok","session":"%s","data":%s}`, - recordInfo.appName, resultJSON) + recordInfo.session, resultJSON) } else { str = fmt.Sprintf(`{"status":"ok","data":%s}`, resultJSON) } diff --git a/src/sharedrecords_test.go b/src/sharedrecords_test.go index 0e5492e..88ef0d1 100644 --- a/src/sharedrecords_test.go +++ b/src/sharedrecords_test.go @@ -44,7 +44,6 @@ func TestCreateSharedRecord(t *testing.T) { t.Fatalf("error: %s", err) } var userTOKEN string - var recordTOKEN string if status, ok := raw["status"]; ok { if status == "error" { if strings.HasPrefix(raw["message"].(string), "duplicate") { @@ -64,25 +63,14 @@ func TestCreateSharedRecord(t *testing.T) { data := `{"expiration":"1d","fields":"uuid,name,pass,k1,k2.f3"}` raw, _ = helpCreateSharedRecord(userTOKEN, data) - - if status, ok := raw["status"]; ok { - if status == "error" { - t.Fatalf("Failed to create shared record: %s\n", raw["message"]) - return - } else if status == "ok" { - recordTOKEN = raw["record"].(string) - } - } - if len(recordTOKEN) == 0 { - t.Fatalf("Failed to retrieve user token: %s\n", raw) + if _, ok := raw["status"]; !ok || raw["status"].(string) != "ok" { + t.Fatalf("Failed to create shared record: %s\n", raw["message"]) } + recordTOKEN := raw["record"].(string) fmt.Printf("User record token: %s\n", recordTOKEN) raw, _ = helpGetSharedRecord(recordTOKEN) - if status, ok := raw["status"]; ok { - if status == "error" { - t.Fatalf("Failed to get shared record: %s\n", raw["message"]) - return - } + if _, ok := raw["status"]; !ok || raw["status"].(string) != "ok" { + t.Fatalf("Failed to get shared record: %s\n", raw["message"]) } helpDeleteUser("token", userTOKEN) }