summaryrefslogtreecommitdiffstats
path: root/tests/integration/cmd_admin_test.go
blob: 576b09eefd6a17b57f8b7536f2a09dfe7ee36fad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright 2024 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package integration

import (
	"net/url"
	"testing"

	"code.gitea.io/gitea/models/db"
	"code.gitea.io/gitea/models/unittest"
	user_model "code.gitea.io/gitea/models/user"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func Test_Cmd_AdminUser(t *testing.T) {
	onGiteaRun(t, func(*testing.T, *url.URL) {
		for _, testCase := range []struct {
			name               string
			options            []string
			mustChangePassword bool
		}{
			{
				name:               "default",
				options:            []string{},
				mustChangePassword: true,
			},
			{
				name:               "--must-change-password=false",
				options:            []string{"--must-change-password=false"},
				mustChangePassword: false,
			},
			{
				name:               "--must-change-password=true",
				options:            []string{"--must-change-password=true"},
				mustChangePassword: true,
			},
			{
				name:               "--must-change-password",
				options:            []string{"--must-change-password"},
				mustChangePassword: true,
			},
		} {
			t.Run(testCase.name, func(t *testing.T) {
				name := "testuser"

				options := []string{"user", "create", "--username", name, "--password", "password", "--email", name + "@example.com"}
				options = append(options, testCase.options...)
				output, err := runMainApp("admin", options...)
				require.NoError(t, err)
				assert.Contains(t, output, "has been successfully created")
				user := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: name})
				assert.Equal(t, testCase.mustChangePassword, user.MustChangePassword)

				options = []string{"user", "change-password", "--username", name, "--password", "password"}
				options = append(options, testCase.options...)
				output, err = runMainApp("admin", options...)
				require.NoError(t, err)
				assert.Contains(t, output, "has been successfully updated")
				user = unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: name})
				assert.Equal(t, testCase.mustChangePassword, user.MustChangePassword)

				_, err = runMainApp("admin", "user", "delete", "--username", name)
				require.NoError(t, err)
				unittest.AssertNotExistsBean(t, &user_model.User{Name: name})
			})
		}
	})
}

func Test_Cmd_AdminFirstUser(t *testing.T) {
	onGiteaRun(t, func(*testing.T, *url.URL) {
		for _, testCase := range []struct {
			name               string
			options            []string
			mustChangePassword bool
			isAdmin            bool
		}{
			{
				name:               "default",
				options:            []string{},
				mustChangePassword: false,
				isAdmin:            false,
			},
			{
				name:               "--must-change-password=false",
				options:            []string{"--must-change-password=false"},
				mustChangePassword: false,
				isAdmin:            false,
			},
			{
				name:               "--must-change-password=true",
				options:            []string{"--must-change-password=true"},
				mustChangePassword: true,
				isAdmin:            false,
			},
			{
				name:               "--must-change-password",
				options:            []string{"--must-change-password"},
				mustChangePassword: true,
				isAdmin:            false,
			},
			{
				name:               "--admin default",
				options:            []string{"--admin"},
				mustChangePassword: false,
				isAdmin:            true,
			},
			{
				name:               "--admin --must-change-password=false",
				options:            []string{"--admin", "--must-change-password=false"},
				mustChangePassword: false,
				isAdmin:            true,
			},
			{
				name:               "--admin --must-change-password=true",
				options:            []string{"--admin", "--must-change-password=true"},
				mustChangePassword: true,
				isAdmin:            true,
			},
			{
				name:               "--admin --must-change-password",
				options:            []string{"--admin", "--must-change-password"},
				mustChangePassword: true,
				isAdmin:            true,
			},
		} {
			t.Run(testCase.name, func(t *testing.T) {
				db.GetEngine(db.DefaultContext).Exec("DELETE FROM `user`")
				db.GetEngine(db.DefaultContext).Exec("DELETE FROM `email_address`")
				assert.Equal(t, int64(0), user_model.CountUsers(db.DefaultContext, nil))
				name := "testuser"

				options := []string{"user", "create", "--username", name, "--password", "password", "--email", name + "@example.com"}
				options = append(options, testCase.options...)
				output, err := runMainApp("admin", options...)
				require.NoError(t, err)
				assert.Contains(t, output, "has been successfully created")
				user := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: name})
				assert.Equal(t, testCase.mustChangePassword, user.MustChangePassword)
				assert.Equal(t, testCase.isAdmin, user.IsAdmin)
			})
		}
	})
}