Skip to content

Commit

Permalink
improve existing code test
Browse files Browse the repository at this point in the history
  • Loading branch information
stremovsky committed Aug 12, 2024
1 parent cb7df56 commit cfbd89d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
6 changes: 4 additions & 2 deletions src/email.go
Original file line number Diff line number Diff line change
Expand Up @@ -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!")
Expand Down
3 changes: 2 additions & 1 deletion src/requests_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 0 additions & 2 deletions src/requests_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down
19 changes: 15 additions & 4 deletions src/xtokens_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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")
}
Expand Down

0 comments on commit cfbd89d

Please sign in to comment.