1
1
'use strict' ;
2
-
2
+ const fs = require ( 'fs' ) ;
3
+ const path = require ( 'path' ) ;
3
4
const expect = require ( 'chai' ) . expect ;
4
5
const sinon = require ( 'sinon' ) ;
5
6
const proxyquire = require ( 'proxyquire' ) . noCallThru ( ) ;
@@ -17,6 +18,9 @@ const context = {
17
18
}
18
19
} ;
19
20
21
+ const sslWithoutLe = fs . readFileSync ( path . join ( __dirname , './fixtures/ssl-without-le.txt' ) , { encoding : 'utf8' } ) ;
22
+ const oldSslWithLe = fs . readFileSync ( path . join ( __dirname , './fixtures/old-ssl-with-le.txt' ) , { encoding : 'utf8' } ) ;
23
+
20
24
describe ( 'Unit: Extensions > Nginx > Migrations' , function ( ) {
21
25
describe ( 'migrateSSL' , function ( ) {
22
26
it ( 'skips if ssl is not set up' , function ( ) {
@@ -34,12 +38,61 @@ describe('Unit: Extensions > Nginx > Migrations', function () {
34
38
expect ( skipStub . calledOnce ) . to . be . true ;
35
39
} ) ;
36
40
41
+ it ( 'skips if cert has not been generated using the old method' , function ( ) {
42
+ const skip = sinon . stub ( ) ;
43
+ const existsSync = sinon . stub ( ) ;
44
+
45
+ existsSync . withArgs ( '/var/www/ghost/system/files/ghost.org-ssl.conf' ) . returns ( true ) ;
46
+ existsSync . withArgs ( '/home/ghost/.acme.sh/ghost.org' ) . returns ( false ) ;
47
+
48
+ const migrate = proxyquire ( modulePath , {
49
+ 'fs-extra' : { existsSync : existsSync } ,
50
+ os : { homedir : ( ) => '/home/ghost' }
51
+ } ) ;
52
+
53
+ migrate . migrateSSL ( context , { skip : skip } ) ;
54
+
55
+ expect ( existsSync . calledTwice ) . to . be . true ;
56
+ expect ( existsSync . calledWithExactly ( '/var/www/ghost/system/files/ghost.org-ssl.conf' ) ) . to . be . true ;
57
+ expect ( existsSync . calledWithExactly ( '/home/ghost/.acme.sh/ghost.org' ) ) . to . be . true ;
58
+ expect ( skip . calledOnce ) . to . be . true ;
59
+ } ) ;
60
+
61
+ it ( 'skips if ssl conf isn\'t using an LE cert' , function ( ) {
62
+ const skip = sinon . stub ( ) ;
63
+ const existsSync = sinon . stub ( ) ;
64
+ const readFileSync = sinon . stub ( ) ;
65
+
66
+ const confFile = '/var/www/ghost/system/files/ghost.org-ssl.conf' ;
67
+
68
+ existsSync . withArgs ( confFile ) . returns ( true ) ;
69
+ existsSync . withArgs ( '/home/ghost/.acme.sh/ghost.org' ) . returns ( true ) ;
70
+ readFileSync . withArgs ( confFile ) . returns ( sslWithoutLe ) ;
71
+
72
+ const migrate = proxyquire ( modulePath , {
73
+ 'fs-extra' : { existsSync : existsSync , readFileSync : readFileSync } ,
74
+ os : { homedir : ( ) => '/home/ghost' }
75
+ } ) ;
76
+
77
+ migrate . migrateSSL ( context , { skip : skip } ) ;
78
+
79
+ expect ( existsSync . calledTwice ) . to . be . true ;
80
+ expect ( existsSync . calledWithExactly ( '/var/www/ghost/system/files/ghost.org-ssl.conf' ) ) . to . be . true ;
81
+ expect ( existsSync . calledWithExactly ( '/home/ghost/.acme.sh/ghost.org' ) ) . to . be . true ;
82
+ expect ( readFileSync . calledOnce ) . to . be . true ;
83
+ expect ( readFileSync . calledWithExactly ( confFile , { encoding : 'utf8' } ) ) . to . be . true ;
84
+ expect ( skip . calledOnce ) . to . be . true ;
85
+ } ) ;
86
+
37
87
it ( 'throws an error if it can\'t parse the letsencrypt account email' , function ( ) {
38
- const existsStub = sinon . stub ( ) . returns ( true ) ;
39
- const rfsStub = sinon . stub ( ) . returns ( '' ) ;
88
+ const existsSync = sinon . stub ( ) . returns ( true ) ;
89
+ const readFileSync = sinon . stub ( ) ;
90
+
91
+ readFileSync . onFirstCall ( ) . returns ( oldSslWithLe ) ;
92
+ readFileSync . onSecondCall ( ) . returns ( '' ) ;
40
93
41
94
const migrate = proxyquire ( modulePath , {
42
- 'fs-extra' : { existsSync : existsStub , readFileSync : rfsStub } ,
95
+ 'fs-extra' : { existsSync : existsSync , readFileSync : readFileSync } ,
43
96
os : { homedir : ( ) => '/home/ghost' }
44
97
} ) ;
45
98
@@ -50,13 +103,18 @@ describe('Unit: Extensions > Nginx > Migrations', function () {
50
103
expect ( e ) . to . be . an . instanceof ( cli . errors . SystemError ) ;
51
104
expect ( e . message ) . to . equal ( 'Unable to parse letsencrypt account email' ) ;
52
105
53
- expect ( rfsStub . calledWithExactly ( '/home/ghost/.acme.sh/account.conf' ) ) ;
106
+ expect ( readFileSync . calledTwice ) . to . be . true ;
107
+ expect ( readFileSync . calledWithExactly ( '/home/ghost/.acme.sh/account.conf' , { encoding : 'utf8' } ) ) . to . be . true ;
54
108
}
55
109
} ) ;
56
110
57
111
it ( 'runs tasks correctly' , function ( ) {
58
- const existsStub = sinon . stub ( ) . returns ( true ) ;
59
- const rfsStub = sinon . stub ( ) . returns ( 'ACCOUNT_EMAIL=\'[email protected] \'\n' ) ;
112
+ const existsSync = sinon . stub ( ) . returns ( true ) ;
113
+ const readFileSync = sinon . stub ( ) ;
114
+
115
+ readFileSync . onFirstCall ( ) . returns ( oldSslWithLe ) ;
116
+ readFileSync . onSecondCall ( ) . returns ( 'ACCOUNT_EMAIL=\'[email protected] \'\n' ) ;
117
+
60
118
const restartStub = sinon . stub ( ) . resolves ( ) ;
61
119
const replaceStub = sinon . stub ( ) . resolves ( ) ;
62
120
@@ -70,7 +128,7 @@ describe('Unit: Extensions > Nginx > Migrations', function () {
70
128
} ;
71
129
72
130
const migrate = proxyquire ( modulePath , {
73
- 'fs-extra' : { existsSync : existsStub , readFileSync : rfsStub } ,
131
+ 'fs-extra' : { existsSync : existsSync , readFileSync : readFileSync } ,
74
132
'replace-in-file' : replaceStub ,
75
133
'./acme' : acme ,
76
134
os : { homedir : ( ) => '/home/ghost' }
@@ -80,8 +138,8 @@ describe('Unit: Extensions > Nginx > Migrations', function () {
80
138
81
139
fn ( context ) ;
82
140
83
- expect ( existsStub . calledOnce ) . to . be . true ;
84
- expect ( rfsStub . calledOnce ) . to . be . true ;
141
+ expect ( existsSync . calledTwice ) . to . be . true ;
142
+ expect ( readFileSync . calledTwice ) . to . be . true ;
85
143
expect ( ui . listr . calledOnce ) . to . be . true ;
86
144
87
145
const tasks = ui . listr . getCall ( 0 ) . args [ 0 ] ;
@@ -112,7 +170,7 @@ describe('Unit: Extensions > Nginx > Migrations', function () {
112
170
return tasks [ 4 ] . task ( ) ;
113
171
} ) . then ( ( ) => {
114
172
expect ( acme . remove . calledOnce ) . to . be . true ;
115
- expect ( acme . remove . calledWithExactly ( 'ghost.org' , ui , '/home/ghost/.acme.sh' ) ) ;
173
+ expect ( acme . remove . calledWithExactly ( 'ghost.org' , ui , '/home/ghost/.acme.sh' ) ) . to . be . true ;
116
174
} ) ;
117
175
} ) ;
118
176
} ) ;
0 commit comments