Skip to content

Commit ec4be6c

Browse files
committed
Fixes tests
* skip tests that have no test middleware implementation * update middleware to correctly implement test expectations
1 parent f79c841 commit ec4be6c

File tree

4 files changed

+165
-89
lines changed

4 files changed

+165
-89
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@
5959
"yargs": "^4.8.1"
6060
},
6161
"devDependencies": {
62+
"chai-things": "^0.2.0",
63+
"cheerio": "^1.0.0-rc.2",
6264
"webpack-dev-middleware": "^1.6.1",
6365
"webpack-hot-middleware": "^2.12.2"
6466
}

server.js

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var fs = require("fs")
1616
// express library for defining routes
1717
var express = require("express");
1818

19-
// try and load a .env file.
19+
// try and load a .env file.
2020
// This will put out a warning if one does not exist but that's fine
2121
// if it doesn't exist, you should be setting the environment variables manually.
2222
require("dotenv").config();
@@ -62,7 +62,7 @@ if(process.env.NODE_ENV !== 'production') {
6262
var webpackHotMiddleware = require('webpack-hot-middleware');
6363
var webpack = require('webpack');
6464
var compiler = webpack(config);
65-
65+
6666
app.use(webpackDevMiddleware(compiler, { noInfo: true, publicPath: config.output.publicPath }));
6767
app.use(webpackHotMiddleware(compiler));
6868
}
@@ -73,7 +73,7 @@ var cookieParser = require("cookie-parser")
7373
var csrf = require("csurf");
7474
var csrfProtection = csrf(
7575
{cookie:{
76-
secure: app_config.http_override ? false : true,
76+
secure: app_config.http_override ? false : true,
7777
httpOnly: true
7878
}
7979
}
@@ -122,7 +122,7 @@ winston.add(winston.transports.Console, {
122122

123123
// define our logger
124124
// the expressWinston.logger only logs the HTTP(s) requests and response
125-
// the expressWinston.errorLogger is at the bottom of the file. It has be
125+
// the expressWinston.errorLogger is at the bottom of the file. It has be
126126
var expressTransports = [
127127
new winston.transports.Console({
128128
colorize: true,
@@ -194,9 +194,9 @@ function sendResponse(package, res) {
194194
res.setHeader('Content-Type', 'application/json');
195195
if (package.error_code) {
196196
var error_package = {
197-
"error_code":500,
198-
"error_description":"wepay call died. Check server logs for more details.",
199-
"error_message":package.error_description,
197+
"error_code":500,
198+
"error_description":"wepay call died. Check server logs for more details.",
199+
"error_message":package.error_description,
200200
"original_error":package}
201201
winston.warn("Sending error!\t", error_package);
202202
return res.status(500).send(JSON.stringify(error_package));
@@ -216,7 +216,7 @@ function sendResponse(package, res) {
216216
* @param package - the package of data we want to send along with request. This can be an empty object depending on the endpoint
217217
*/
218218
function getWePayData(res, wepay_endpoint, access_token, package) {
219-
219+
220220
var wepay_settings = {}
221221

222222
if (access_token) {
@@ -263,13 +263,13 @@ function getDataFromMiddleware(resource, data, callback) {
263263
winston.info("Requesting data from middleware: ", uri, data);
264264
return request.post(
265265
{
266-
url: uri,
266+
url: uri,
267267
json: data,
268268
headers: {
269269
"Authorization":app_config.middleware_secret
270270
}
271271

272-
},
272+
},
273273
callback
274274
);
275275
}
@@ -317,20 +317,20 @@ function parseMiddlewareResponse(req, res, error, response, body, wepay_endpoint
317317
app.post("/user", csrfProtection, function(req, res) {
318318
winston.info('Incoming user request: ', req.body);
319319
var package = {};
320-
320+
321321
// get the email from the search
322322
var email = req.body.email;
323323
var account_id = req.body.account_id;
324324

325325
// get the necessary data from our middleware function and then make the corresponding request to WePay
326326
getDataFromMiddleware(
327-
"user",
327+
"user",
328328
{
329329
"account_owner_email":email,
330330
"account_id": account_id
331-
},
331+
},
332332
function(error, response, body) {
333-
return parseMiddlewareResponse(req, res, error, response, body, "/user", {});
333+
return parseMiddlewareResponse(req, res, error, response, body, "/user", {});
334334
}
335335
);
336336
})
@@ -355,8 +355,8 @@ app.post('/account', csrfProtection, function(req, res){
355355
package['account_id'] = req.body.account_id;
356356
wepay_endpoint = "/account";
357357
return getDataFromMiddleware(
358-
"user",
359-
{"account_id":req.body.account_id},
358+
"user",
359+
{"account_id":req.body.account_id},
360360
function(error, response, body){
361361
parseMiddlewareResponse(req, res, error, response, body, wepay_endpoint, package)
362362
});
@@ -374,7 +374,7 @@ app.post('/account', csrfProtection, function(req, res){
374374
* This endpoint has two seperate actions depending on the parameters passed
375375
* If no checkout_id is given, then this will get the 50 most recent checkouts for the given account_id
376376
* If a checkout_id is given, then this will fetch information for that checkout specifically.
377-
* Passing the checkout_id is useful for updating a checkout's info after performing an action with the dashboard.
377+
* Passing the checkout_id is useful for updating a checkout's info after performing an action with the dashboard.
378378
*/
379379
app.post("/checkout", csrfProtection, function(req, res) {
380380
// prep the package and wepay_endpoint we want to hit
@@ -395,7 +395,7 @@ app.post("/checkout", csrfProtection, function(req, res) {
395395

396396
return getDataFromMiddleware("user", {"account_id":req.body.account_id}, function(error, response, body){
397397
parseMiddlewareResponse(req, res, error, response, body, wepay_endpoint, package);
398-
});
398+
});
399399
})
400400

401401
/**
@@ -412,24 +412,24 @@ app.post("/withdrawal", csrfProtection, function(req, res){
412412
winston.info("Received request for withdrawals");
413413
var package = {"account_id":req.body.account_id};
414414
return getDataFromMiddleware(
415-
"user",
416-
{"account_id":req.body.account_id},
415+
"user",
416+
{"account_id":req.body.account_id},
417417
function(error, response, body) {
418418
parseMiddlewareResponse(req, res, error, response, body, "/withdrawal/find", package);
419419
});
420420
})
421421

422422
/**
423423
* Perform a refund for a given checkout_id.
424-
*
424+
*
425425
* This endpoint expects the following fields in the body of the request:
426426
* @param checkout_id - the checkout_id
427427
* @param refund_reason - the reason that the checkout is being refunded
428428
* @param amount - (optional) initates a partial refund for the specified amount
429429
* @param app_fee - (optional) initiates a partial refund for the specified app_fee amount
430430
* The amount field should be used to perform a partial refund.
431431
* When doing a partial refund, you can also pass a app_fee that specifies how much of the app_fee you want to refund
432-
*
432+
*
433433
* If no amount is passed, this will do a full refund
434434
*/
435435
app.post("/refund", csrfProtection, function(req, res) {
@@ -445,8 +445,8 @@ app.post("/refund", csrfProtection, function(req, res) {
445445
}
446446

447447
return getDataFromMiddleware(
448-
"user",
449-
{"account_id":req.body.account_id},
448+
"user",
449+
{"account_id":req.body.account_id},
450450
function(error, response, body){
451451
return parseMiddlewareResponse(req, res, error, response, body, "/checkout/refund", package)
452452
}
@@ -472,14 +472,14 @@ app.post("/reserve", csrfProtection, function(req, res) {
472472
/**
473473
* Given a payer's unique identifying information (such as their email), get a list of all of their checkouts from the middleware
474474
*/
475-
app.post("/payer", csrfProtection, function(req, res) {
476-
winston.info("Received request for payer");
475+
app.post("/payer", csrfProtection, function(req, res) {
476+
winston.info("Received request for payer");
477477
// get the email from the search
478478
var email = req.body.email;
479479
// get the necessary data from our middleware function and then make the corresponding request to WePay
480480
return getDataFromMiddleware(
481-
"payer",
482-
{"payer_email":email, "num_elements":50},
481+
"payer",
482+
{"payer_email":email, "num_elements":50},
483483
function(error, response, body) {
484484
return parseMiddlewareResponse(req, res, error, response, body, null, null);
485485
}

test/middleware.py

Lines changed: 82 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def _getUserFromAccountId(self, account_id):
4646
return account['username']
4747
return None
4848

49-
def _getAccessTokenFromUser(self, data):
49+
def _getUser(self, data):
5050
"""
5151
Given either an email address or an account_id, this function will fetch an associated access_token
5252
@@ -55,9 +55,9 @@ def _getAccessTokenFromUser(self, data):
5555
:param data: the body of the incoming HTTP(S) request.
5656
"""
5757
print("Getting access_token from user")
58-
if "account_owner_email" in data:
59-
user = self.database['Users'].get(data['account_owner_email'])
60-
58+
if "email" in data:
59+
user = self.database['Users'].get(data['email'])
60+
6161
if not user:
6262
return self._returnError("user", {"error_message":"could not locate user with email {0}".format(data)})
6363

@@ -66,25 +66,93 @@ def _getAccessTokenFromUser(self, data):
6666

6767
elif "account_id" in data:
6868
account = self.database['Accounts'].get(data['account_id'])
69-
69+
7070
if not account:
7171
return self._returnError("user", {"error_message": "could not find account with id: {0}".format(data.get('account_id'))})
72-
72+
7373
user = self.database['Users'].get(account['username'])
74-
74+
7575
if user:
7676
print("Found user: {0}".format(user))
7777
return jsonify(user)
7878
print("Could not find resource. Returning error")
7979
return self._returnError("user", {"error_message":"could not locate resource with {0}".format(data)})
80+
def _getAccount(self, data):
81+
"""
82+
Given either an email address or an account_id, this function will fetch an associated access_token
83+
84+
This function will either return a JSON response with the associated access_token or an error
85+
86+
:param data: the body of the incoming HTTP(S) request.
87+
"""
88+
print("Getting account")
89+
if "account_id" in data:
90+
account = self.database['Accounts'].get(data['account_id'])
91+
92+
if not account:
93+
return self._returnError("account", {"error_message": "could not find account with id: {0}".format(data.get('account_id'))})
94+
95+
print("Found account: {0}".format(account))
96+
return jsonify(account)
97+
elif "email" in data:
98+
user = self.database['Users'].get(data['email'])
99+
100+
if not user:
101+
return self._returnError("user", {"error_message":"could not locate user with email {0}".format(data)})
102+
103+
accounts = []
104+
user_id = user['user_id']
105+
for key, value in self.database['Accounts'].items():
106+
account_user_id = value['user_id']
107+
if account_user_id == user_id:
108+
accounts.append(value)
109+
110+
print("Found user: {0}".format(user))
111+
return json.dumps(accounts)
112+
print("Could not find resource. Returning error")
113+
return self._returnError("user", {"error_message":"could not locate resource with {0}".format(data)})
114+
def _getCheckout(self, data):
115+
"""
116+
Given either an checkout_id or an account_id, this function will fetch an associated checkout
117+
118+
This function will either return a JSON response with the associated checkout or an error
119+
120+
:param data: the body of the incoming HTTP(S) request.
121+
"""
122+
print("Getting checkout")
123+
if "checkout_id" in data:
124+
checkout = self.database['Checkouts'].get(data['checkout_id'])
125+
126+
if not checkout:
127+
return self._returnError("checkout", {"error_message": "could not find checkout with id: {0}".format(data.get('checkout_id'))})
128+
129+
print("Found checkout: {0}".format(checkout))
130+
return jsonify(checkout)
131+
elif "account_id" in data:
132+
account = self.database['Accounts'].get(data['account_id'])
133+
134+
if not account:
135+
return self._returnError("user", {"error_message":"could not locate account with account_id {0}".format(data)})
136+
137+
checkouts = []
138+
account_id = account['account_id']
139+
for key, value in self.database['Checkouts'].items():
140+
checkout_account_id = value['account_id']
141+
if checkout_account_id == account_id:
142+
checkouts.append(value)
143+
144+
print("Found account: {0}".format(account))
145+
return json.dumps(checkouts)
146+
print("Could not find resource. Returning error")
147+
return self._returnError("user", {"error_message":"could not locate resource with {0}".format(data)})
80148
def _getPayerCheckouts(self, data):
81149
"""
82150
given a payer email get a list of all of their checkouts
83151
84152
TODO: make this timebased. Payer must provide their email and the date that the charge occured. We can search for +/- a day around the date they provide
85153
"""
86-
print("Checking checkouts with payer: {0}".format(data['payer_email']))
87-
checkouts = [v for k,v in self.database['Checkouts'].items() if v['payer_email'] == data['payer_email']]
154+
print("Checking checkouts with payer: {0}".format(data['email']))
155+
checkouts = [v for k,v in self.database['Checkouts'].items() if v['payer_email'] == data['email']]
88156

89157
print("Found checkouts: {0}".format(checkouts))
90158

@@ -135,7 +203,11 @@ def KvasirMiddleware(resource):
135203
data = request.json
136204

137205
if resource == "user":
138-
return kvasir_middleware._getAccessTokenFromUser(data)
206+
return kvasir_middleware._getUser(data)
207+
elif resource == "account":
208+
return kvasir_middleware._getAccount(data)
209+
elif resource == "checkout":
210+
return kvasir_middleware._getCheckout(data)
139211
elif resource == "payer":
140212
return kvasir_middleware._getPayerCheckouts(data)
141213
else:

0 commit comments

Comments
 (0)