Skip to content

Commit ce32b18

Browse files
authored
Merge pull request #13 from ockibagusp/branch
fix master
2 parents e05023a + 55384cf commit ce32b18

File tree

15 files changed

+548
-260
lines changed

15 files changed

+548
-260
lines changed

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ file: golang-website-example.sql -> new database: golang-website-example_test
3737
This using [router](https://github.com/ockibagusp/golang-website-example/blob/master/router/router.go).
3838

3939
## httpexpect: Test for Debug
40-
This using [debug](https://github.com/ockibagusp/golang-website-example/blob/master/test/main_test.go).
40+
This using [debug](https://github.com/ockibagusp/golang-website-example/blob/master/tests/main_test.go).
4141

4242
Optional. Default value @debug: {true} or {1}.
4343

@@ -96,13 +96,13 @@ $ go build
9696
#### Test the packages
9797
9898
```
99-
$ go test github.com/ockibagusp/golang-website-example/test
99+
$ go test github.com/ockibagusp/golang-website-example/tests
100100
```
101101
102102
or, verbose output (-v) flag:
103103
104104
```
105-
$ go test github.com/ockibagusp/golang-website-example/test -v
105+
$ go test github.com/ockibagusp/golang-website-example/tests -v
106106
```
107107
108108
@@ -113,9 +113,12 @@ $ go test github.com/ockibagusp/golang-website-example/test -v
113113
Restore | Delete Permanently
114114
115115
- Admin user button: delete not for admin
116+
- Admin user search
116117
- mock unit test
117118
- list pagination with next, previous, first and last
119+
- moves files function Server and NewServer, etc.
118120
- Mutex: BankAccount
121+
- docker
119122
- too much
120123
121124
## Operating System (with me)

controllers/user_controller.go

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,14 @@ func (controller *Controller) Users(c echo.Context) error {
5151
return c.Redirect(http.StatusFound, fmt.Sprintf("/users/read/%v", user.ID))
5252
}
5353

54-
var users []models.User
55-
var err error
54+
var (
55+
users []models.User
56+
err error
57+
58+
// typing: all, admin and user
59+
typing string
60+
)
5661

57-
// typing: all, admin and user
58-
var typing string
5962
if c.QueryParam("admin") == "all" {
6063
log.Infof(`for GET to users admin models.User{}.FindAll(db, "admin")`)
6164
typing = "Admin"
@@ -305,7 +308,7 @@ func (controller *Controller) ReadUser(c echo.Context) error {
305308
"for GET to read user without models.User{}.FirstByID() errors: `%v`", err,
306309
)
307310
log.Warn("END request method GET for read user: [-]failure")
308-
// HTTP response status: 405 Method Not Allowed
311+
// HTTP response status: 406 Method Not Acceptable
309312
return c.HTML(http.StatusNotAcceptable, err.Error())
310313
}
311314

@@ -370,7 +373,11 @@ func (controller *Controller) UpdateUser(c echo.Context) error {
370373
// admin: yes
371374
// (IsUser and (not user.Username)): 403 Forbidden
372375
if middleware.IsUser(is_auth_type) && (user.Username != session.Values["username"]) {
373-
log.Warn("IsUser and (not user.Username): 403 Forbidden")
376+
log.Warnf(
377+
"IsUser (%v) and [not user.Username (%v)]: 403 Forbidden",
378+
middleware.IsUser(is_auth_type),
379+
(user.Username != session.Values["username"]),
380+
)
374381
log.Warn("END request method GET for update user: [-]failure")
375382
return c.HTML(http.StatusForbidden, "403 Forbidden")
376383
}
@@ -508,15 +515,13 @@ func (controller *Controller) UpdateUserByPassword(c echo.Context) error {
508515
controller.DB, id, session.Values["username"].(string),
509516
)
510517

511-
if !middleware.IsAdmin(is_auth_type) {
512-
if err != nil {
513-
log.Warnf(
514-
"for GET to update user by password without models.User{}.FirstByIDAndUsername() errors: `%v`", err,
515-
)
516-
log.Warn("END request method GET for update user by password: [-]failure")
517-
// HTTP response status: 403 Forbidden
518-
return c.HTML(http.StatusForbidden, err.Error())
519-
}
518+
if middleware.IsUser(is_auth_type) && err != nil {
519+
log.Warnf(
520+
"for GET to update user by password without models.User{}.FirstByIDAndUsername() errors: `%v`", err,
521+
)
522+
log.Warn("END request method GET for update user by password: [-]failure")
523+
// HTTP response status: 403 Forbidden
524+
return c.HTML(http.StatusForbidden, err.Error())
520525
}
521526

522527
if c.Request().Method == "POST" {
@@ -527,8 +532,11 @@ func (controller *Controller) UpdateUserByPassword(c echo.Context) error {
527532
ConfirmNewPassword: c.FormValue("confirm_new_password"),
528533
}
529534

530-
if !middleware.IsAdmin(is_auth_type) && !middleware.CheckHashPassword(user.Password, _newPasswordForm.OldPassword) {
531-
log.Warnf("for POST to update user by password without !middleware.CheckHashPassword() errors: `%v`", err)
535+
if !middleware.CheckHashPassword(user.Password, _newPasswordForm.OldPassword) {
536+
log.Warnf(
537+
"for POST to update user by password without check hash password (%v): 403 Forbidden",
538+
middleware.CheckHashPassword(user.Password, _newPasswordForm.OldPassword),
539+
)
532540
middleware.SetFlashError(c, "check hash password is wrong!")
533541
log.Warn("END request method POST for update user by password: [-]failure")
534542
return c.Render(http.StatusForbidden, "user-view-password.html", echo.Map{

db/db.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ var connection *gorm.DB
1414
var db *sql.DB
1515
var err error
1616

17-
// Init: new database
17+
// Init: new database ["PROD" or "DEV"]
1818
func Init(env string) *gorm.DB {
1919
var connectString string
2020
configuration := config.GetConfig()

models/user.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,19 @@ type UserCity struct {
3030
*/
3131

3232
// User: FindAll(db, admin_or_user={admin, user})
33-
func (User) FindAll(db *gorm.DB, admin_or_user ...string) ([]User, error) {
33+
func (user User) FindAll(db *gorm.DB, admin_or_user ...string) ([]User, error) {
3434
users := []User{}
3535

3636
var err error
3737

3838
// same,
39-
// if len(*admin_or_user) == 0 || len(*admin_or_user) == 1 && (*admin_or_user)[0] == "all" {...}
40-
if isAll(&admin_or_user) {
39+
// if len(admin_or_user) == 0 || len(admin_or_user) == 1 && admin_or_user[0] == "all" {...}
40+
if user.isAll(&admin_or_user) {
4141
// Limit: 25 ?
4242
err = db.Limit(25).Find(&users).Error
43-
} else if isAdmin(&admin_or_user) {
43+
} else if user.isAdmin(&admin_or_user) {
4444
err = db.Limit(25).Where("is_admin = 1").Find(&users).Error
45-
} else if isUser(&admin_or_user) {
45+
} else if user.isUser(&admin_or_user) {
4646
err = db.Limit(25).Where("is_admin = 0").Find(&users).Error
4747
} else { // admin_or_user agrs [2,..]=string
4848
return nil, errors.New(`models.User{}.FirstAll: admin_or_user agrs [2]{"admin", "user"}=string`)
@@ -179,14 +179,14 @@ func (user User) Delete(db *gorm.DB, id int) error {
179179
}
180180

181181
// is? all, admin or user?
182-
func isAll(admin_or_user *[]string) bool {
182+
func (User) isAll(admin_or_user *[]string) bool {
183183
return len(*admin_or_user) == 0 || len(*admin_or_user) == 1 && (*admin_or_user)[0] == "all"
184184
}
185185

186-
func isAdmin(admin_or_user *[]string) bool {
186+
func (User) isAdmin(admin_or_user *[]string) bool {
187187
return len(*admin_or_user) == 1 && (*admin_or_user)[0] == "admin"
188188
}
189189

190-
func isUser(admin_or_user *[]string) bool {
190+
func (User) isUser(admin_or_user *[]string) bool {
191191
return len(*admin_or_user) == 1 && (*admin_or_user)[0] == "user"
192192
}

template/template.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func rootedPathName() (dir string) {
138138
}
139139

140140
// TODO: Linux and MacOS: ok. Windows: ...?
141-
regex := regexp.MustCompile("/test$")
141+
regex := regexp.MustCompile("/tests$")
142142
match := regex.Match([]byte(dir))
143143

144144
if match {

test/README.md renamed to tests/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
### golang-website-example/test
1+
### golang-website-example/tests
22

3-
#### Test the packages
3+
#### Tests the packages
44

55
```
66
$ go test
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package test
1+
package tests
22

33
import (
44
"net/http"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package test
1+
package tests
22

33
import (
44
"net/http"

test/db_test.go renamed to tests/db_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package test
1+
package tests
22

33
import (
44
"testing"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package test
1+
package tests
22

33
import (
44
"net/http"

0 commit comments

Comments
 (0)