| 
									
										
										
										
											2016-08-09 14:05:47 -04:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var PromiseA = require('bluebird'); | 
					
						
							| 
									
										
										
										
											2016-08-09 14:17:26 -04:00
										 |  |  | var path = require('path'); | 
					
						
							| 
									
										
										
										
											2018-12-17 00:43:47 -07:00
										 |  |  | var requestAsync = PromiseA.promisify(require('@coolaj86/request')); | 
					
						
							| 
									
										
										
										
											2016-08-09 14:05:47 -04:00
										 |  |  | var LE = require('../').LE; | 
					
						
							|  |  |  | var le = LE.create({ | 
					
						
							|  |  |  |   server: 'staging' | 
					
						
							|  |  |  | , acme: require('le-acme-core').ACME.create() | 
					
						
							|  |  |  | , store: require('le-store-certbot').create({ | 
					
						
							| 
									
										
										
										
											2016-08-09 14:17:26 -04:00
										 |  |  |     configDir: '~/letsencrypt.test/etc'.split('/').join(path.sep) | 
					
						
							|  |  |  |   , webrootPath: '~/letsencrypt.test/var/:hostname'.split('/').join(path.sep) | 
					
						
							| 
									
										
										
										
											2016-08-09 14:05:47 -04:00
										 |  |  |   }) | 
					
						
							|  |  |  | , challenge: require('le-challenge-fs').create({ | 
					
						
							| 
									
										
										
										
											2016-08-09 14:17:26 -04:00
										 |  |  |     webrootPath: '~/letsencrypt.test/var/:hostname'.split('/').join(path.sep) | 
					
						
							| 
									
										
										
										
											2016-08-09 14:05:47 -04:00
										 |  |  |   }) | 
					
						
							|  |  |  | , debug: true | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | var utils = require('../lib/utils'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if ('/.well-known/acme-challenge/' !== LE.acmeChallengePrefix) { | 
					
						
							|  |  |  |   throw new Error("Bad constant 'acmeChallengePrefix'"); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var baseUrl; | 
					
						
							| 
									
										
										
										
											2016-08-09 14:17:26 -04:00
										 |  |  | // could use localhost as well, but for the sake of an FQDN for testing, we use this
 | 
					
						
							|  |  |  | // also, example.com is just a junk domain to make sure that it is ignored
 | 
					
						
							|  |  |  | // (even though it should always be an array of only one element in lib/core.js)
 | 
					
						
							|  |  |  | var domains = [ 'localhost.daplie.com', 'example.com' ]; // or just localhost
 | 
					
						
							| 
									
										
										
										
											2016-08-09 14:05:47 -04:00
										 |  |  | var token = 'token-id'; | 
					
						
							|  |  |  | var secret = 'key-secret'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var tests = [ | 
					
						
							|  |  |  |   function () { | 
					
						
							|  |  |  |     console.log('Test Url:', baseUrl + token); | 
					
						
							|  |  |  |     return requestAsync({ url: baseUrl + token }).then(function (req) { | 
					
						
							|  |  |  |       if (404 !== req.statusCode) { | 
					
						
							|  |  |  |         console.log(req.statusCode); | 
					
						
							|  |  |  |         throw new Error("Should be status 404"); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | , function () { | 
					
						
							| 
									
										
										
										
											2016-08-09 14:17:26 -04:00
										 |  |  |     var copy = utils.merge({ domains: domains }, le); | 
					
						
							| 
									
										
										
										
											2016-08-09 14:05:47 -04:00
										 |  |  |     copy = utils.tplCopy(copy); | 
					
						
							| 
									
										
										
										
											2016-08-09 14:17:26 -04:00
										 |  |  |     return PromiseA.promisify(le.challenge.set)(copy, domains[0], token, secret); | 
					
						
							| 
									
										
										
										
											2016-08-09 14:05:47 -04:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | , function () { | 
					
						
							|  |  |  |     return requestAsync(baseUrl + token).then(function (req) { | 
					
						
							|  |  |  |       if (200 !== req.statusCode) { | 
					
						
							|  |  |  |         console.log(req.statusCode, req.body); | 
					
						
							|  |  |  |         throw new Error("Should be status 200"); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if (req.body !== secret) { | 
					
						
							|  |  |  |         console.error(token, secret, req.body); | 
					
						
							|  |  |  |         throw new Error("req.body should be secret"); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | , function () { | 
					
						
							| 
									
										
										
										
											2016-08-09 14:17:26 -04:00
										 |  |  |     var copy = utils.merge({ domains: domains }, le); | 
					
						
							| 
									
										
										
										
											2016-08-09 14:05:47 -04:00
										 |  |  |     copy = utils.tplCopy(copy); | 
					
						
							| 
									
										
										
										
											2016-08-09 14:17:26 -04:00
										 |  |  |     return PromiseA.promisify(le.challenge.remove)(copy, domains[0], token); | 
					
						
							| 
									
										
										
										
											2016-08-09 14:05:47 -04:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | , function () { | 
					
						
							|  |  |  |     return requestAsync(baseUrl + token).then(function (req) { | 
					
						
							|  |  |  |       if (404 !== req.statusCode) { | 
					
						
							|  |  |  |         console.log(req.statusCode); | 
					
						
							|  |  |  |         throw new Error("Should be status 404"); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function run() { | 
					
						
							|  |  |  |   //var express = require(express);
 | 
					
						
							|  |  |  |   var server = require('http').createServer(le.middleware()); | 
					
						
							|  |  |  |   server.listen(0, function () { | 
					
						
							|  |  |  |     console.log('Server running, proceeding to test.'); | 
					
						
							| 
									
										
										
										
											2016-08-09 14:17:26 -04:00
										 |  |  |     baseUrl = 'http://' + domains[0] + ':' + server.address().port + LE.acmeChallengePrefix; | 
					
						
							| 
									
										
										
										
											2016-08-09 14:05:47 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     function next() { | 
					
						
							|  |  |  |       var test = tests.shift(); | 
					
						
							|  |  |  |       if (!test) { | 
					
						
							|  |  |  |         console.info('All tests passed'); | 
					
						
							|  |  |  |         server.close(); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       test().then(next, function (err) { | 
					
						
							|  |  |  |         console.error('ERROR'); | 
					
						
							|  |  |  |         console.error(err.stack); | 
					
						
							|  |  |  |         server.close(); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     next(); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | run(); |