add more test and fix bug

This commit is contained in:
Yuli
2020-02-13 14:16:03 +02:00
parent 64532afd5d
commit 1627f449a6
5 changed files with 109 additions and 19 deletions

76
src/consent_test.go Normal file
View File

@@ -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")
}
}

View File

@@ -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,

View File

@@ -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"}`

View File

@@ -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)
}

View File

@@ -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)
}