Skip to content

Commit 111854c

Browse files
committed
[30/12/22] test controller DeteleUser: id=3 x
1 parent fc55a24 commit 111854c

15 files changed

+132
-172
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ golang-website-example
88
golang-website-example.exe
99

1010
# air
11+
bin
1112
.air.toml
1213
tmp

controllers/home_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func (controller Controller) Home(c echo.Context) error {
3232
var user models.User
3333
if err := controller.DB.Select("name").Where(
3434
"username = ?", session.Values["username"],
35-
).First(&user); err.Error != nil { // TODO: why?
35+
).First(&user); err.Error != nil { // why?
3636
log.Warnf(`session values "username" error: %v`, err.Error)
3737
}
3838

controllers/user_controller.go

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ func (controller *Controller) Users(c echo.Context) error {
2929
})
3030
log.Info("START request method GET for users")
3131

32+
log.Println(session.ID)
33+
log.Println(session.IsNew)
34+
log.Println(session.Options)
35+
log.Println(session.Values)
36+
3237
is_auth_type := session.Values["is_auth_type"]
3338
if is_auth_type == -1 {
3439
log.Warn("for GET to users without no-session [@route: /login]")
@@ -149,7 +154,7 @@ func (controller *Controller) CreateUser(c echo.Context) error {
149154
}
150155

151156
// userForm: type of a user
152-
_userForm := types.UserForm{
157+
userForm := types.UserForm{
153158
Username: c.FormValue("username"),
154159
Email: c.FormValue("email"),
155160
Password: c.FormValue("password"),
@@ -159,19 +164,19 @@ func (controller *Controller) CreateUser(c echo.Context) error {
159164
Photo: c.FormValue("photo"),
160165
}
161166

162-
// _userForm: Validate of a validate user
167+
// userForm: Validate of a validate user
163168
err := validation.Errors{
164169
"username": validation.Validate(
165-
_userForm.Username, validation.Required, validation.Length(4, 15),
170+
userForm.Username, validation.Required, validation.Length(4, 15),
166171
),
167-
"email": validation.Validate(_userForm.Email, validation.Required, is.Email),
172+
"email": validation.Validate(userForm.Email, validation.Required, is.EmailFormat),
168173
"password": validation.Validate(
169-
_userForm.Password, validation.Required, validation.Length(6, 18),
170-
validation.By(types.PasswordEquals(_userForm.ConfirmPassword)),
174+
userForm.Password, validation.Required, validation.Length(6, 18),
175+
validation.By(types.PasswordEquals(userForm.ConfirmPassword)),
171176
),
172-
"name": validation.Validate(_userForm.Name, validation.Required),
173-
"city": validation.Validate(_userForm.City),
174-
"photo": validation.Validate(_userForm.Photo),
177+
"name": validation.Validate(userForm.Name, validation.Required),
178+
"city": validation.Validate(userForm.City),
179+
"photo": validation.Validate(userForm.Photo),
175180
}.Filter()
176181
/* if err = validation.Errors{...}.Filter(); err != nil {
177182
...
@@ -196,20 +201,20 @@ func (controller *Controller) CreateUser(c echo.Context) error {
196201
}
197202

198203
// Password Hash
199-
hash, err := middleware.PasswordHash(_userForm.Password)
204+
hash, err := middleware.PasswordHash(userForm.Password)
200205
if err != nil {
201206
log.Warnf("for POST to create user without middleware.PasswordHash error: `%v`", err)
202207
log.Warn("END request method POST for create user: [-]failure")
203208
return err
204209
}
205210

206211
user := models.User{
207-
Username: _userForm.Username,
208-
Email: _userForm.Email,
212+
Username: userForm.Username,
213+
Email: userForm.Email,
209214
Password: hash,
210-
Name: _userForm.Name,
211-
City: _userForm.City,
212-
Photo: _userForm.Photo,
215+
Name: userForm.Name,
216+
City: userForm.City,
217+
Photo: userForm.Photo,
213218
}
214219

215220
// _, err := user.Save(...): be able
@@ -403,9 +408,9 @@ func (controller *Controller) UpdateUser(c echo.Context) error {
403408
Email: c.FormValue("email"),
404409
Name: c.FormValue("name"),
405410
City: city,
406-
// TODO: photo
411+
// TODO: photo, insyaallah
407412
Photo: "",
408-
// TODO: is admin
413+
// TODO: is admin, insyaallah
409414
IsAdmin: 0,
410415
}
411416

@@ -666,8 +671,11 @@ func (controller *Controller) DeleteUser(c echo.Context) error {
666671
for example:
667672
username ockibagusp delete 'ockibagusp': ok
668673
username ockibagusp delete 'sugriwa': no
674+
675+
insyaallah
669676
*/
670677
_, err = models.User{}.FirstByIDAndUsername(
678+
// ???
671679
controller.DB, id, session.Values["username"].(string),
672680
)
673681

golang-website-example.txt

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Insyaallah, New tasks:
2+
[] test without csrf
3+
4+
[] test controller DeteleUser: id=3
5+
controllers/user_controller.go:678
6+
...
7+
time="2022-12-30T14:54:47+07:00" level=info msg="START request method GET for delete user" route="/users/delete/:id -> id:3" username="<nil>"
8+
{"time":"2022-12-30T14:54:47.752300052+07:00","level":"-","prefix":"echo","file":"recover.go","line":"92","message":"[PANIC RECOVER] interface conversion: interface {} is nil, not string goroutine 289 [running]:\ngithub.com/labstack/echo/v4/middleware.RecoverWithConfig.func1.1.1()\n\t/home/ockibagusp/go/src/github.com/ockibagusp/golang-website-example/vendor/github.com/labstack/echo/v4/middleware/recover.go:77 +0x10d\npanic({0xaec560, 0xc0004f9680})\n\t/usr/lib/golang/src/runtime/panic.go:884 +0x212\ngithub.com/ockibagusp/golang-website-example/controllers.(*Controller).DeleteUser(0x11273e8, {0xcbfed0, 0xc0000a4280})\n\t/home/ockibagusp/go/src/github.com/ockibagusp/golang-website-example/controllers/user_controller.go:678 +0xc65\ngithub.com/labstack/echo/v4.(*Echo).add.func1({0xcbfed0, 0xc0000a4280})\n\t/home/ockibagusp/go/src/github.com/ockibagusp/golang-website-example/vendor/github.com/labstack/echo/v4/echo.go:552 +0x51\ngithub.com/labstack/echo-contrib/session.MiddlewareWithConfig.func1.1({0xcbfed0, 0xc0000a4280})\n\t/home/ockibagusp/go/src/github.com/ockibagusp/golang-website-example/vendor/github.com/labstack/echo-contrib/session/session.go:70 +0x105\ngithub.com/labstack/echo/v4/middleware.RecoverWithConfig.func1.1({0xcbfed0, 0xc0000a4280})\n\t/home/ockibagusp/go/src/github.com/ockibagusp/golang-website-example/vendor/github.com/labstack/echo/v4/middleware/recover.go:98 +0xf8\ngithub.com/labstack/echo/v4/middleware.LoggerWithConfig.func2.1({0xcbfed0, 0xc0000a4280})\n\t/home/ockibagusp/go/src/github.com/ockibagusp/golang-website-example/vendor/github.com/labstack/echo/v4/middleware/logger.go:117 +0xe2\ngithub.com/labstack/echo/v4.(*Echo).ServeHTTP(0xc000358d80, {0xcb7450?, 0xc0003f72c0}, 0xc000521200)\n\t/home/ockibagusp/go/src/github.com/ockibagusp/golang-website-example/vendor/github.com/labstack/echo/v4/echo.go:662 +0x3d1\ngithub.com/gavv/httpexpect/v2.Binder.RoundTrip({{0xcb2620?, 0xc000358d80?}, 0x0?}, 0xc000521100)\n\t/home/ockibagusp/go/src/github.com/ockibagusp/golang-website-example/vendor/github.com/gavv/httpexpect/v2/binder.go:64 +0x3f4\nnet/http.send(0xc000521100, {0xcb34a0, 0xc000482a38}, {0xb75360?, 0xc00002d501?, 0x0?})\n\t/usr/lib/golang/src/net/http/client.go:251 +0x5f7\nnet/http.(*Client).send(0xc0003935f0, 0xc000521100, {0x29?, 0x36?, 0x0?})\n\t/usr/lib/golang/src/net/http/client.go:175 +0x9b\nnet/http.(*Client).do(0xc0003935f0, 0xc000521100)\n\t/usr/lib/golang/src/net/http/client.go:715 +0x8fc\nnet/http.(*Client).Do(0xc000437a00?, 0x48f5d7?)\n\t/usr/lib/golang/src/net/http/client.go:581 +0x19\ngithub.com/gavv/httpexpect/v2.(*Request).sendRequest.func1()\n\t/home/ockibagusp/go/src/github.com/ockibagusp/golang-website-example/vendor/github.com/gavv/httpexpect/v2/request.go:1276 +0x2d\ngithub.com/gavv/httpexpect/v2.(*Request).retryRequest.func1(0xc000060bb0?, 0xc000437b88, 0xc000423ae8, 0xc000423b18, 0xc000423ab0)\n\t/home/ockibagusp/go/src/github.com/ockibagusp/golang-website-example/vendor/github.com/gavv/httpexpect/v2/request.go:1344 +0x19e\ngithub.com/gavv/httpexpect/v2.(*Request).retryRequest(0xc0005042c0, 0x0?)\n\t/home/ockibagusp/go/src/github.com/ockibagusp/golang-website-example/vendor/github.com/gavv/httpexpect/v2/request.go:1346 +0x2bc\ngithub.com/gavv/httpexpect/v2.(*Request).sendRequest(0xc0005042c0)\n\t/home/ockibagusp/go/src/github.com/ockibagusp/golang-website-example/vendor/github.com/gavv/httpexpect/v2/request.go:1275 +0x4c\ngithub.com/gavv/httpexpect/v2.(*Request).roundTrip(0xc0005042c0)\n\t/home/ockibagusp/go/src/github.com/ockibagusp/golang-website-example/vendor/github.com/gavv/httpexpect/v2/request.go:1194 +0x12c\ngithub.com/gavv/httpexpect/v2.(*Request).Expect(0xc0005042c0)\n\t/home/ockibagusp/go/src/github.com/ockibagusp/golang-website-example/vendor/github.com/gavv/httpexpect/v2/request.go:1155 +0x36\ngithub.com/ockibagusp/golang-website-example/tests.TestDeleteUserController.func1(0xc0000cda00)\n\t/home/ockibagusp/go/src/github.com/ockibagusp/golang-website-example/tests/user_controller_test.go:1604 +0xd5\ntesting.tRunner(0xc0000cda00, 0xc0005b05a0)\n\t/usr/lib/golang/src/testing/testing.go:1446 +0x10b\ncreated by testing.(*T).Run\n\t/usr/lib/golang/src/testing/testing.go:1493 +0x35f\n\ngoroutine 1 [chan receive]:\ntesting.(*T).Run(0xc00008a680, {0xba2483?, 0x5234e5?}, 0xbec030)\n\t/usr/lib/golang/src/testing/testing.go:1494 \n"}
9+
{"time":"2022-12-30T14:54:47.754727104+07:00","id":"","remote_ip":"","host":"127.0.0.1:38137","method":"GET","uri":"/users/delete/3","user_agent":"","status":500,"error":"","latency":4300702,"latency_human":"4.300702ms","bytes_in":0,"bytes_out":36}
10+
time="2022-12-30T14:54:47+07:00" level=warning msg="for GET to delete user without no-session [@route: /login]" route="/users/delete/:id -> id:1" username=
11+
...
12+
13+
[] test main_test.go func setupTestSetCookie(...)
14+
admin v
15+
sugriwa x
16+
subali x
17+
18+
19+
# Completed task:
20+
1. [-] testing session
21+
[-] Set-Cookie headers there isn't any: session
22+
23+
[-] 2 missing users
24+
Func: TestLogin
25+
Users:
26+
- admin - ok x
27+
- sugriwa - ok x
28+
- subali - ok x
29+
- ockibagusp - no v
30+
31+
[-] UndeclaredName: setupTestHandler v
32+
https://pkg.go.dev/golang.org/x/tools/internal/typesinternal#UndeclaredName
33+
34+
package tests v
35+
session.go x
36+
- for example: setupTestHandler() -> UndeclaredName
37+
move:
38+
- session.go to session_test.go v
39+
40+
2. [x] controller UpdateUser: validation

main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
)
1414

1515
func main() {
16+
os.Setenv("session_test", "0")
17+
1618
// controllers init
1719
controllers := controllers.New()
1820

router/router.go

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package router
22

33
import (
4+
"os"
5+
46
"github.com/gorilla/sessions"
57
"github.com/labstack/echo-contrib/session"
68
"github.com/labstack/echo/v4"
@@ -20,27 +22,35 @@ func New(controllers *controllers.Controller) (router *echo.Echo) {
2022
router.Use(middleware.Recover())
2123

2224
/*
23-
set env. "session_test": [all], [session] or [CSRF]
25+
Insyaallah, TODO: .env session_test: {1} or {0}
26+
session_test: {true} or {false}
27+
28+
1. os.Setenv("session_test", ...)
29+
@session_test: {true} or {1}
30+
os.Setenv("session_test", "true") or,
31+
os.Setenv("session_test", "1")
32+
33+
@session_test: {false} or {0}
34+
os.Setenv("session_test", "false") or,
35+
os.Setenv("session_test", "0")
2436
*/
25-
// if os.Getenv("session_test") == "all" || os.Getenv("session_test") == "session" {
2637
logrus.Println("Setenv: session_test = session")
27-
// TODO: .env cookie store ?
2838
router.Use(session.Middleware(sessions.NewCookieStore(
2939
[]byte("something-very-secret"),
3040
)))
31-
// }
3241

33-
// if os.Getenv("session_test") == "all" || os.Getenv("session_test") == "CSRF" {
34-
logrus.Println("Setenv: session_test = CSRF")
35-
router.Use(middleware.CSRFWithConfig(middleware.CSRFConfig{
36-
// Optional. Default value "header:X-CSRF-Token".
37-
// Possible values:
38-
// - "header:<name>"
39-
// - "form:<name>"
40-
// - "query:<name>"
41-
TokenLookup: "form:X-CSRF-Token",
42-
}))
43-
// }
42+
// PROD
43+
if os.Getenv("session_test") == "0" || os.Getenv("session_test") == "false" {
44+
logrus.Println("Setenv: session_test = CSRF")
45+
router.Use(middleware.CSRFWithConfig(middleware.CSRFConfig{
46+
// Optional. Default value "header:X-CSRF-Token".
47+
// Possible values:
48+
// - "header:<name>"
49+
// - "form:<name>"
50+
// - "query:<name>"
51+
TokenLookup: "form:X-CSRF-Token",
52+
}))
53+
}
4454

4555
// Instantiate a template registry with an array of template set
4656
router.Renderer = template.NewTemplates()

template/func_map.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import (
55
"strconv"
66
)
77

8-
// TODO: why?
9-
108
// function map more
119
var FuncMapMore = func() template.FuncMap {
1210
list := template.FuncMap{
@@ -16,7 +14,7 @@ var FuncMapMore = func() template.FuncMap {
1614
return list
1715
}
1816

19-
// TODO
17+
// TODO, insyaallah
2018
//
2119
// Code: session_gorilla.Values["s_auth_type"]
2220
// HTML: {{index $.session_gorilla.Values "is_auth_type" | toString}} ?

template/template.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,11 @@ func parseFileHTMLOnly(name string) *template.Template {
136136
func rootedPathName() (dir string) {
137137
dir, err := os.Getwd()
138138
if err != nil {
139-
// TODO: Docker, Kubernetes ex.?
139+
// Docker, Kubernetes ex.? insyaallah
140140
log.Fatal(err)
141141
}
142142

143-
// TODO: Linux and MacOS: ok. Windows: ...?
143+
// Linux and MacOS: ok. Windows: ...? insyaallah
144144
regex := regexp.MustCompile("/tests$")
145145
match := regex.Match([]byte(dir))
146146

tests/home_controller_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func TestHomeController(t *testing.T) {
7171

7272
result_body := result.Body().Raw()
7373

74-
// TODO: why?
74+
// why?
7575
var regex *regexp.Regexp
7676
var match string
7777
if test.html_navbar.must_compile != "" {

0 commit comments

Comments
 (0)