Permalink
Browse files

Finish implementation

  • Loading branch information...
1 parent 07d7504 commit 2c06db22d51102fc753099800d4abd285f6bf301 @brianloveswords brianloveswords committed Mar 27, 2013
Showing with 32 additions and 16 deletions.
  1. +7 −10 controllers/badge.js
  2. +1 −1 lib/award.js
  3. +4 −0 models/badge-image.js
  4. +7 −1 test/award.test.js
  5. +5 −3 test/backpack-controller.test.js
  6. +8 −1 test/badge-image-model.test.js
View
@@ -1,6 +1,6 @@
+const async = require('async');
const Badge = require('../models/badge');
const BadgeImage = require('../models/badge-image');
-
const logger = require('../lib/logging').logger;
const utils = require('../lib/utils');
@@ -57,7 +57,7 @@ exports.image = function image(req, res, next) {
const image = req.badgeImage;
if (!image) return res.send(404);
res.type('image/png');
- return res.send(200, Buffer(image.get('image_data'), 'base64'));
+ return res.send(200, image.toBuffer());
}
exports.share = function share(req, res, next) {
@@ -96,16 +96,13 @@ exports.show = function show(req, res, next) {
exports.destroy = function destroy(req, res) {
const badge = req.badge;
const user = req.user;
- function failNow() {
- return res.send(respond('forbidden', "Cannot delete a badge you don't own"), 403);
- }
if (!badge)
- return res.send(respond('missing', "Cannot delete a badge that doesn't exist"), 404);
+ return res.send(404, respond('missing', "Cannot delete a badge that doesn't exist"));
if (!user || badge.get('user_id') !== user.get('id'))
- return failNow();
+ return res.send(403, respond('forbidden', "Cannot delete a badge you don't own"));
async.series([
function destroyBadgeImage(callback) {
- const condition = { badge_hash: badge.get('hash') };
+ const condition = { badge_hash: badge.get('body_hash') };
BadgeImage.findAndDestroy(condition, callback)
},
function destroyBadge(callback) {
@@ -115,8 +112,8 @@ exports.destroy = function destroy(req, res) {
if (err) {
logger.warn('Failed to delete badge');
logger.warn(err);
- return res.send(respond('error', 'Could not delete badge: ' + err), 500);
+ return res.send(500, respond('error', 'Could not delete badge: ' + err));
}
- return res.send({ status: 'okay' }, 200);
+ return res.send(200, { status: 'okay' });
});
};
View
@@ -65,7 +65,7 @@ function award(opts, callback) {
badge_hash: Badge.createHash(opts.assertion),
image_data: opts.imagedata.toString('base64')
})
- async.parallel({
+ async.series({
badge: badge.save.bind(badge),
image: badgeImage.save.bind(badgeImage)
}, function (err, results) {
@@ -3,5 +3,9 @@ const Base = require('./mysql-base');
const BadgeImage = function (attr) { this.attributes = attr }
Base.apply(BadgeImage, 'badge_image');
+BadgeImage.prototype.toBuffer = function toBuffer() {
+ return Buffer(this.get('image_data'), 'base64');
+};
+
module.exports = BadgeImage;
View
@@ -6,6 +6,7 @@ const path = require('path');
const awardBadge = require('../lib/award');
const mysql = require('../lib/mysql');
const Badge = require('../models/badge');
+const BadgeImage = require('../models/badge-image');
const normalize = require('../lib/normalize-assertion');
const TEST_ASSERTION = $.makeAssertion();
@@ -98,7 +99,12 @@ $.prepareDatabase(function (done) {
}, function (err, badge) {
t.same(signature, badge.get('signature'));
t.same(normalizedAssertion.uid, badge.getFromAssertion('uid'));
- t.end();
+
+ // get the badge image
+ BadgeImage.findOne({ badge_hash: badge.get('body_hash') }, function (err, image) {
+ t.same(image.toBuffer(), PNG_DATA);
+ t.end();
+ })
});
});
@@ -4,6 +4,7 @@ const test = require('tap').test;
const backpack = require('../controllers/backpack');
const conmock = require('./conmock');
const Badge = require('../models/badge');
+const BadgeImage = require('../models/badge-image');
const User = require('../models/user');
const ASSERTION_NOT_FOUND = __dirname + '/data/404.png';
@@ -73,9 +74,10 @@ $.prepareDatabase({
Badge.findAll(function (err, badges) {
const expectedImageData = fs.readFileSync(VALID_BAKED_IMAGE).toString('base64');
t.same(badges.length, 1);
- if (badges.length)
- t.same(badges[0].get('image_data'), expectedImageData);
- t.end();
+ BadgeImage.findOne({badge_hash: badges[0].get('body_hash')}, function (err, image) {
+ t.same(image.get('image_data'), expectedImageData);
+ t.end();
+ });
})
});
});
@@ -3,6 +3,7 @@ const User = require('../models/user');
const Badge = require('../models/badge');
const BadgeImage = require('../models/badge-image');
const test = require('tap').test;
+const images = require('./test-images');
$.prepareDatabase({
'1-user': new User({
@@ -15,7 +16,7 @@ $.prepareDatabase({
}),
'3-existing-badge-image': new BadgeImage({
badge_hash: Badge.createHash($.makeAssertion()),
- image_data: 'image1'
+ image_data: images.unbaked.toString('base64')
}),
}, function (fixtures) {
@@ -29,5 +30,11 @@ $.prepareDatabase({
});
});
+ test('BadgeImage#toBuffer', function (t) {
+ const badgeimage = fixtures['3-existing-badge-image'];
+ t.same(badgeimage.toBuffer(), images.unbaked);
+ t.end();
+ });
+
$.finish(test);
})

0 comments on commit 2c06db2

Please sign in to comment.