loading up the forgejo repo on tangled to test page performance
1// Copyright 2024 The Forgejo Authors. All rights reserved.
2// SPDX-License-Identifier: MIT
3
4package integration
5
6import (
7 "net/url"
8 "testing"
9
10 "forgejo.org/models/db"
11 "forgejo.org/models/unittest"
12 user_model "forgejo.org/models/user"
13 "forgejo.org/tests"
14
15 "github.com/stretchr/testify/assert"
16 "github.com/stretchr/testify/require"
17)
18
19func Test_Cmd_AdminUser(t *testing.T) {
20 onGiteaRun(t, func(*testing.T, *url.URL) {
21 for _, testCase := range []struct {
22 name string
23 options []string
24 mustChangePassword bool
25 }{
26 {
27 name: "default",
28 options: []string{},
29 mustChangePassword: true,
30 },
31 {
32 name: "--must-change-password=false",
33 options: []string{"--must-change-password=false"},
34 mustChangePassword: false,
35 },
36 {
37 name: "--must-change-password=true",
38 options: []string{"--must-change-password=true"},
39 mustChangePassword: true,
40 },
41 {
42 name: "--must-change-password",
43 options: []string{"--must-change-password"},
44 mustChangePassword: true,
45 },
46 } {
47 t.Run(testCase.name, func(t *testing.T) {
48 defer tests.PrintCurrentTest(t)()
49 name := "testuser"
50
51 options := []string{"user", "create", "--username", name, "--password", "password", "--email", name + "@example.com"}
52 options = append(options, testCase.options...)
53 output, err := runMainApp("admin", options...)
54 require.NoError(t, err)
55 assert.Contains(t, output, "has been successfully created")
56 user := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: name})
57 assert.Equal(t, testCase.mustChangePassword, user.MustChangePassword)
58
59 options = []string{"user", "change-password", "--username", name, "--password", "password"}
60 options = append(options, testCase.options...)
61 output, err = runMainApp("admin", options...)
62 require.NoError(t, err)
63 assert.Contains(t, output, "has been successfully updated")
64 user = unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: name})
65 assert.Equal(t, testCase.mustChangePassword, user.MustChangePassword)
66
67 _, err = runMainApp("admin", "user", "delete", "--username", name)
68 require.NoError(t, err)
69 unittest.AssertNotExistsBean(t, &user_model.User{Name: name})
70 })
71 }
72 })
73}
74
75func Test_Cmd_AdminFirstUser(t *testing.T) {
76 onGiteaRun(t, func(*testing.T, *url.URL) {
77 for _, testCase := range []struct {
78 name string
79 options []string
80 mustChangePassword bool
81 isAdmin bool
82 }{
83 {
84 name: "default",
85 options: []string{},
86 mustChangePassword: false,
87 isAdmin: false,
88 },
89 {
90 name: "--must-change-password=false",
91 options: []string{"--must-change-password=false"},
92 mustChangePassword: false,
93 isAdmin: false,
94 },
95 {
96 name: "--must-change-password=true",
97 options: []string{"--must-change-password=true"},
98 mustChangePassword: true,
99 isAdmin: false,
100 },
101 {
102 name: "--must-change-password",
103 options: []string{"--must-change-password"},
104 mustChangePassword: true,
105 isAdmin: false,
106 },
107 {
108 name: "--admin default",
109 options: []string{"--admin"},
110 mustChangePassword: false,
111 isAdmin: true,
112 },
113 {
114 name: "--admin --must-change-password=false",
115 options: []string{"--admin", "--must-change-password=false"},
116 mustChangePassword: false,
117 isAdmin: true,
118 },
119 {
120 name: "--admin --must-change-password=true",
121 options: []string{"--admin", "--must-change-password=true"},
122 mustChangePassword: true,
123 isAdmin: true,
124 },
125 {
126 name: "--admin --must-change-password",
127 options: []string{"--admin", "--must-change-password"},
128 mustChangePassword: true,
129 isAdmin: true,
130 },
131 } {
132 t.Run(testCase.name, func(t *testing.T) {
133 db.GetEngine(db.DefaultContext).Exec("DELETE FROM `user`")
134 db.GetEngine(db.DefaultContext).Exec("DELETE FROM `email_address`")
135 assert.Equal(t, int64(0), user_model.CountUsers(db.DefaultContext, nil))
136 name := "testuser"
137
138 options := []string{"user", "create", "--username", name, "--password", "password", "--email", name + "@example.com"}
139 options = append(options, testCase.options...)
140 output, err := runMainApp("admin", options...)
141 require.NoError(t, err)
142 assert.Contains(t, output, "has been successfully created")
143 user := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: name})
144 assert.Equal(t, testCase.mustChangePassword, user.MustChangePassword)
145 assert.Equal(t, testCase.isAdmin, user.IsAdmin)
146 })
147 }
148 })
149}