Skip to content

Commit 698b2e0

Browse files
committed
nss: add tests for DisablePasswd/EnablePasswd API methods
1 parent 1accccd commit 698b2e0

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed

internal/services/nss/nss_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,68 @@ func TestGetShadowEntries(t *testing.T) {
256256
}
257257
}
258258

259+
func TestDisablePasswd(t *testing.T) {
260+
tests := map[string]struct {
261+
sourceDB string
262+
263+
username string
264+
currentUserNotRoot bool
265+
266+
wantErr bool
267+
}{
268+
"Successfully_disable_user": {username: "user1"},
269+
270+
"Error_when_username_is_empty": {wantErr: true},
271+
"Error_when_user_does_not_exist": {username: "doesnotexist", wantErr: true},
272+
"Error_when_not_root": {username: "notroot", currentUserNotRoot: true, wantErr: true},
273+
}
274+
for name, tc := range tests {
275+
t.Run(name, func(t *testing.T) {
276+
client := newNSSClient(t, tc.sourceDB, tc.currentUserNotRoot)
277+
278+
_, err := client.DisablePasswd(context.Background(), &authd.DisablePasswdRequest{Name: tc.username})
279+
if tc.wantErr {
280+
require.Error(t, err, "DisablePasswd should return an error, but did not")
281+
return
282+
}
283+
require.NoError(t, err, "DisablePasswd should not return an error, but did")
284+
})
285+
}
286+
}
287+
288+
func TestEnablePasswd(t *testing.T) {
289+
tests := map[string]struct {
290+
sourceDB string
291+
292+
username string
293+
currentUserNotRoot bool
294+
295+
wantErr bool
296+
}{
297+
"Successfully_enable_user": {username: "user1"},
298+
299+
"Error_when_username_is_empty": {wantErr: true},
300+
"Error_when_user_does_not_exist": {username: "doesnotexist", wantErr: true},
301+
"Error_when_not_root": {username: "notroot", currentUserNotRoot: true, wantErr: true},
302+
}
303+
for name, tc := range tests {
304+
t.Run(name, func(t *testing.T) {
305+
if tc.sourceDB == "" {
306+
tc.sourceDB = "disabled-user.db.yaml"
307+
}
308+
309+
client := newNSSClient(t, tc.sourceDB, tc.currentUserNotRoot)
310+
311+
_, err := client.EnablePasswd(context.Background(), &authd.EnablePasswdRequest{Name: tc.username})
312+
if tc.wantErr {
313+
require.Error(t, err, "EnablePasswd should return an error, but did not")
314+
return
315+
}
316+
require.NoError(t, err, "EnablePasswd should not return an error, but did")
317+
})
318+
}
319+
}
320+
259321
func TestMockgpasswd(t *testing.T) {
260322
localgroupstestutils.Mockgpasswd(t)
261323
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
users:
2+
- name: user1
3+
uid: 1111
4+
gid: 11111
5+
gecos: |-
6+
User1 gecos
7+
On multiple lines
8+
dir: /home/user1
9+
shell: /bin/bash
10+
broker_id: broker-id
11+
disabled: true
12+
- name: user2
13+
uid: 2222
14+
gid: 22222
15+
gecos: User2
16+
dir: /home/user2
17+
shell: /bin/dash
18+
broker_id: broker-id
19+
- name: user3
20+
uid: 3333
21+
gid: 33333
22+
gecos: User3
23+
dir: /home/user3
24+
shell: /bin/zsh
25+
broker_id: broker-id
26+
groups:
27+
- name: group1
28+
gid: 11111
29+
ugid: group1
30+
- name: group2
31+
gid: 22222
32+
ugid: group2
33+
- name: group3
34+
gid: 33333
35+
ugid: group3
36+
- name: commongroup
37+
gid: 99999
38+
ugid: commongroup
39+
users_to_groups:
40+
- uid: 1111
41+
gid: 11111
42+
- uid: 2222
43+
gid: 22222
44+
- uid: 2222
45+
gid: 99999
46+
- uid: 3333
47+
gid: 33333
48+
- uid: 3333
49+
gid: 99999

0 commit comments

Comments
 (0)