Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sonarcloud issues #502

Draft
wants to merge 14 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 25 additions & 23 deletions API/controllers/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"go.mongodb.org/mongo-driver/bson/primitive"
)

const invalidRequestMsg = "invalid request"

func init() {
rand.Seed(time.Now().UnixNano())
}
Expand Down Expand Up @@ -47,9 +49,9 @@ func init() {
// description: Internal server error

func CreateAccount(w http.ResponseWriter, r *http.Request) {
fmt.Println("******************************************************")
fmt.Println(u.AsteriskLine)
fmt.Println("FUNCTION CALL: CreateAccount ")
fmt.Println("******************************************************")
fmt.Println(u.AsteriskLine)
DispRequestMetaData(r)

if r.Method == "OPTIONS" {
Expand Down Expand Up @@ -105,16 +107,16 @@ func CreateAccount(w http.ResponseWriter, r *http.Request) {
// description: Internal server error

func CreateBulkAccount(w http.ResponseWriter, r *http.Request) {
fmt.Println("******************************************************")
fmt.Println(u.AsteriskLine)
fmt.Println("FUNCTION CALL: CreateBulkAccount ")
fmt.Println("******************************************************")
fmt.Println(u.AsteriskLine)
DispRequestMetaData(r)

var accounts []models.Account
err := json.NewDecoder(r.Body).Decode(&accounts)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
u.Respond(w, u.Message("Invalid request"))
u.Respond(w, u.Message(invalidRequestMsg))
return
}

Expand Down Expand Up @@ -178,9 +180,9 @@ func randStringBytes(n int) string {
// description: Internal server error

func Authenticate(w http.ResponseWriter, r *http.Request) {
fmt.Println("******************************************************")
fmt.Println(u.AsteriskLine)
fmt.Println("FUNCTION CALL: Authenticate ")
fmt.Println("******************************************************")
fmt.Println(u.AsteriskLine)
DispRequestMetaData(r)

if r.Method == "OPTIONS" {
Expand All @@ -190,7 +192,7 @@ func Authenticate(w http.ResponseWriter, r *http.Request) {
err := json.NewDecoder(r.Body).Decode(&account)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
u.Respond(w, u.Message("Invalid request"))
u.Respond(w, u.Message(invalidRequestMsg))
return
}

Expand Down Expand Up @@ -221,9 +223,9 @@ func Authenticate(w http.ResponseWriter, r *http.Request) {
// description: Internal server error

func VerifyToken(w http.ResponseWriter, r *http.Request) {
fmt.Println("******************************************************")
fmt.Println(u.AsteriskLine)
fmt.Println("FUNCTION CALL: Verify ")
fmt.Println("******************************************************")
fmt.Println(u.AsteriskLine)
DispRequestMetaData(r)

if r.Method == "OPTIONS" {
Expand All @@ -247,9 +249,9 @@ func VerifyToken(w http.ResponseWriter, r *http.Request) {
// description: Internal server error

func GetAllAccounts(w http.ResponseWriter, r *http.Request) {
fmt.Println("******************************************************")
fmt.Println(u.AsteriskLine)
fmt.Println("FUNCTION CALL: GetAllAccount ")
fmt.Println("******************************************************")
fmt.Println(u.AsteriskLine)
DispRequestMetaData(r)

if r.Method == "OPTIONS" {
Expand Down Expand Up @@ -300,9 +302,9 @@ func GetAllAccounts(w http.ResponseWriter, r *http.Request) {
// description: Internal server error

func RemoveAccount(w http.ResponseWriter, r *http.Request) {
fmt.Println("******************************************************")
fmt.Println(u.AsteriskLine)
fmt.Println("FUNCTION CALL: RemoveAccount ")
fmt.Println("******************************************************")
fmt.Println(u.AsteriskLine)
DispRequestMetaData(r)

if r.Method == "OPTIONS" {
Expand Down Expand Up @@ -378,9 +380,9 @@ func RemoveAccount(w http.ResponseWriter, r *http.Request) {
// description: Internal server error

func ModifyUserRoles(w http.ResponseWriter, r *http.Request) {
fmt.Println("******************************************************")
fmt.Println(u.AsteriskLine)
fmt.Println("FUNCTION CALL: ModifyUserRoles ")
fmt.Println("******************************************************")
fmt.Println(u.AsteriskLine)
DispRequestMetaData(r)

if r.Method == "OPTIONS" {
Expand Down Expand Up @@ -435,7 +437,7 @@ func getUserRolesFromBody(r *http.Request) (map[string]models.Role, error) {
var data map[string]interface{}
err := json.NewDecoder(r.Body).Decode(&data)
if err != nil {
return nil, fmt.Errorf("invalid request")
return nil, fmt.Errorf(invalidRequestMsg)
}

roles, ok := data["roles"].(map[string]interface{})
Expand Down Expand Up @@ -502,9 +504,9 @@ func getUserRolesFromBody(r *http.Request) (map[string]models.Role, error) {
// description: Internal server error

func ModifyUserPassword(w http.ResponseWriter, r *http.Request) {
fmt.Println("******************************************************")
fmt.Println(u.AsteriskLine)
fmt.Println("FUNCTION CALL: ModifyUserPassword ")
fmt.Println("******************************************************")
fmt.Println(u.AsteriskLine)
DispRequestMetaData(r)

if r.Method == "OPTIONS" {
Expand Down Expand Up @@ -566,7 +568,7 @@ func getModifyPassDataFromBody(r *http.Request, userEmail string) (string, strin
var data map[string]interface{}
err := json.NewDecoder(r.Body).Decode(&data)
if err != nil {
return currentPassword, "", isReset, fmt.Errorf("invalid request")
return currentPassword, "", isReset, fmt.Errorf(invalidRequestMsg)
}
if userEmail == u.RESET_TAG {
// it's not change, it's reset (no need for current password)
Expand Down Expand Up @@ -606,9 +608,9 @@ func getModifyPassDataFromBody(r *http.Request, userEmail string) (string, strin
// description: Internal server error

func UserForgotPassword(w http.ResponseWriter, r *http.Request) {
fmt.Println("******************************************************")
fmt.Println(u.AsteriskLine)
fmt.Println("FUNCTION CALL: UserForgotPassword ")
fmt.Println("******************************************************")
fmt.Println(u.AsteriskLine)
DispRequestMetaData(r)

if r.Method == "OPTIONS" {
Expand All @@ -619,7 +621,7 @@ func UserForgotPassword(w http.ResponseWriter, r *http.Request) {
err := json.NewDecoder(r.Body).Decode(&data)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
u.Respond(w, u.Message("Invalid request"))
u.Respond(w, u.Message(invalidRequestMsg))
return
}
userEmail, hasEmail := data["email"].(string)
Expand Down
4 changes: 2 additions & 2 deletions API/controllers/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,8 @@ func TestRequestsWithInvalidBody(t *testing.T) {
message string
}{
{"CreateUser", "POST", test_utils.GetEndpoint("users"), "Invalid request: wrong format body"},
{"CreateBulkUsers", "POST", test_utils.GetEndpoint("usersBulk"), "Invalid request"},
{"Login", "POST", test_utils.GetEndpoint("login"), "Invalid request"},
{"CreateBulkUsers", "POST", test_utils.GetEndpoint("usersBulk"), "invalid request"},
{"Login", "POST", test_utils.GetEndpoint("login"), "invalid request"},
{"ModifyUser", "PATCH", test_utils.GetEndpoint("usersInstance", userId), "invalid request"},
{"ModifyPassword", "POST", test_utils.GetEndpoint("changePassword"), "invalid request"},
}
Expand Down
Loading
Loading