mirror of
https://github.com/optim-enterprises-bv/databunker.git
synced 2025-11-01 18:38:06 +00:00
add new tests and fix small bugs
This commit is contained in:
@@ -100,6 +100,9 @@ func (dbobj dbcon) getAuditEvents(userTOKEN string, offset int32, limit int32) (
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
if count == 0 {
|
||||
return []byte("[]"), 0, err
|
||||
}
|
||||
var results []bson.M
|
||||
records, err := dbobj.getList(TblName.Audit, "record", userTOKEN, offset, limit)
|
||||
if err != nil {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
@@ -175,7 +176,11 @@ func (e mainEnv) approveUserRequest(w http.ResponseWriter, r *http.Request, ps h
|
||||
notifyURL := e.conf.Notification.ForgetmeNotificationURL
|
||||
notifyForgetMe(notifyURL, resultJSON, "token", userTOKEN)
|
||||
} else if action == "change-profile" {
|
||||
oldJSON, newJSON, err := e.db.updateUserRecord(requestInfo["change"].([]uint8), userTOKEN, event, e.conf)
|
||||
oldJSON, newJSON, lookupErr, err := e.db.updateUserRecord(requestInfo["change"].([]uint8), userTOKEN, event, e.conf)
|
||||
if lookupErr {
|
||||
returnError(w, r, "internal error", 405, errors.New("not found"), event)
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
returnError(w, r, "internal error", 405, err, event)
|
||||
return
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
@@ -191,7 +192,11 @@ func (e mainEnv) userChange(w http.ResponseWriter, r *http.Request, ps httproute
|
||||
return
|
||||
}
|
||||
}
|
||||
oldJSON, newJSON, err := e.db.updateUserRecord(parsedData.jsonData, userTOKEN, event, e.conf)
|
||||
oldJSON, newJSON, lookupErr, err := e.db.updateUserRecord(parsedData.jsonData, userTOKEN, event, e.conf)
|
||||
if lookupErr {
|
||||
returnError(w, r, "not found", 405, errors.New("not found"), event)
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
returnError(w, r, "internal error", 405, err, event)
|
||||
return
|
||||
|
||||
@@ -130,54 +130,57 @@ func (dbobj dbcon) validateIndexChange(indexName string, idxOldValue string, raw
|
||||
return -1, nil
|
||||
}
|
||||
|
||||
func (dbobj dbcon) updateUserRecord(jsonDataPatch []byte, userTOKEN string, event *auditEvent, conf Config) ([]byte, []byte, error) {
|
||||
func (dbobj dbcon) updateUserRecord(jsonDataPatch []byte, userTOKEN string, event *auditEvent, conf Config) ([]byte, []byte, bool, error) {
|
||||
var err error
|
||||
for x := 0; x < 10; x++ {
|
||||
oldJSON, newJSON, err := dbobj.updateUserRecordDo(jsonDataPatch, userTOKEN, event, conf)
|
||||
oldJSON, newJSON, lookupErr, err := dbobj.updateUserRecordDo(jsonDataPatch, userTOKEN, event, conf)
|
||||
if lookupErr == true {
|
||||
return oldJSON, newJSON, lookupErr, err
|
||||
}
|
||||
if err == nil {
|
||||
return oldJSON, newJSON, nil
|
||||
return oldJSON, newJSON, lookupErr, nil
|
||||
}
|
||||
fmt.Printf("Trying to update user again: %s\n", userTOKEN)
|
||||
}
|
||||
return nil, nil, err
|
||||
return nil, nil, false, err
|
||||
}
|
||||
|
||||
func (dbobj dbcon) updateUserRecordDo(jsonDataPatch []byte, userTOKEN string, event *auditEvent, conf Config) ([]byte, []byte, error) {
|
||||
func (dbobj dbcon) updateUserRecordDo(jsonDataPatch []byte, userTOKEN string, event *auditEvent, conf Config) ([]byte, []byte, bool, error) {
|
||||
//_, err = collection.InsertOne(context.TODO(), bson.M{"name": "The Go Language2", "genre": "Coding", "authorId": "4"})
|
||||
oldUserBson, err := dbobj.lookupUserRecord(userTOKEN)
|
||||
if oldUserBson == nil || err != nil {
|
||||
// not found
|
||||
return nil, nil, err
|
||||
return nil, nil, true, errors.New("not found")
|
||||
}
|
||||
|
||||
// get user key
|
||||
userKey := oldUserBson["key"].(string)
|
||||
recordKey, err := base64.StdEncoding.DecodeString(userKey)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
return nil, nil, false, err
|
||||
}
|
||||
encData0 := oldUserBson["data"].(string)
|
||||
encData, err := base64.StdEncoding.DecodeString(encData0)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
return nil, nil, false, err
|
||||
}
|
||||
decrypted, err := decrypt(dbobj.masterKey, recordKey, encData)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
return nil, nil, false, err
|
||||
}
|
||||
// merge
|
||||
fmt.Printf("old json: %s\n", decrypted)
|
||||
fmt.Printf("json patch: %s\n", jsonDataPatch)
|
||||
newJSON, err := jsonpatch.MergePatch(decrypted, jsonDataPatch)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
return nil, nil, false, err
|
||||
}
|
||||
fmt.Printf("result: %s\n", newJSON)
|
||||
|
||||
var raw map[string]interface{}
|
||||
err = json.Unmarshal(newJSON, &raw)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
return nil, nil, false, err
|
||||
}
|
||||
bdel := bson.M{}
|
||||
sig := oldUserBson["md5"].(string)
|
||||
@@ -190,7 +193,7 @@ func (dbobj dbcon) updateUserRecordDo(jsonDataPatch []byte, userTOKEN string, ev
|
||||
if idxOldValue, ok := oldUserBson[idx+"idx"]; ok {
|
||||
loginCode, err = dbobj.validateIndexChange(idx, idxOldValue.(string), raw, conf)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
return nil, nil, false, err
|
||||
}
|
||||
if loginCode == -1 {
|
||||
bdel[idx+"idx"] = ""
|
||||
@@ -202,7 +205,7 @@ func (dbobj dbcon) updateUserRecordDo(jsonDataPatch []byte, userTOKEN string, ev
|
||||
otherUserBson, _ := dbobj.lookupUserRecordByIndex(idx, newIdxValue.(string), conf)
|
||||
if otherUserBson != nil {
|
||||
// already exist user with same index value
|
||||
return nil, nil, fmt.Errorf("duplicate %s index", idx)
|
||||
return nil, nil, true, fmt.Errorf("duplicate %s index", idx)
|
||||
}
|
||||
//fmt.Printf("adding index2? %s\n", raw[idx])
|
||||
// create login index
|
||||
@@ -231,7 +234,7 @@ func (dbobj dbcon) updateUserRecordDo(jsonDataPatch []byte, userTOKEN string, ev
|
||||
//fmt.Printf("op json: %s\n", update)
|
||||
result, err := dbobj.updateRecord2(TblName.Users, "token", userTOKEN, "md5", sig, &bdoc, &bdel)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
return nil, nil, false, err
|
||||
}
|
||||
if event != nil {
|
||||
event.Before = encData0
|
||||
@@ -243,7 +246,7 @@ func (dbobj dbcon) updateUserRecordDo(jsonDataPatch []byte, userTOKEN string, ev
|
||||
event.Msg = "failed to update"
|
||||
}
|
||||
}
|
||||
return decrypted, newJSON, nil
|
||||
return decrypted, newJSON, false, nil
|
||||
}
|
||||
|
||||
func (dbobj dbcon) lookupUserRecord(userTOKEN string) (bson.M, error) {
|
||||
|
||||
@@ -5,6 +5,8 @@ import (
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
uuid "github.com/hashicorp/go-uuid"
|
||||
)
|
||||
|
||||
func helpCreateUser(userJSON string) (map[string]interface{}, error) {
|
||||
@@ -43,8 +45,8 @@ func helpDeleteUser(index string, indexValue string) (map[string]interface{}, er
|
||||
return helpServe(request)
|
||||
}
|
||||
|
||||
func helpGetUserAuditEvents(userTOKEN string) (map[string]interface{}, error) {
|
||||
url := "http://localhost:3000/v1/audit/list/" + userTOKEN
|
||||
func helpGetUserAuditEvents(userTOKEN string, args string) (map[string]interface{}, error) {
|
||||
url := "http://localhost:3000/v1/audit/list/" + userTOKEN + args
|
||||
request := httptest.NewRequest("GET", url, nil)
|
||||
request.Header.Set("X-Bunker-Token", rootToken)
|
||||
return helpServe(request)
|
||||
@@ -58,9 +60,7 @@ func helpGetUserAuditEvent(atoken string) (map[string]interface{}, error) {
|
||||
}
|
||||
|
||||
func TestCreateUpdateUser(t *testing.T) {
|
||||
|
||||
userJSON := `{"login":"user1","name":"tom","pass":"mylittlepony","k1":[1,10,20],"k2":{"f1":"t1","f3":{"a":"b"}}}`
|
||||
|
||||
raw, err := helpCreateUser(userJSON)
|
||||
if err != nil {
|
||||
t.Fatalf("error: %s", err)
|
||||
@@ -85,14 +85,18 @@ func TestCreateUpdateUser(t *testing.T) {
|
||||
if _, ok := raw["status"]; ok && raw["status"].(string) == "ok" {
|
||||
t.Fatalf("Lookup by login should fail now")
|
||||
}
|
||||
raw2, _ := helpGetUserAuditEvents(userTOKEN)
|
||||
if raw2["status"].(string) != "ok" {
|
||||
raw, _ = helpGetUserAuditEvents(userTOKEN, "?limit=1")
|
||||
if _, ok := raw["status"]; !ok || raw["status"].(string) != "ok" {
|
||||
t.Fatalf("Failed to get audit event/s\n")
|
||||
}
|
||||
if raw2["total"].(float64) != 3 {
|
||||
records := raw["rows"].([]interface{})
|
||||
if raw["total"].(float64) != 3 {
|
||||
t.Fatalf("Wrong number of audit event/s\n")
|
||||
}
|
||||
records := raw2["rows"].([]interface{})
|
||||
if len(records) != 1 {
|
||||
t.Fatalf("Wrong number of audit rows/s\n")
|
||||
}
|
||||
records = raw["rows"].([]interface{})
|
||||
records0 := records[0].(map[string]interface{})
|
||||
atoken := records0["atoken"].(string)
|
||||
if len(atoken) == 0 {
|
||||
@@ -111,6 +115,25 @@ func TestCreateUpdateUser(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuditEventsFakeUser(t *testing.T) {
|
||||
userTOKEN := "token123"
|
||||
raw, _ := helpGetUserAuditEvents(userTOKEN, "")
|
||||
if _, ok := raw["status"]; ok && raw["status"].(string) == "ok" {
|
||||
t.Fatalf("Should fail to get user audit events")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuditEventsFakeUser2(t *testing.T) {
|
||||
userTOKEN, _ := uuid.GenerateUUID()
|
||||
raw, _ := helpGetUserAuditEvents(userTOKEN, "")
|
||||
//if _, ok := raw["status"]; ok && raw["status"].(string) == "ok" {
|
||||
// t.Fatalf("Should fail to get user audit events")
|
||||
//}
|
||||
if raw["total"].(float64) != 0 {
|
||||
t.Fatalf("Should return empty list of audit events")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetFakeUserToken(t *testing.T) {
|
||||
userTOKEN := "token123"
|
||||
raw, _ := helpGetUser("token", userTOKEN)
|
||||
@@ -119,6 +142,30 @@ func TestGetFakeUserToken(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetFakeUserToken2(t *testing.T) {
|
||||
userTOKEN, _ := uuid.GenerateUUID()
|
||||
raw, _ := helpGetUser("token", userTOKEN)
|
||||
if _, ok := raw["status"]; ok && raw["status"].(string) == "ok" {
|
||||
t.Fatalf("Should fail to get user record")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateFakeUser(t *testing.T) {
|
||||
userTOKEN := "token123"
|
||||
raw, _ := helpChangeUser("token", userTOKEN, `{"login":null}`)
|
||||
if _, ok := raw["status"]; ok && raw["status"].(string) == "ok" {
|
||||
t.Fatalf("Should failed to update user")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateFakeUser2(t *testing.T) {
|
||||
userTOKEN, _ := uuid.GenerateUUID()
|
||||
raw, _ := helpChangeUser("token", userTOKEN, `{"login":null}`)
|
||||
if _, ok := raw["status"]; ok && raw["status"].(string) == "ok" {
|
||||
t.Fatalf("Should failed to update user")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateUser2(t *testing.T) {
|
||||
data := "name=user2&job=developer&email=user2@user2.com"
|
||||
raw, _ := helpCreateUser2(data)
|
||||
|
||||
Reference in New Issue
Block a user