3
3
const expect = require ( 'chai' ) . expect ;
4
4
const sinon = require ( 'sinon' ) ;
5
5
const os = require ( 'os' ) ;
6
+ const fs = require ( 'fs' ) ;
6
7
const checkRootUser = require ( '../../../lib/utils/check-root-user' ) ;
7
8
8
9
describe ( 'Unit: Utils > checkRootUser' , function ( ) {
@@ -16,7 +17,7 @@ describe('Unit: Utils > checkRootUser', function () {
16
17
const osStub = sandbox . stub ( os , 'platform' ) . returns ( 'win32' ) ;
17
18
const processStub = sandbox . stub ( process , 'getuid' ) . returns ( 0 ) ;
18
19
19
- checkRootUser ( 'test ' ) ;
20
+ checkRootUser ( 'install ' ) ;
20
21
expect ( osStub . calledOnce ) . to . be . true ;
21
22
expect ( processStub . called ) . to . be . false ;
22
23
} ) ;
@@ -25,22 +26,139 @@ describe('Unit: Utils > checkRootUser', function () {
25
26
const osStub = sandbox . stub ( os , 'platform' ) . returns ( 'darwin' ) ;
26
27
const processStub = sandbox . stub ( process , 'getuid' ) . returns ( 0 ) ;
27
28
28
- checkRootUser ( 'test ' ) ;
29
+ checkRootUser ( 'doctor ' ) ;
29
30
expect ( osStub . calledOnce ) . to . be . true ;
30
31
expect ( processStub . called ) . to . be . false ;
31
32
} ) ;
32
33
33
- it ( 'throws error command run with root' , function ( ) {
34
+ it ( 'skips check if command run as non root user ' , function ( ) {
34
35
const osStub = sandbox . stub ( os , 'platform' ) . returns ( 'linux' ) ;
36
+ const processStub = sandbox . stub ( process , 'getuid' ) . returns ( 501 ) ;
37
+ const exitStub = sandbox . stub ( process , 'exit' ) . throws ( ) ;
38
+ const errorStub = sandbox . stub ( console , 'error' ) ;
39
+
40
+ checkRootUser ( 'update' ) ;
41
+ expect ( osStub . calledOnce ) . to . be . true ;
42
+ expect ( processStub . calledOnce ) . to . be . true ;
43
+ expect ( errorStub . calledOnce ) . to . be . false ;
44
+ expect ( exitStub . calledOnce ) . to . be . false ;
45
+ } ) ;
46
+
47
+ it ( 'shows special message for DigitalOcean One-Click installs' , function ( ) {
48
+ const osStub = sandbox . stub ( os , 'platform' ) . returns ( 'linux' ) ;
49
+ const fsStub = sandbox . stub ( fs , 'existsSync' ) ;
35
50
const processStub = sandbox . stub ( process , 'getuid' ) . returns ( 0 ) ;
36
51
const exitStub = sandbox . stub ( process , 'exit' ) . throws ( ) ;
37
52
const errorStub = sandbox . stub ( console , 'error' ) ;
38
53
54
+ fsStub . withArgs ( '/root/.digitalocean_password' ) . returns ( true ) ;
55
+ fsStub . withArgs ( '/var/www/ghost/.ghost-cli' ) . returns ( true ) ;
56
+
39
57
try {
40
- checkRootUser ( 'test ' ) ;
58
+ checkRootUser ( 'ls ' ) ;
41
59
throw new Error ( 'should not be thrown' ) ;
42
60
} catch ( e ) {
43
61
expect ( e . message ) . to . not . equal ( 'should not be thrown' ) ;
62
+ expect ( fsStub . calledWithExactly ( '/root/.digitalocean_password' ) ) . to . be . true ;
63
+ expect ( osStub . calledOnce ) . to . be . true ;
64
+ expect ( processStub . calledOnce ) . to . be . true ;
65
+ expect ( errorStub . calledOnce ) . to . be . true ;
66
+ expect ( exitStub . calledOnce ) . to . be . true ;
67
+ expect ( errorStub . args [ 0 ] [ 0 ] ) . to . match ( / W e d i s c o v e r e d t h a t y o u a r e u s i n g t h e D i g i t a l o c e a n O n e - C l i c k i n s t a l l ./ ) ;
68
+ }
69
+ } ) ;
70
+
71
+ it ( 'shows special message for DigitalOcean One-Click installs, but doesn\'t exit on `stop`' , function ( ) {
72
+ const osStub = sandbox . stub ( os , 'platform' ) . returns ( 'linux' ) ;
73
+ const fsStub = sandbox . stub ( fs , 'existsSync' ) ;
74
+ const processStub = sandbox . stub ( process , 'getuid' ) . returns ( 0 ) ;
75
+ const exitStub = sandbox . stub ( process , 'exit' ) . throws ( ) ;
76
+ const errorStub = sandbox . stub ( console , 'error' ) ;
77
+
78
+ fsStub . withArgs ( '/root/.digitalocean_password' ) . returns ( true ) ;
79
+ fsStub . withArgs ( '/var/www/ghost/.ghost-cli' ) . returns ( true ) ;
80
+
81
+ checkRootUser ( 'stop' ) ;
82
+ expect ( fsStub . calledWithExactly ( '/root/.digitalocean_password' ) ) . to . be . true ;
83
+ expect ( osStub . calledOnce ) . to . be . true ;
84
+ expect ( processStub . calledOnce ) . to . be . true ;
85
+ expect ( errorStub . calledOnce ) . to . be . true ;
86
+ expect ( exitStub . calledOnce ) . to . be . false ;
87
+ expect ( errorStub . args [ 0 ] [ 0 ] ) . to . match ( / W e d i s c o v e r e d t h a t y o u a r e u s i n g t h e D i g i t a l o c e a n O n e - C l i c k i n s t a l l ./ ) ;
88
+ } ) ;
89
+
90
+ it ( 'shows special message for root installs' , function ( ) {
91
+ const osStub = sandbox . stub ( os , 'platform' ) . returns ( 'linux' ) ;
92
+ const cwdStub = sandbox . stub ( process , 'cwd' ) . returns ( '/var/www/ghost' ) ;
93
+ const fsStub = sandbox . stub ( fs , 'existsSync' ) ;
94
+ const fsStatStub = sandbox . stub ( fs , 'statSync' ) . returns ( { uid : 0 } ) ;
95
+ const processStub = sandbox . stub ( process , 'getuid' ) . returns ( 0 ) ;
96
+ const exitStub = sandbox . stub ( process , 'exit' ) . throws ( ) ;
97
+ const errorStub = sandbox . stub ( console , 'error' ) ;
98
+
99
+ fsStub . withArgs ( '/root/.digitalocean_password' ) . returns ( false ) ;
100
+ fsStub . withArgs ( '/var/www/ghost/.ghost-cli' ) . returns ( true ) ;
101
+
102
+ try {
103
+ checkRootUser ( 'ls' ) ;
104
+ throw new Error ( 'should not be thrown' ) ;
105
+ } catch ( e ) {
106
+ expect ( e . message ) . to . not . equal ( 'should not be thrown' ) ;
107
+ expect ( cwdStub . calledOnce ) . to . be . true ;
108
+ expect ( fsStub . calledWithExactly ( '/root/.digitalocean_password' ) ) . to . be . true ;
109
+ expect ( fsStub . calledWithExactly ( '/var/www/ghost/.ghost-cli' ) ) . to . be . true ;
110
+ expect ( fsStatStub . calledWithExactly ( '/var/www/ghost/.ghost-cli' ) ) . to . be . true ;
111
+ expect ( osStub . calledOnce ) . to . be . true ;
112
+ expect ( processStub . calledOnce ) . to . be . true ;
113
+ expect ( errorStub . calledOnce ) . to . be . true ;
114
+ expect ( exitStub . calledOnce ) . to . be . true ;
115
+ expect ( errorStub . args [ 0 ] [ 0 ] ) . to . match ( / I t s e e m s G h o s t w a s i n s t a l l e d u s i n g t h e r o o t u s e r ./ ) ;
116
+ }
117
+ } ) ;
118
+
119
+ it ( 'shows special message for root installs, but doesn\'t exit on `start`' , function ( ) {
120
+ const osStub = sandbox . stub ( os , 'platform' ) . returns ( 'linux' ) ;
121
+ const cwdStub = sandbox . stub ( process , 'cwd' ) . returns ( '/var/www/ghost' ) ;
122
+ const fsStub = sandbox . stub ( fs , 'existsSync' ) ;
123
+ const fsStatStub = sandbox . stub ( fs , 'statSync' ) . returns ( { uid : 0 } ) ;
124
+ const processStub = sandbox . stub ( process , 'getuid' ) . returns ( 0 ) ;
125
+ const exitStub = sandbox . stub ( process , 'exit' ) . throws ( ) ;
126
+ const errorStub = sandbox . stub ( console , 'error' ) ;
127
+
128
+ fsStub . withArgs ( '/root/.digitalocean_password' ) . returns ( false ) ;
129
+ fsStub . withArgs ( '/var/www/ghost/.ghost-cli' ) . returns ( true ) ;
130
+
131
+ checkRootUser ( 'start' ) ;
132
+ expect ( cwdStub . calledOnce ) . to . be . true ;
133
+ expect ( fsStub . calledWithExactly ( '/root/.digitalocean_password' ) ) . to . be . true ;
134
+ expect ( fsStub . calledWithExactly ( '/var/www/ghost/.ghost-cli' ) ) . to . be . true ;
135
+ expect ( fsStatStub . calledWithExactly ( '/var/www/ghost/.ghost-cli' ) ) . to . be . true ;
136
+ expect ( osStub . calledOnce ) . to . be . true ;
137
+ expect ( processStub . calledOnce ) . to . be . true ;
138
+ expect ( errorStub . calledOnce ) . to . be . true ;
139
+ expect ( exitStub . calledOnce ) . to . be . false ;
140
+ expect ( errorStub . args [ 0 ] [ 0 ] ) . to . match ( / I t s e e m s G h o s t w a s i n s t a l l e d u s i n g t h e r o o t u s e r ./ ) ;
141
+ } ) ;
142
+
143
+ it ( 'throws error command run with root for non-root installs' , function ( ) {
144
+ const osStub = sandbox . stub ( os , 'platform' ) . returns ( 'linux' ) ;
145
+ const cwdStub = sandbox . stub ( process , 'cwd' ) . returns ( '/var/www/ghost' ) ;
146
+ const fsStub = sandbox . stub ( fs , 'existsSync' ) ;
147
+ const fsStatStub = sandbox . stub ( fs , 'statSync' ) . returns ( { uid : 501 } ) ;
148
+ const processStub = sandbox . stub ( process , 'getuid' ) . returns ( 0 ) ;
149
+ const exitStub = sandbox . stub ( process , 'exit' ) . throws ( ) ;
150
+ const errorStub = sandbox . stub ( console , 'error' ) ;
151
+
152
+ fsStub . withArgs ( '/root/.digitalocean_password' ) . returns ( false ) ;
153
+ fsStub . withArgs ( '/var/www/ghost/.ghost-cli' ) . returns ( true ) ;
154
+
155
+ try {
156
+ checkRootUser ( 'update' ) ;
157
+ throw new Error ( 'should not be thrown' ) ;
158
+ } catch ( e ) {
159
+ expect ( e . message ) . to . not . equal ( 'should not be thrown' ) ;
160
+ expect ( cwdStub . calledOnce ) . to . be . true ;
161
+ expect ( fsStatStub . calledWithExactly ( '/var/www/ghost/.ghost-cli' ) ) . to . be . true ;
44
162
expect ( osStub . calledOnce ) . to . be . true ;
45
163
expect ( processStub . calledOnce ) . to . be . true ;
46
164
expect ( errorStub . calledOnce ) . to . be . true ;
@@ -49,16 +167,76 @@ describe('Unit: Utils > checkRootUser', function () {
49
167
}
50
168
} ) ;
51
169
52
- it ( 'doesn\'t do anything if command run as non root user ' , function ( ) {
170
+ it ( 'throws error command run with root for non- root installs, but doesn\'t exit on `restart` ' , function ( ) {
53
171
const osStub = sandbox . stub ( os , 'platform' ) . returns ( 'linux' ) ;
54
- const processStub = sandbox . stub ( process , 'getuid' ) . returns ( 501 ) ;
172
+ const cwdStub = sandbox . stub ( process , 'cwd' ) . returns ( '/var/www/ghost' ) ;
173
+ const fsStub = sandbox . stub ( fs , 'existsSync' ) ;
174
+ const fsStatStub = sandbox . stub ( fs , 'statSync' ) . returns ( { uid : 501 } ) ;
175
+ const processStub = sandbox . stub ( process , 'getuid' ) . returns ( 0 ) ;
55
176
const exitStub = sandbox . stub ( process , 'exit' ) . throws ( ) ;
56
177
const errorStub = sandbox . stub ( console , 'error' ) ;
57
178
58
- checkRootUser ( 'test' ) ;
179
+ fsStub . withArgs ( '/root/.digitalocean_password' ) . returns ( false ) ;
180
+ fsStub . withArgs ( '/var/www/ghost/.ghost-cli' ) . returns ( true ) ;
181
+
182
+ checkRootUser ( 'restart' ) ;
183
+ expect ( cwdStub . calledOnce ) . to . be . true ;
184
+ expect ( fsStatStub . calledWithExactly ( '/var/www/ghost/.ghost-cli' ) ) . to . be . true ;
59
185
expect ( osStub . calledOnce ) . to . be . true ;
60
186
expect ( processStub . calledOnce ) . to . be . true ;
61
- expect ( errorStub . calledOnce ) . to . be . false ;
187
+ expect ( errorStub . calledOnce ) . to . be . true ;
188
+ expect ( exitStub . calledOnce ) . to . be . false ;
189
+ expect ( errorStub . args [ 0 ] [ 0 ] ) . to . match ( / C a n ' t r u n c o m m a n d a s ' r o o t ' u s e r / ) ;
190
+ } ) ;
191
+
192
+ it ( 'throws error command run with root outside of valid ghost installation' , function ( ) {
193
+ const osStub = sandbox . stub ( os , 'platform' ) . returns ( 'linux' ) ;
194
+ const cwdStub = sandbox . stub ( process , 'cwd' ) . returns ( '/var/www/' ) ;
195
+ const fsStub = sandbox . stub ( fs , 'existsSync' ) ;
196
+ const fsStatStub = sandbox . stub ( fs , 'statSync' ) . returns ( { uid : 501 } ) ;
197
+ const processStub = sandbox . stub ( process , 'getuid' ) . returns ( 0 ) ;
198
+ const exitStub = sandbox . stub ( process , 'exit' ) . throws ( ) ;
199
+ const errorStub = sandbox . stub ( console , 'error' ) ;
200
+
201
+ fsStub . withArgs ( '/root/.digitalocean_password' ) . returns ( false ) ;
202
+ fsStub . withArgs ( '/var/www/.ghost-cli' ) . returns ( false ) ;
203
+
204
+ try {
205
+ checkRootUser ( 'update' ) ;
206
+ throw new Error ( 'should not be thrown' ) ;
207
+ } catch ( e ) {
208
+ expect ( e . message ) . to . not . equal ( 'should not be thrown' ) ;
209
+ expect ( cwdStub . calledOnce ) . to . be . true ;
210
+ expect ( fsStub . calledWithExactly ( '/var/www/.ghost-cli' ) ) . to . be . true ;
211
+ expect ( fsStatStub . calledOnce ) . to . be . false ;
212
+ expect ( osStub . calledOnce ) . to . be . true ;
213
+ expect ( processStub . calledOnce ) . to . be . true ;
214
+ expect ( errorStub . calledOnce ) . to . be . true ;
215
+ expect ( exitStub . calledOnce ) . to . be . true ;
216
+ expect ( errorStub . args [ 0 ] [ 0 ] ) . to . match ( / C a n ' t r u n c o m m a n d a s ' r o o t ' u s e r / ) ;
217
+ }
218
+ } ) ;
219
+
220
+ it ( 'throws error command run with root outside of valid ghost installation, but doesn\'t exit on `restart`' , function ( ) {
221
+ const osStub = sandbox . stub ( os , 'platform' ) . returns ( 'linux' ) ;
222
+ const cwdStub = sandbox . stub ( process , 'cwd' ) . returns ( '/var/www/' ) ;
223
+ const fsStub = sandbox . stub ( fs , 'existsSync' ) ;
224
+ const fsStatStub = sandbox . stub ( fs , 'statSync' ) . returns ( { uid : 501 } ) ;
225
+ const processStub = sandbox . stub ( process , 'getuid' ) . returns ( 0 ) ;
226
+ const exitStub = sandbox . stub ( process , 'exit' ) . throws ( ) ;
227
+ const errorStub = sandbox . stub ( console , 'error' ) ;
228
+
229
+ fsStub . withArgs ( '/root/.digitalocean_password' ) . returns ( false ) ;
230
+ fsStub . withArgs ( '/var/www/.ghost-cli' ) . returns ( false ) ;
231
+
232
+ checkRootUser ( 'restart' ) ;
233
+ expect ( cwdStub . calledOnce ) . to . be . true ;
234
+ expect ( fsStub . calledWithExactly ( '/var/www/.ghost-cli' ) ) . to . be . true ;
235
+ expect ( fsStatStub . calledOnce ) . to . be . false ;
236
+ expect ( osStub . calledOnce ) . to . be . true ;
237
+ expect ( processStub . calledOnce ) . to . be . true ;
238
+ expect ( errorStub . calledOnce ) . to . be . true ;
62
239
expect ( exitStub . calledOnce ) . to . be . false ;
240
+ expect ( errorStub . args [ 0 ] [ 0 ] ) . to . match ( / C a n ' t r u n c o m m a n d a s ' r o o t ' u s e r / ) ;
63
241
} ) ;
64
242
} ) ;
0 commit comments