-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
63dffae
commit ff824b8
Showing
15 changed files
with
201 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module.exports.reqBasic = function *(next){ | ||
try { | ||
yield next; | ||
} | ||
catch (err) { | ||
if (401 == err.status) { | ||
this.status = 401; | ||
this.set('WWW-Authenticate', 'Basic'); | ||
this.body = 'Nope... you need to authenticate first. With a proper user!'; | ||
} | ||
else { | ||
throw err; | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
var adminUser = { | ||
name : process.env.BASIC_USER || 'ypkbk', | ||
pass : process.env.BASIC_PASS || 'ypkbk' | ||
}; | ||
|
||
var config = { | ||
local: { | ||
mode: 'local', | ||
port: 3000, | ||
adminUser : adminUser | ||
}, | ||
testing: { | ||
mode: 'testing', | ||
port: 4000, | ||
adminUser : adminUser | ||
}, | ||
prod: { | ||
mode: 'prod', | ||
port: process.env.PORT || 5000, | ||
adminUser : adminUser | ||
} | ||
}; | ||
|
||
module.exports = function (mode) { | ||
return config[mode || process.argv[2] || 'local'] || config.local; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
var koa = require('koa'); | ||
var app = module.exports = koa(); | ||
var mount = require('koa-mount'); | ||
var config = require("./config.js")(); | ||
|
||
// The root application | ||
var rootApp = koa(); | ||
rootApp.use(function *(next){ | ||
yield next; | ||
this.body = 'Find the APIs under /user, /order and /address respectively'; | ||
}); | ||
|
||
// APIS | ||
var userApi = require('UserAPI'); | ||
var addressApi = require('AddressAPI'); | ||
var orderApi = require('OrderAPI'); | ||
|
||
// Mounting | ||
app.use(mount('/', rootApp)); | ||
app.use(mount('/users', userApi)); | ||
app.use(mount('/address', addressApi)); | ||
|
||
|
||
// middleware configuration | ||
var auth = require('koa-basic-auth'); | ||
var userAuth = require('./authentication.js'); | ||
app.use(userAuth.reqBasic); | ||
app.use(mount('/orders', auth(config.adminUser))); | ||
app.use(mount('/orders', orderApi)); | ||
|
||
// listen and all of that | ||
app.listen(config.port); | ||
console.log("listening on port " + config.port); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
{ | ||
"name": "UserApiWithTest", | ||
"version": "1.0.0", | ||
"description": "The overarching application for the 3 apis", | ||
"main": "index.js", | ||
"scripts": { | ||
"start": "node --harmony index.js prod", | ||
"startLocal": "nodemon --harmony index.js", | ||
"test": "./node_modules/mocha/bin/mocha --harmony-generators -u bdd -R spec" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/marcusoftnet/UserApiWithTest.git" | ||
}, | ||
"keywords": [ | ||
"koa", | ||
"api", | ||
"rest", | ||
"supertest" | ||
], | ||
"author": "Marcus Hammarberg @marcusoftnet", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/marcusoftnet/UserApiWithTest/issues" | ||
}, | ||
"homepage": "https://github.com/marcusoftnet/UserApiWithTest", | ||
"dependencies": { | ||
"AddressAPI": "file:apis/address", | ||
"OrderApi": "file:apis/order", | ||
"UserAPI": "file:apis/user", | ||
"koa": "^0.20.0", | ||
"koa-basic-auth": "^1.1.2", | ||
"koa-mount": "^1.3.0" | ||
}, | ||
"devDependencies": { | ||
"mocha": "^2.2.4", | ||
"supertest": "^0.15.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/// <reference path="../typings/mocha/mocha.d.ts"/> | ||
var supertest = require('supertest'); | ||
var app = require("../"); | ||
var config = require('../config')(); | ||
var request = supertest.agent(app.listen()); | ||
|
||
describe('Our application', function () { | ||
it('has a simple root application', function (done) { | ||
request | ||
.get('/') | ||
.expect(200) | ||
.expect(/Find the APIs under/) | ||
.end(done); | ||
}); | ||
it('an user api to which we can post', function (done) { | ||
request | ||
.post('/users') | ||
.send({ name: 'Marcus', city : 'Bandung, Indonesia'}) | ||
.expect(201) | ||
.expect('location', /^\/users\/[0-9a-fA-F]{24}$/) | ||
.end(done); | ||
}); | ||
it('and an address api to which we can post', function (done) { | ||
var test_address = { | ||
userId: 987654321, | ||
street : 'Jalan Jawa No 20', | ||
city : 'Bandung', | ||
country: 'Indonesia' | ||
}; | ||
|
||
request | ||
.post('/address') | ||
.send(test_address) | ||
.expect(201) | ||
.expect('location', /^\/address\/[0-9a-fA-F]{24}$/) | ||
.end(done); | ||
}); | ||
it('and an order api, but that requires login', function (done) { | ||
var test_order = { | ||
orderId: '123456789', | ||
ordered : new Date("2015-01-01"), | ||
userId : "987654321" | ||
}; | ||
|
||
request | ||
.post('/orders') | ||
.auth(config.adminUser.name, config.adminUser.pass) | ||
.send(test_order) | ||
.expect('location', /^\/orders\/[0-9]{9}$/) | ||
.expect(201) | ||
.end(done); | ||
}); | ||
it('exactly - the order API require login. Not logging in will give you access', function (done) { | ||
var test_order = { }; | ||
|
||
request | ||
.post('/orders') | ||
.send(test_order) | ||
.expect(401) | ||
.end(done); | ||
}); | ||
}); |