mirror of
				https://github.com/optim-enterprises-bv/databunker.git
				synced 2025-10-31 01:47:57 +00:00 
			
		
		
		
	add user API tests
This commit is contained in:
		| @@ -34,7 +34,6 @@ func (e mainEnv) getAuditEvents(w http.ResponseWriter, r *http.Request, ps httpr | |||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 	fmt.Printf("Total count of events: %d\n", counter) | 	fmt.Printf("Total count of events: %d\n", counter) | ||||||
| 	//fmt.Fprintf(w, "<html><head><title>title</title></head>") |  | ||||||
| 	w.Header().Set("Access-Control-Allow-Origin", "*") | 	w.Header().Set("Access-Control-Allow-Origin", "*") | ||||||
| 	w.Header().Set("Content-Type", "application/json; charset=utf-8") | 	w.Header().Set("Content-Type", "application/json; charset=utf-8") | ||||||
| 	w.WriteHeader(200) | 	w.WriteHeader(200) | ||||||
| @@ -60,7 +59,6 @@ func (e mainEnv) getAuditEvent(w http.ResponseWriter, r *http.Request, ps httpro | |||||||
| 	if e.enforceAuth(w, r, event) == "" { | 	if e.enforceAuth(w, r, event) == "" { | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 	//fmt.Fprintf(w, "<html><head><title>title</title></head>") |  | ||||||
| 	w.Header().Set("Access-Control-Allow-Origin", "*") | 	w.Header().Set("Access-Control-Allow-Origin", "*") | ||||||
| 	w.Header().Set("Content-Type", "application/json; charset=utf-8") | 	w.Header().Set("Content-Type", "application/json; charset=utf-8") | ||||||
| 	w.WriteHeader(200) | 	w.WriteHeader(200) | ||||||
|   | |||||||
| @@ -143,7 +143,7 @@ func (dbobj dbcon) getAuditEvent(atoken string) (string, []byte, error) { | |||||||
| 	if len(record) == 0 { | 	if len(record) == 0 { | ||||||
| 		return "", nil, errors.New("not found") | 		return "", nil, errors.New("not found") | ||||||
| 	} | 	} | ||||||
| 	fmt.Printf("audit record: %s\n", record) | 	//fmt.Printf("audit record: %s\n", record) | ||||||
| 	before := "" | 	before := "" | ||||||
| 	after := "" | 	after := "" | ||||||
| 	debug := "" | 	debug := "" | ||||||
|   | |||||||
| @@ -91,6 +91,7 @@ func init() { | |||||||
| 	var cfg Config | 	var cfg Config | ||||||
| 	cfg.Sms.TwilioToken = "ttoken" | 	cfg.Sms.TwilioToken = "ttoken" | ||||||
| 	cfg.SelfService.AppRecordChange = []string{"*"} | 	cfg.SelfService.AppRecordChange = []string{"*"} | ||||||
|  | 	cfg.Generic.CreateUserWithoutAccessToken = true | ||||||
| 	e := mainEnv{db, cfg, make(chan struct{})} | 	e := mainEnv{db, cfg, make(chan struct{})} | ||||||
| 	rootToken, err = db.createRootXtoken() | 	rootToken, err = db.createRootXtoken() | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
|   | |||||||
| @@ -101,25 +101,18 @@ func TestCreateUpdateUser(t *testing.T) { | |||||||
| 		t.Fatalf("Failed to get audit event/s\n") | 		t.Fatalf("Failed to get audit event/s\n") | ||||||
| 	} | 	} | ||||||
| 	records = raw["rows"].([]interface{}) | 	records = raw["rows"].([]interface{}) | ||||||
| 	records0 := records[0].(map[string]interface{}) | 	atoken := "" | ||||||
| 	records2 := records[2].(map[string]interface{}) | 	for id := range records { | ||||||
| 	atoken := records0["atoken"].(string) | 		records0 := records[id].(map[string]interface{}) | ||||||
| 	if len(atoken) == 0 { | 		atoken = records0["atoken"].(string) | ||||||
| 		t.Fatalf("Failed to extract atoken\n") | 		if len(atoken) == 0 { | ||||||
| 	} | 			t.Fatalf("Failed to extract atoken\n") | ||||||
| 	fmt.Printf("Audit record: %s\n", atoken) | 		} | ||||||
| 	raw, _ = helpGetUserAuditEvent(atoken) | 		fmt.Printf("Audit record: %s\n", atoken) | ||||||
| 	if _, ok := raw["status"]; !ok || raw["status"].(string) != "ok" { | 		raw, _ = helpGetUserAuditEvent(atoken) | ||||||
| 		t.Fatalf("Failed to get specific audit event\n") | 		if _, ok := raw["status"]; !ok || raw["status"].(string) != "ok" { | ||||||
| 	} | 			t.Fatalf("Failed to get specific audit event\n") | ||||||
| 	atoken = records2["atoken"].(string) | 		} | ||||||
| 	if len(atoken) == 0 { |  | ||||||
| 		t.Fatalf("Failed to extract atoken\n") |  | ||||||
| 	} |  | ||||||
| 	fmt.Printf("Audit record[2]: %s\n", atoken) |  | ||||||
| 	raw, _ = helpGetUserAuditEvent(atoken) |  | ||||||
| 	if _, ok := raw["status"]; !ok || raw["status"].(string) != "ok" { |  | ||||||
| 		t.Fatalf("Failed to get specific audit event\n") |  | ||||||
| 	} | 	} | ||||||
| 	oldRootToken := rootToken | 	oldRootToken := rootToken | ||||||
| 	rootToken, _ = uuid.GenerateUUID() | 	rootToken, _ = uuid.GenerateUUID() | ||||||
| @@ -218,3 +211,73 @@ func TestCreateUser2(t *testing.T) { | |||||||
| 		t.Fatalf("Wrong email address") | 		t.Fatalf("Wrong email address") | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
|  | func TestCreateUserEmptyBody(t *testing.T) { | ||||||
|  | 	data := "{}" | ||||||
|  | 	raw, _ := helpCreateUser(data) | ||||||
|  | 	if _, ok := raw["status"]; ok && raw["status"].(string) == "ok" { | ||||||
|  | 		t.Fatalf("Should failed to create user") | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func TestCreateUserDupLogin(t *testing.T) { | ||||||
|  | 	data := `{"login":"dup","name":"dup"}` | ||||||
|  | 	raw, _ := helpCreateUser(data) | ||||||
|  | 	if _, ok := raw["status"]; !ok || raw["status"].(string) != "ok" { | ||||||
|  | 		t.Fatalf("Failed to create dup1 user") | ||||||
|  | 	} | ||||||
|  | 	data = `{"login":"dup","name":"dup2"}` | ||||||
|  | 	raw, _ = helpCreateUser(data) | ||||||
|  | 	if _, ok := raw["status"]; ok && raw["status"].(string) == "ok" { | ||||||
|  | 		t.Fatalf("Should failed to create user") | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func TestCreateUserDupEmail(t *testing.T) { | ||||||
|  | 	data := `{"email":"dup@dupdup.com","name":"dup"}` | ||||||
|  | 	raw, _ := helpCreateUser(data) | ||||||
|  | 	if _, ok := raw["status"]; !ok || raw["status"].(string) != "ok" { | ||||||
|  | 		t.Fatalf("Failed to create dup1 user") | ||||||
|  | 	} | ||||||
|  | 	data = `{"email":"dup@dupdup.com","name":"dup2"}` | ||||||
|  | 	raw, _ = helpCreateUser(data) | ||||||
|  | 	if _, ok := raw["status"]; ok && raw["status"].(string) == "ok" { | ||||||
|  | 		t.Fatalf("Should failed to create user") | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func TestCreateUserDupPhone(t *testing.T) { | ||||||
|  | 	data := `{"phone":"334455667788","name":"dup"}` | ||||||
|  | 	raw, _ := helpCreateUser(data) | ||||||
|  | 	if _, ok := raw["status"]; !ok || raw["status"].(string) != "ok" { | ||||||
|  | 		t.Fatalf("Failed to create dup1 user") | ||||||
|  | 	} | ||||||
|  | 	data = `{"phone":"334455667788","name":"dup2"}` | ||||||
|  | 	raw, _ = helpCreateUser(data) | ||||||
|  | 	if _, ok := raw["status"]; ok && raw["status"].(string) == "ok" { | ||||||
|  | 		t.Fatalf("Should failed to create user") | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func TestCreateUserBadPOST(t *testing.T) { | ||||||
|  | 	url := "http://localhost:3000/v1/user" | ||||||
|  | 	data := "name=user6&job=developer&email=user6@user6.com" | ||||||
|  | 	request := httptest.NewRequest("POST", url, strings.NewReader(data)) | ||||||
|  | 	request.Header.Set("X-Bunker-Token", rootToken) | ||||||
|  | 	raw, _ := helpServe(request) | ||||||
|  | 	if _, ok := raw["status"]; ok && raw["status"].(string) == "ok" { | ||||||
|  | 		t.Fatalf("Should failed to create user") | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func TestCreateUserEmptyXToken2(t *testing.T) { | ||||||
|  | 	//e.conf.Generic.CreateUserWithoutAccessToken = true | ||||||
|  | 	url := "http://localhost:3000/v1/user" | ||||||
|  | 	data := "name=user8&job=developer&email=user8@user8.com" | ||||||
|  | 	request := httptest.NewRequest("POST", url, strings.NewReader(data)) | ||||||
|  | 	request.Header.Set("Content-Type", "application/x-www-form-urlencoded") | ||||||
|  | 	raw, _ := helpServe2(request) | ||||||
|  | 	if _, ok := raw["status"]; !ok || raw["status"].(string) != "ok" { | ||||||
|  | 		t.Fatalf("Should failed to create user") | ||||||
|  | 	} | ||||||
|  | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Yuli
					Yuli