Skip to content

Commit ad7a085

Browse files
authored
Merge pull request #18 from ockibagusp/test-without-new-session
Test without session
2 parents 94e3d15 + 3e8d4b7 commit ad7a085

13 files changed

+629
-474
lines changed

controllers/home_controller.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
func (controller Controller) Home(c echo.Context) error {
2121
// Please note the the second parameter "home.html" is the template name and should
2222
// be equal to one of the keys in the TemplateRegistry array defined in main.go
23+
// ?
2324
session, _ := middleware.GetAuth(c)
2425
log := log.WithFields(log.Fields{
2526
"username": session.Values["username"],

controllers/user_controller.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,6 @@ 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-
3732
is_auth_type := session.Values["is_auth_type"]
3833
if is_auth_type == -1 {
3934
log.Warn("for GET to users without no-session [@route: /login]")

golang-website-example.txt

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,82 @@
1+
[v] success
2+
[x] failure
3+
[-] load
4+
15
# Insyaallah, New tasks:
2-
[] test main_test.go func setupTestSetCookie(...)
3-
admin v
4-
sugriwa x
5-
subali x
6+
[] no debug for create models.User
7+
8+
example,
9+
[6.075ms] [rows:0] INSERT INTO `users` ... VALUES (...'unit-test','unit-test@exemple.com','$2a$10$wyzBl/d31ahlHVU52kgkT.cH1lXI9b0cmdiM9Gw2ciyY9C69bTLh6','Unit Test',0,'',0)
10+
...
11+
time="2023-01-14T15:14:14+07:00" level=info msg="START request method POST for create user" route=/users/add username=
12+
time="2023-01-14T15:14:15+07:00" level=info msg="models.User: [+]success" route=/users/add user_success="{{... example example@example.com $2a$10$1cbJNsJNOXk4hwYYVtlUT.B0MwNwp4sRmI2pS9frCuC.0IBLyRsSO Example 0 0}" username=}}
613

14+
[] ...
715

816
# Completed task:
917
1. [-] testing session
10-
[-] Set-Cookie headers there isn't any: session
18+
[x] Set-Cookie headers there isn't any: session
1119

12-
[-] 2 missing users
20+
[x] 2 missing users
1321
Func: TestLogin
1422
Users:
1523
- admin - ok x
1624
- sugriwa - ok x
1725
- subali - ok x
1826
- ockibagusp - no v
1927

20-
[-] UndeclaredName: setupTestHandler v
28+
[x] UndeclaredName: setupTestHandler v
2129
https://pkg.go.dev/golang.org/x/tools/internal/typesinternal#UndeclaredName
2230

2331
package tests v
2432
session.go x
2533
- for example: setupTestHandler() -> UndeclaredName
2634
move:
2735
- session.go to session_test.go v
36+
37+
[-] SetSession and GetAuth
38+
v login
39+
v user controller
40+
v home controller
41+
v about controller
42+
- admin controller
43+
44+
--- FAIL: TestUsersController
45+
- user_controller_test.go:253
46+
- user_controller_test.go:263
47+
48+
http://127.0.0.1:35351/users
49+
http://127.0.0.1:35351/users?admin=all
50+
http://127.0.0.1:35351/users?user=all
51+
http://127.0.0.1:35351/users?false=all
52+
53+
--- FAIL: TestCreateUserController
54+
users_[admin]_to_GET_create_it_success
55+
GET http://127.0.0.1:39181/users/add
56+
57+
- user_controller_test.go:518
58+
59+
--- FAIL: TestCreateUserController/user_[admin]_to_POST_create_it_success
60+
61+
POST http://127.0.0.1:39181/users/add
62+
63+
--- FAIL: TestCreateUserController/users_[admin]_to_POST_create_it_failure:_Duplicate_entry
64+
65+
--- FAIL: TestReadUserController
66+
67+
--- FAIL: TestUpdateUserController
68+
--- FAIL: TestUpdateUserController/users_[admin]_to_admin_GET_update_it_success:_id=1
69+
70+
--- FAIL: TestUpdateUserController/users_[admin]_to_user_GET_update_it_success:_id=2
71+
72+
--- FAIL: TestUpdateUserController/users_[admin]_to_admin_POST_update_it_success:_id=1
73+
74+
--- FAIL: TestUpdateUserByPasswordUserController
75+
--- FAIL: TestUpdateUserByPasswordUserController/users_[admin]_to_GET_update_user_by_password_it_success:_id=1
2876

29-
2. [x] controller UpdateUser: validation
77+
2. [v] controller UpdateUser: validation
3078

31-
3. [x] test controller DeteleUser: id=3
79+
3. [v] test controller DeteleUser: id=3
3280
controllers/user_controller.go:678
3381

3482
name: "users [subali] to [subali] DELETE it success: id=3",
@@ -40,4 +88,4 @@ time="2022-12-30T14:54:47+07:00" level=info msg="START request method GET for de
4088
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=
4189
...
4290

43-
4. [x] test without csrf
91+
4. [v] test without csrf

middleware/session_middleware.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,27 @@
11
package middleware
22

33
import (
4+
"os"
5+
46
"github.com/gorilla/sessions"
57
"github.com/labstack/echo-contrib/session"
68
"github.com/labstack/echo/v4"
79
"github.com/ockibagusp/golang-website-example/models"
10+
"github.com/ockibagusp/golang-website-example/tests/method"
11+
modelsTest "github.com/ockibagusp/golang-website-example/tests/models"
812
)
913

1014
// base.html -> {{if eq ((index .session.Values "is_auth_type") | tostring) -1 }}ok{{end}}
1115

1216
// GetAuth: get session to authenticated
1317
func GetAuth(c echo.Context) (session_gorilla *sessions.Session, err error) {
14-
if session_gorilla, err = session.Get("session", c); err != nil {
15-
return
18+
// Test: session_test = true
19+
if os.Getenv("session_test") == "1" && method.SetSession == false {
20+
session_gorilla, err = modelsTest.GetAuthSession()
21+
} else {
22+
if session_gorilla, err = session.Get("session", c); err != nil {
23+
return
24+
}
1625
}
1726

1827
is_auth_type := session_gorilla.Values["is_auth_type"]
@@ -63,6 +72,11 @@ func GetAdmin(c echo.Context) (session_gorilla *sessions.Session, err error) {
6372

6473
// SetSession: set session from User
6574
func SetSession(user models.User, c echo.Context) (session_gorilla *sessions.Session, err error) {
75+
// Test: session_test = true
76+
if os.Getenv("session_test") == "1" {
77+
modelsTest.SetAuthSession(&user)
78+
}
79+
6680
session_gorilla, err = session.Get("session", c)
6781
if err != nil {
6882
return

tests/about_controller_test.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,40 +6,45 @@ import (
66
"testing"
77

88
"github.com/gavv/httpexpect/v2"
9+
"github.com/ockibagusp/golang-website-example/tests/method"
10+
modelsTest "github.com/ockibagusp/golang-website-example/tests/models"
911
"github.com/stretchr/testify/assert"
1012
)
1113

1214
func TestAboutSuccess(t *testing.T) {
1315
assert := assert.New(t)
1416

1517
no_auth := setupTestServer(t)
16-
auth_admin := setupTestServerAuth(no_auth, 1)
17-
auth_sugriwa := setupTestServerAuth(no_auth, 2)
18+
19+
// test for SetSession = false
20+
method.SetSession = false
21+
// test for db users
22+
truncateUsers(db)
1823

1924
test_cases := []struct {
2025
name string
21-
expect *httpexpect.Expect // auth_admin, session_sugriwa or no-auth
26+
expect string // admin, sugriwa
2227
html_navbar regex
2328
}{
2429
{
2530
name: "about [no-auth] success",
26-
expect: no_auth,
31+
expect: "",
2732
html_navbar: regex{
2833
must_compile: `<a href="/login" class="btn btn-outline-success my-2 my-sm-0">(.*)</a>`,
2934
actual: `<a href="/login" class="btn btn-outline-success my-2 my-sm-0">Login</a>`,
3035
},
3136
},
3237
{
3338
name: "about [admin] success",
34-
expect: auth_admin,
39+
expect: ADMIN,
3540
html_navbar: regex{
3641
must_compile: `<a class="btn">(.*)</a>`,
3742
actual: `<a class="btn">ADMIN</a>`,
3843
},
3944
},
4045
{
4146
name: "home [user] success",
42-
expect: auth_sugriwa,
47+
expect: SUGRIWA,
4348
html_navbar: regex{
4449
must_compile: `<a href="/users" class="btn btn-outline-secondary my-2 my-sm-0">(.*)</a>`,
4550
actual: `<a href="/users" class="btn btn-outline-secondary my-2 my-sm-0">Users</a>`,
@@ -49,10 +54,10 @@ func TestAboutSuccess(t *testing.T) {
4954

5055
for _, test := range test_cases {
5156
var result *httpexpect.Response
52-
expect := test.expect // auth_admin, auth_sugriwa or no-auth
57+
modelsTest.UserSelectTest = test.expect // auth_admin, auth_sugriwa or no-auth
5358

5459
t.Run(test.name, func(t *testing.T) {
55-
result = expect.GET("/about").
60+
result = no_auth.GET("/about").
5661
Expect().
5762
Status(http.StatusOK)
5863

tests/admin_controller_test.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,21 @@ import (
55
"testing"
66

77
"github.com/gavv/httpexpect/v2"
8+
"github.com/ockibagusp/golang-website-example/tests/method"
9+
modelsTest "github.com/ockibagusp/golang-website-example/tests/models"
810
)
911

1012
func TestAdminDeletePermanently(t *testing.T) {
1113
no_auth := setupTestServer(t)
12-
auth_admin := setupTestServerAuth(no_auth, 1)
13-
// auth_sugriwa := setupTestServerAuth(no_auth, 2)
1414

15+
// test for SetSession = false
16+
method.SetSession = false
1517
// test for db users
1618
truncateUsers(db)
1719

1820
test_cases := []struct {
1921
name string
20-
expect *httpexpect.Expect // auth or no-auth
22+
expect string // auth or no-auth
2123
url_query string
2224
status int
2325
}{
@@ -26,7 +28,7 @@ func TestAdminDeletePermanently(t *testing.T) {
2628
*/
2729
{
2830
name: "delete permanently [admin] to GET it success: all",
29-
expect: auth_admin,
31+
expect: ADMIN,
3032
// HTTP response status: 200 OK
3133
status: http.StatusOK,
3234
},
@@ -61,18 +63,18 @@ func TestAdminDeletePermanently(t *testing.T) {
6163
6264
var expect = test.expect
6365
*/
64-
var expect *httpexpect.Expect = test.expect
66+
modelsTest.UserSelectTest = test.expect // ADMIN and SUGRIWA
6567

6668
t.Run(test.name, func(t *testing.T) {
6769
// @route: exemple "/admin/delete-permanently?admin=all"
6870
if test.url_query != "" {
69-
result = expect.GET("/admin/delete-permanently").
71+
result = no_auth.GET("/admin/delete-permanently").
7072
WithQuery(test.url_query, "all").
7173
Expect().
7274
Status(test.status)
7375
} else {
7476
// @route: "/admin/delete-permanently"
75-
result = expect.GET("/admin/delete-permanently").
77+
result = no_auth.GET("/admin/delete-permanently").
7678
Expect().
7779
Status(test.status)
7880
}

tests/home_controller_test.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,30 @@ import (
66
"testing"
77

88
"github.com/gavv/httpexpect/v2"
9+
"github.com/ockibagusp/golang-website-example/tests/method"
10+
modelsTest "github.com/ockibagusp/golang-website-example/tests/models"
911
"github.com/stretchr/testify/assert"
1012
)
1113

1214
func TestHomeController(t *testing.T) {
1315
assert := assert.New(t)
1416

1517
no_auth := setupTestServer(t)
16-
auth_admin := setupTestServerAuth(no_auth, 1)
17-
auth_sugriwa := setupTestServerAuth(no_auth, 2)
18+
19+
// test for SetSession = false
20+
method.SetSession = false
21+
// test for db users
22+
truncateUsers(db)
1823

1924
test_cases := []struct {
2025
name string
21-
expect *httpexpect.Expect // auth_admin, session_sugriwa or no-auth
26+
expect string // admin, sugriwa
2227
html_navbar regex
2328
html_jumbotron regex
2429
}{
2530
{
2631
name: "home [no-auth] success",
27-
expect: no_auth,
32+
expect: "",
2833
html_navbar: regex{
2934
must_compile: `<a href="/login" (.*)</a>`,
3035
actual: `<a href="/login" class="btn btn-outline-success my-2 my-sm-0">Login</a>`,
@@ -36,7 +41,7 @@ func TestHomeController(t *testing.T) {
3641
},
3742
{
3843
name: "home [admin] success",
39-
expect: auth_admin,
44+
expect: ADMIN,
4045
html_navbar: regex{
4146
must_compile: `<a class="btn">(.*)</a>`,
4247
actual: `<a class="btn">ADMIN</a>`,
@@ -48,7 +53,7 @@ func TestHomeController(t *testing.T) {
4853
},
4954
{
5055
name: "home [user] success",
51-
expect: auth_sugriwa,
56+
expect: SUGRIWA,
5257
html_navbar: regex{
5358
must_compile: `<a href="/users" (.*)</a>`,
5459
actual: `<a href="/users" class="btn btn-outline-secondary my-2 my-sm-0">Users</a>`,
@@ -62,10 +67,10 @@ func TestHomeController(t *testing.T) {
6267

6368
for _, test := range test_cases {
6469
var result *httpexpect.Response
65-
expect := test.expect // auth_admin, auth_sugriwa or no-auth
70+
modelsTest.UserSelectTest = test.expect // auth_admin, auth_sugriwa or no-auth
6671

6772
t.Run(test.name, func(t *testing.T) {
68-
result = expect.GET("/").
73+
result = no_auth.GET("/").
6974
Expect().
7075
Status(http.StatusOK)
7176

0 commit comments

Comments
 (0)