mirror of
https://github.com/outbackdingo/databunker.git
synced 2026-01-27 10:18:45 +00:00
improve existing code test
This commit is contained in:
@@ -10,17 +10,19 @@ import (
|
||||
|
||||
func sendCodeByEmail(code int32, identity string, cfg Config) {
|
||||
dest := []string{identity}
|
||||
target := strings.Join(dest, ",")
|
||||
subject := "Access Code"
|
||||
bodyMessage := "Access code is " + strconv.Itoa(int((code)))
|
||||
msg := "From: " + cfg.SMTP.Sender + "\n" +
|
||||
"To: " + strings.Join(dest, ",") + "\n" +
|
||||
"To: " + target + "\n" +
|
||||
"Subject: " + subject + "\n" +
|
||||
bodyMessage
|
||||
auth := smtp.PlainAuth("", cfg.SMTP.User, cfg.SMTP.Pass, cfg.SMTP.Server)
|
||||
err := smtp.SendMail(cfg.SMTP.Server+":"+cfg.SMTP.Port,
|
||||
auth, cfg.SMTP.User, dest, []byte(msg))
|
||||
log.Printf("Send email to %s via %s", target, cfg.SMTP.Server)
|
||||
if err != nil {
|
||||
log.Printf("error sending email: %s", err)
|
||||
log.Printf("Error sending email: %s", err)
|
||||
return
|
||||
}
|
||||
log.Printf("Mail sent successfully!")
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
)
|
||||
|
||||
// This function retrieves all requests that require admin approval. This function supports result pager.
|
||||
func (e mainEnv) getUserRequests(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||
if e.enforceAuth(w, r, nil) == "" {
|
||||
return
|
||||
@@ -38,6 +39,7 @@ func (e mainEnv) getUserRequests(w http.ResponseWriter, r *http.Request, ps http
|
||||
w.Write([]byte(str))
|
||||
}
|
||||
|
||||
// Get list of requests for specific user
|
||||
func (e mainEnv) getCustomUserRequests(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||
identity := ps.ByName("identity")
|
||||
mode := ps.ByName("mode")
|
||||
@@ -83,7 +85,6 @@ func (e mainEnv) getCustomUserRequests(w http.ResponseWriter, r *http.Request, p
|
||||
returnError(w, r, "internal error", 405, err, nil)
|
||||
return
|
||||
}
|
||||
fmt.Printf("Total count of custom user requests: %d\n", counter)
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.WriteHeader(200)
|
||||
str := fmt.Sprintf(`{"status":"ok","total":%d,"rows":%s}`, counter, resultJSON)
|
||||
|
||||
@@ -89,7 +89,6 @@ func (dbobj dbcon) getRequests(status string, offset int32, limit int32) ([]byte
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
//fmt.Printf("Found multiple documents (array of pointers): %+v\n", results)
|
||||
return resultJSON, count, nil
|
||||
}
|
||||
|
||||
@@ -117,7 +116,6 @@ func (dbobj dbcon) getUserRequests(userTOKEN string, offset int32, limit int32)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
//fmt.Printf("Found multiple documents (array of pointers): %+v\n", results)
|
||||
return resultJSON, count, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -25,13 +25,20 @@ func helpUserLogin(mode string, identity string, code string) (map[string]interf
|
||||
return helpServe(request)
|
||||
}
|
||||
|
||||
func helpGetUserRequests() (map[string]interface{}, error) {
|
||||
func helpGetAllUserRequests() (map[string]interface{}, error) {
|
||||
url := "http://localhost:3000/v1/requests"
|
||||
request := httptest.NewRequest("GET", url, nil)
|
||||
request.Header.Set("X-Bunker-Token", rootToken)
|
||||
return helpServe(request)
|
||||
}
|
||||
|
||||
func helpGetUserRequests(mode string, identity string) (map[string]interface{}, error) {
|
||||
url := "http://localhost:3000/v1/requests/" + mode + "/" + identity
|
||||
request := httptest.NewRequest("GET", url, nil)
|
||||
request.Header.Set("X-Bunker-Token", rootToken)
|
||||
return helpServe(request)
|
||||
}
|
||||
|
||||
func helpApproveUserRequest(rtoken string) (map[string]interface{}, error) {
|
||||
url := "http://localhost:3000/v1/request/" + rtoken
|
||||
request := httptest.NewRequest("POST", url, nil)
|
||||
@@ -131,11 +138,15 @@ func TestUserLoginDelete(t *testing.T) {
|
||||
}
|
||||
rtoken0 := raw["rtoken"].(string)
|
||||
raw, _ = helpGetUserAppList(userTOKEN)
|
||||
log.Printf("apps: %s\n", raw["apps"])
|
||||
log.Printf("List apps: %s\n", raw["apps"])
|
||||
|
||||
rootToken = oldRootToken
|
||||
user_requests, _ := helpGetUserRequests("token", userTOKEN)
|
||||
if user_requests["total"].(float64) != 4 {
|
||||
t.Fatalf("Wrong number of user requests\n")
|
||||
}
|
||||
// get user requests
|
||||
raw, _ = helpGetUserRequests()
|
||||
raw, _ = helpGetAllUserRequests()
|
||||
if raw["total"].(float64) != 4 {
|
||||
t.Fatalf("Wrong number of user requests for admin to approval\n")
|
||||
}
|
||||
@@ -175,7 +186,7 @@ func TestUserLoginDelete(t *testing.T) {
|
||||
}
|
||||
}
|
||||
helpApproveUserRequest(rtoken0)
|
||||
raw, _ = helpGetUserRequests()
|
||||
raw, _ = helpGetAllUserRequests()
|
||||
if raw["total"].(float64) != 0 {
|
||||
t.Fatalf("Wrong number of user requests for admin to approval\n")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user