1
1
'use strict' ;
2
2
const expect = require ( 'chai' ) . expect ;
3
3
const sinon = require ( 'sinon' ) ;
4
- const os = require ( 'os' ) ;
5
4
const fs = require ( 'fs' ) ;
6
5
7
- const execa = require ( 'execa' ) ;
8
6
const errors = require ( '../../../../../lib/errors' ) ;
7
+ const ghostUser = require ( '../../../../../lib/utils/use-ghost-user' ) ;
9
8
10
9
const loggedInUserOwner = require ( '../../../../../lib/commands/doctor/checks/logged-in-user-owner' ) ;
11
10
12
11
describe ( 'Unit: Doctor Checks > loggedInUserOwner' , function ( ) {
13
12
const sandbox = sinon . sandbox . create ( ) ;
14
- let osStub ;
15
-
16
- beforeEach ( ( ) => {
17
- osStub = sandbox . stub ( os , 'platform' ) . returns ( 'linux' ) ;
18
- } )
19
13
20
14
afterEach ( ( ) => {
21
15
sandbox . restore ( ) ;
22
16
} ) ;
23
17
24
18
it ( 'enabled works' , function ( ) {
25
19
expect ( loggedInUserOwner . enabled ( {
26
- instance : { process : { name : 'local' } }
27
- } ) , 'false if process name is local' ) . to . be . false ;
28
- } ) ;
29
-
30
- it ( 'skip works' , function ( ) {
31
- osStub . returns ( 'win32' ) ;
32
- expect ( loggedInUserOwner . skip ( ) , 'true if platform is not linux' ) . to . be . true ;
33
- expect ( osStub . calledOnce ) . to . be . true ;
20
+ system : { platform : { linux : false } }
21
+ } ) , 'false if platform is not linux' ) . to . be . false ;
34
22
} ) ;
35
23
36
24
describe ( 'Ghost user' , function ( ) {
37
25
it ( 'rejects if user is logged in as ghost and ghost owns content folder' , function ( ) {
38
26
const uidStub = sandbox . stub ( process , 'getuid' ) . returns ( 1002 ) ;
39
27
const gidStub = sandbox . stub ( process , 'getgroups' ) . returns ( [ 30 , 1002 ] ) ;
40
- const execaStub = sandbox . stub ( execa , 'shellSync' ) ;
28
+ const ghostUserStub = sandbox . stub ( ghostUser , 'getGhostUid' ) . returns ( { uid : 1002 , guid : 1002 } ) ;
41
29
const fsStub = sandbox . stub ( fs , 'lstatSync' ) ;
42
30
43
- execaStub . onFirstCall ( ) . returns ( { stdout : '1002' } ) ;
44
- execaStub . onSecondCall ( ) . returns ( { stdout : '1002' } ) ;
45
31
fsStub . onFirstCall ( ) . returns ( { uid : 1000 , gid : 1000 } ) ;
46
32
fsStub . onSecondCall ( ) . returns ( { uid : 1002 , gid : 1002 } ) ;
47
33
48
- return loggedInUserOwner . task ( ) . then ( ( ) => {
34
+ try {
35
+ loggedInUserOwner . task ( )
49
36
expect ( false , 'error should have been thrown' ) . to . be . true ;
50
- } ) . catch ( ( error ) => {
37
+ } catch ( error ) {
51
38
expect ( error ) . to . exist ;
52
39
expect ( uidStub . calledOnce ) . to . be . true ;
53
40
expect ( gidStub . calledOnce ) . to . be . true ;
54
- expect ( execaStub . calledWithExactly ( 'id -u ghost' ) ) . to . be . true ;
55
- expect ( execaStub . calledWithExactly ( 'id -g ghost' ) ) . to . be . true ;
41
+ expect ( ghostUserStub . calledOnce ) . to . be . true ;
56
42
expect ( error ) . to . be . an . instanceof ( errors . SystemError ) ;
57
43
expect ( error . message ) . to . match ( / Y o u c a n ' t u s e G h o s t w i t h t h e g h o s t u s e r ./ ) ;
58
- } ) ;
44
+ }
59
45
} ) ;
60
46
61
47
it ( 'resolves if user is logged in as ghost but ghost doesn\'t own the content folder' , function ( ) {
62
48
const uidStub = sandbox . stub ( process , 'getuid' ) . returns ( 1002 ) ;
63
49
const gidStub = sandbox . stub ( process , 'getgroups' ) . returns ( [ 30 , 1002 ] ) ;
64
- const execaStub = sandbox . stub ( execa , 'shellSync' ) ;
50
+ const ghostUserStub = sandbox . stub ( ghostUser , 'getGhostUid' ) . returns ( { uid : 1002 , guid : 1002 } ) ;
65
51
const fsStub = sandbox . stub ( fs , 'lstatSync' ) ;
66
52
67
- execaStub . onFirstCall ( ) . returns ( { stdout : '1002' } ) ;
68
- execaStub . onSecondCall ( ) . returns ( { stdout : '1002' } ) ;
69
53
fsStub . onFirstCall ( ) . returns ( { uid : 1002 , gid : 1002 } ) ;
70
54
fsStub . onSecondCall ( ) . returns ( { uid : 1001 , gid : 1001 } ) ;
71
55
72
- return loggedInUserOwner . task ( ) . then ( ( ) => {
73
- expect ( uidStub . calledOnce ) . to . be . true ;
74
- expect ( gidStub . calledOnce ) . to . be . true ;
75
- expect ( execaStub . calledWithExactly ( 'id -u ghost' ) ) . to . be . true ;
76
- expect ( execaStub . calledWithExactly ( 'id -g ghost' ) ) . to . be . true ;
77
- } ) ;
56
+ loggedInUserOwner . task ( ) ;
57
+ expect ( uidStub . calledOnce ) . to . be . true ;
58
+ expect ( gidStub . calledOnce ) . to . be . true ;
59
+ expect ( ghostUserStub . calledOnce ) . to . be . true ;
78
60
} ) ;
79
61
} ) ;
80
62
81
63
describe ( 'Other users' , function ( ) {
82
64
it ( 'rejects if current user is not owner and not in the same group as owner' , function ( ) {
83
65
const uidStub = sandbox . stub ( process , 'getuid' ) . returns ( 1000 ) ;
84
66
const gidStub = sandbox . stub ( process , 'getgroups' ) . returns ( [ 30 , 1000 ] ) ;
85
- const execaStub = sandbox . stub ( execa , 'shellSync' ) ;
67
+ const ghostUserStub = sandbox . stub ( ghostUser , 'getGhostUid' ) . returns ( { uid : 1002 , guid : 1002 } ) ;
86
68
const fsStub = sandbox . stub ( fs , 'lstatSync' ) ;
87
69
88
- execaStub . onFirstCall ( ) . returns ( { stdout : '1002' } ) ;
89
- execaStub . onSecondCall ( ) . returns ( { stdout : '1002' } ) ;
90
70
fsStub . onFirstCall ( ) . returns ( { uid : 1001 , gid : 1001 } ) ;
91
71
fsStub . onSecondCall ( ) . returns ( { uid : 1002 , gid : 1002 } ) ;
92
72
93
- return loggedInUserOwner . task ( ) . then ( ( ) => {
73
+ try {
74
+ loggedInUserOwner . task ( ) ;
94
75
expect ( false , 'error should have been thrown' ) . to . be . true ;
95
- } ) . catch ( ( error ) => {
76
+ } catch ( error ) {
96
77
expect ( error ) . to . exist ;
97
78
expect ( uidStub . calledOnce ) . to . be . true ;
98
79
expect ( gidStub . calledOnce ) . to . be . true ;
99
- expect ( execaStub . calledWithExactly ( 'id -u ghost' ) ) . to . be . true ;
100
- expect ( execaStub . calledWithExactly ( 'id -g ghost' ) ) . to . be . true ;
80
+ expect ( ghostUserStub . calledOnce ) . to . be . true ;
101
81
expect ( error ) . to . be . an . instanceof ( errors . SystemError ) ;
102
82
expect ( error . message ) . to . match ( / Y o u r c u r r e n t u s e r i s n o t t h e o w n e r o f t h e G h o s t d i r e c t o r y a n d a l s o n o t p a r t o f t h e s a m e g r o u p ./ ) ;
103
- } ) ;
83
+ }
104
84
} ) ;
105
85
106
86
it ( 'shows a warning message, if user is logged in as different user than owner, but same group' , function ( ) {
107
87
const uidStub = sandbox . stub ( process , 'getuid' ) . returns ( 1001 ) ;
108
88
const gidStub = sandbox . stub ( process , 'getgroups' ) . returns ( [ 30 , 1000 ] ) ;
109
- const execaStub = sandbox . stub ( execa , 'shellSync' ) ;
89
+ const ghostUserStub = sandbox . stub ( ghostUser , 'getGhostUid' ) . returns ( { uid : 1002 , guid : 1002 } ) ;
110
90
const fsStub = sandbox . stub ( fs , 'lstatSync' ) ;
111
91
const logStub = sandbox . stub ( ) ;
112
92
113
93
const ctx = {
114
94
ui : { log : logStub }
115
95
} ;
116
96
117
- execaStub . onFirstCall ( ) . returns ( { stdout : '1002' } ) ;
118
- execaStub . onSecondCall ( ) . returns ( { stdout : '1002' } ) ;
119
97
fsStub . onFirstCall ( ) . returns ( { uid : 1000 , gid : 1000 } ) ;
120
98
fsStub . onSecondCall ( ) . returns ( { uid : 1002 , gid : 1002 } ) ;
121
99
122
- return loggedInUserOwner . task ( ctx ) . then ( ( ) => {
123
- expect ( uidStub . calledOnce ) . to . be . true ;
124
- expect ( gidStub . calledOnce ) . to . be . true ;
125
- expect ( logStub . calledOnce ) . to . be . true ;
126
- expect ( logStub . args [ 0 ] [ 0 ] ) . to . match ( / T h e c u r r e n t u s e r i s n o t t h e o w n e r o f t h e G h o s t d i r e c t o r y . T h i s m i g h t c a u s e p r o b l e m s ./ ) ;
127
- expect ( execaStub . calledWithExactly ( 'id -u ghost' ) ) . to . be . true ;
128
- expect ( execaStub . calledWithExactly ( 'id -g ghost' ) ) . to . be . true ;
129
- } ) ;
100
+ loggedInUserOwner . task ( ctx ) ;
101
+ expect ( uidStub . calledOnce ) . to . be . true ;
102
+ expect ( gidStub . calledOnce ) . to . be . true ;
103
+ expect ( logStub . calledOnce ) . to . be . true ;
104
+ expect ( logStub . args [ 0 ] [ 0 ] ) . to . match ( / T h e c u r r e n t u s e r i s n o t t h e o w n e r o f t h e G h o s t d i r e c t o r y . T h i s m i g h t c a u s e p r o b l e m s ./ ) ;
105
+ expect ( ghostUserStub . calledOnce ) . to . be . true ;
130
106
} ) ;
131
107
132
108
it ( 'resolves if current user is also the owner' , function ( ) {
133
109
const uidStub = sandbox . stub ( process , 'getuid' ) . returns ( 1000 ) ;
134
110
const gidStub = sandbox . stub ( process , 'getgroups' ) . returns ( [ 30 , 1000 ] ) ;
135
111
// Ghost user doesn't exist this time
136
- const execaStub = sandbox . stub ( execa , 'shellSync ' ) . throws ( new Error ( 'no such user' ) ) ;
112
+ const ghostUserStub = sandbox . stub ( ghostUser , 'getGhostUid ' ) . returns ( false ) ;
137
113
const fsStub = sandbox . stub ( fs , 'lstatSync' ) ;
138
114
const logStub = sandbox . stub ( ) ;
139
115
@@ -144,12 +120,27 @@ describe('Unit: Doctor Checks > loggedInUserOwner', function () {
144
120
fsStub . onFirstCall ( ) . returns ( { uid : 1000 , gid : 1000 } ) ;
145
121
fsStub . onSecondCall ( ) . returns ( { uid : 1002 , gid : 1002 } ) ;
146
122
147
- return loggedInUserOwner . task ( ctx ) . then ( ( ) => {
148
- expect ( uidStub . calledOnce ) . to . be . true ;
149
- expect ( gidStub . calledOnce ) . to . be . true ;
150
- expect ( logStub . calledOnce ) . to . be . false ;
151
- expect ( execaStub . calledOnce ) . to . be . true ;
152
- } ) ;
123
+ loggedInUserOwner . task ( ctx ) ;
124
+ expect ( uidStub . calledOnce ) . to . be . true ;
125
+ expect ( gidStub . calledOnce ) . to . be . true ;
126
+ expect ( logStub . calledOnce ) . to . be . false ;
127
+ expect ( ghostUserStub . calledOnce ) . to . be . true ;
128
+ } ) ;
129
+
130
+ it ( 'rejects and passes the error if ghostUser util throws error' , function ( ) {
131
+ const uidStub = sandbox . stub ( process , 'getuid' ) . returns ( 1000 ) ;
132
+ const gidStub = sandbox . stub ( process , 'getgroups' ) . returns ( [ 30 , 1000 ] ) ;
133
+ // getGhostUid throws error this time
134
+ const ghostUserStub = sandbox . stub ( ghostUser , 'getGhostUid' ) . returns ( false ) ;
135
+ const fsStub = sandbox . stub ( fs , 'lstatSync' ) ;
136
+
137
+ fsStub . onFirstCall ( ) . returns ( { uid : 1000 , gid : 1000 } ) ;
138
+ fsStub . onSecondCall ( ) . returns ( { uid : 1002 , gid : 1002 } ) ;
139
+
140
+ loggedInUserOwner . task ( ) ;
141
+ expect ( uidStub . calledOnce ) . to . be . true ;
142
+ expect ( gidStub . calledOnce ) . to . be . true ;
143
+ expect ( ghostUserStub . calledOnce ) . to . be . true ;
153
144
} ) ;
154
145
} ) ;
155
146
} ) ;
0 commit comments