Skip to content

Commit 364f328

Browse files
authored
Merge pull request #1 from k1LoW/add-test
Add test
2 parents 98d868f + e4f1062 commit 364f328

File tree

8 files changed

+89
-13
lines changed

8 files changed

+89
-13
lines changed

.editorconfig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
root = true
2+
3+
[*]
4+
end_of_line = lf
5+
insert_final_newline = true
6+
charset = utf-8
7+
trim_trailing_whitespace = true
8+
9+
[*.{js}]
10+
indent_style = space
11+
indent_size = 4
12+
13+
[*.{json,yml}]
14+
indent_style = space
15+
indent_size = 2

.eslintrc.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module.exports = {
2+
"extends": "airbnb-base",
3+
"plugins": [
4+
"import"
5+
],
6+
"rules": {
7+
"arrow-body-style": "off",
8+
"comma-dangle": ["error", "never"],
9+
"import/no-dynamic-require": "off",
10+
"indent": ["error", 4],
11+
"no-prototype-builtins": "off",
12+
"prefer-destructuring": "off"
13+
}
14+
};

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
node_modules
1+
node_modules
2+
.nyc_output

.travis.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
language: node_js
2+
node_js:
3+
- 6
4+
cache:
5+
directories:
6+
- "node_modules"
7+
script:
8+
- npm run test

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Serverless boilerplate for Static website hosting with Basic authentication
1+
# Serverless boilerplate for Static website hosting with Basic authentication [![Build Status](https://travis-ci.org/k1LoW/serverless-static-hosting-with-basic-auth.svg?branch=master)](https://travis-ci.org/k1LoW/serverless-static-hosting-with-basic-auth)
22

33
## Architecture
44

@@ -19,7 +19,7 @@ $ npm install
1919
$ AWS_PROFILE=XxxxxXXX WEBSITE_S3_BUCKET_NAME=sls-static-basic npm run deploy
2020
```
2121

22-
### Synchronize src/* and Website
22+
### Synchronize src/* -> Website
2323

2424
```
2525
$ AWS_PROFILE=XxxxxXXX WEBSITE_S3_BUCKET_NAME=sls-static-basic npm run sync

handler.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
'use strict';
2-
31
const BASIC_AUTH_USERS = {
4-
'user': 'pass'
2+
user: 'pass'
53
};
64

75
module.exports.basicAuth = (event, context, callback) => {
@@ -11,22 +9,26 @@ module.exports.basicAuth = (event, context, callback) => {
119

1210
if (authorization) {
1311
const encoded = authorization[0].value.split(' ')[1];
14-
const userAndPassword = new Buffer(encoded, 'base64').toString();
15-
for (let user in BASIC_AUTH_USERS) {
12+
const userAndPassword = Buffer.from(encoded, 'base64').toString();
13+
const result = Object.keys(BASIC_AUTH_USERS).filter((user) => {
1614
const password = BASIC_AUTH_USERS[user];
1715
if (`${user}:${password}` === userAndPassword) {
18-
callback(null, request);
19-
return;
16+
return true;
2017
}
18+
return false;
19+
});
20+
if (result.length > 0) {
21+
callback(null, request);
22+
return;
2123
}
2224
}
2325

2426
const response = {
2527
status: '401',
2628
statusDescription: 'Authorization Required',
2729
headers: {
28-
'www-authenticate': [{key: 'WWW-Authenticate', value: 'Basic'}],
29-
'content-type': [{key: 'Content-Type', value: 'text/plain; charset=utf-8'}]
30+
'www-authenticate': [{ key: 'WWW-Authenticate', value: 'Basic' }],
31+
'content-type': [{ key: 'Content-Type', value: 'text/plain; charset=utf-8' }]
3032
},
3133
body: '401 Authorization Required'
3234
};

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,20 @@
77
"deploy": "sls deploy -v",
88
"remove": "sls remove",
99
"sync": "sls sync",
10-
"test": "echo \"Error: no test specified\" && exit 1"
10+
"test": "nyc ava -v",
11+
"lint": "eslint ."
1112
},
1213
"keywords": [
1314
"serverless"
1415
],
1516
"author": "k1LoW <k1lowxb@gmail.com> (https://github.com/k1LoW)",
1617
"license": "MIT",
1718
"devDependencies": {
19+
"ava": "^0.24.0",
20+
"eslint-config-airbnb-base": "^12.1.0",
21+
"nyc": "^11.4.1",
1822
"octopublish": "^0.5.0",
23+
"path": "^0.12.7",
1924
"serverless": "^1.25.0",
2025
"serverless-plugin-cloudfront-lambda-edge": "^1.0.0",
2126
"serverless-s3-sync": "^1.3.0"

test/basicAuth.test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const test = require('ava');
2+
const path = require('path');
3+
4+
const { basicAuth } = require(path.join(__dirname, '..', 'handler.js'));
5+
6+
test.cb('handler.basicAuth check user/pass', (t) => {
7+
const event = {
8+
Records: [
9+
{
10+
cf: {
11+
request: {
12+
headers: {
13+
authorization: [{ key: 'Authorization', value: 'Basic dXNlcjpwYXNz' }]
14+
}
15+
}
16+
}
17+
}
18+
]
19+
};
20+
const context = {};
21+
const callback = (error, response) => {
22+
t.deepEqual(response, {
23+
headers: {
24+
authorization: [{ key: 'Authorization', value: 'Basic dXNlcjpwYXNz' }]
25+
}
26+
});
27+
t.end();
28+
};
29+
30+
basicAuth(event, context, callback);
31+
});

0 commit comments

Comments
 (0)