Skip to content
This repository
Browse code

Finish implementation

  • Loading branch information...
commit 2c06db22d51102fc753099800d4abd285f6bf301 1 parent 07d7504
Brian J Brennan authored March 27, 2013
17  controllers/badge.js
... ...
@@ -1,6 +1,6 @@
  1
+const async = require('async');
1 2
 const Badge = require('../models/badge');
2 3
 const BadgeImage = require('../models/badge-image');
3  
-
4 4
 const logger = require('../lib/logging').logger;
5 5
 const utils = require('../lib/utils');
6 6
 
@@ -57,7 +57,7 @@ exports.image = function image(req, res, next) {
57 57
   const image = req.badgeImage;
58 58
   if (!image) return res.send(404);
59 59
   res.type('image/png');
60  
-  return res.send(200, Buffer(image.get('image_data'), 'base64'));
  60
+  return res.send(200, image.toBuffer());
61 61
 }
62 62
 
63 63
 exports.share = function share(req, res, next) {
@@ -96,16 +96,13 @@ exports.show = function show(req, res, next) {
96 96
 exports.destroy = function destroy(req, res) {
97 97
   const badge = req.badge;
98 98
   const user = req.user;
99  
-  function failNow() {
100  
-    return res.send(respond('forbidden', "Cannot delete a badge you don't own"), 403);
101  
-  }
102 99
   if (!badge)
103  
-    return res.send(respond('missing', "Cannot delete a badge that doesn't exist"), 404);
  100
+    return res.send(404, respond('missing', "Cannot delete a badge that doesn't exist"));
104 101
   if (!user || badge.get('user_id') !== user.get('id'))
105  
-    return failNow();
  102
+    return res.send(403, respond('forbidden', "Cannot delete a badge you don't own"));
106 103
   async.series([
107 104
     function destroyBadgeImage(callback) {
108  
-      const condition = { badge_hash: badge.get('hash') };
  105
+      const condition = { badge_hash: badge.get('body_hash') };
109 106
       BadgeImage.findAndDestroy(condition, callback)
110 107
     },
111 108
     function destroyBadge(callback) {
@@ -115,8 +112,8 @@ exports.destroy = function destroy(req, res) {
115 112
     if (err) {
116 113
       logger.warn('Failed to delete badge');
117 114
       logger.warn(err);
118  
-      return res.send(respond('error', 'Could not delete badge: ' + err), 500);
  115
+      return res.send(500, respond('error', 'Could not delete badge: ' + err));
119 116
     }
120  
-    return res.send({ status: 'okay' }, 200);
  117
+    return res.send(200, { status: 'okay' });
121 118
   });
122 119
 };
2  lib/award.js
@@ -65,7 +65,7 @@ function award(opts, callback) {
65 65
       badge_hash: Badge.createHash(opts.assertion),
66 66
       image_data: opts.imagedata.toString('base64')
67 67
     })
68  
-    async.parallel({
  68
+    async.series({
69 69
       badge: badge.save.bind(badge),
70 70
       image: badgeImage.save.bind(badgeImage)
71 71
     }, function (err, results) {
4  models/badge-image.js
@@ -3,5 +3,9 @@ const Base = require('./mysql-base');
3 3
 const BadgeImage = function (attr) { this.attributes = attr }
4 4
 Base.apply(BadgeImage, 'badge_image');
5 5
 
  6
+BadgeImage.prototype.toBuffer = function toBuffer() {
  7
+  return Buffer(this.get('image_data'), 'base64');
  8
+};
  9
+
6 10
 module.exports = BadgeImage;
7 11
 
8  test/award.test.js
@@ -6,6 +6,7 @@ const path = require('path');
6 6
 const awardBadge = require('../lib/award');
7 7
 const mysql = require('../lib/mysql');
8 8
 const Badge = require('../models/badge');
  9
+const BadgeImage = require('../models/badge-image');
9 10
 const normalize = require('../lib/normalize-assertion');
10 11
 
11 12
 const TEST_ASSERTION = $.makeAssertion();
@@ -98,7 +99,12 @@ $.prepareDatabase(function (done) {
98 99
     }, function (err, badge) {
99 100
       t.same(signature, badge.get('signature'));
100 101
       t.same(normalizedAssertion.uid, badge.getFromAssertion('uid'));
101  
-      t.end();
  102
+
  103
+      // get the badge image
  104
+      BadgeImage.findOne({ badge_hash: badge.get('body_hash') }, function (err, image) {
  105
+        t.same(image.toBuffer(), PNG_DATA);
  106
+        t.end();
  107
+      })
102 108
     });
103 109
   });
104 110
 
8  test/backpack-controller.test.js
@@ -4,6 +4,7 @@ const test = require('tap').test;
4 4
 const backpack = require('../controllers/backpack');
5 5
 const conmock = require('./conmock');
6 6
 const Badge = require('../models/badge');
  7
+const BadgeImage = require('../models/badge-image');
7 8
 const User = require('../models/user');
8 9
 
9 10
 const ASSERTION_NOT_FOUND = __dirname + '/data/404.png';
@@ -73,9 +74,10 @@ $.prepareDatabase({
73 74
       Badge.findAll(function (err, badges) {
74 75
         const expectedImageData = fs.readFileSync(VALID_BAKED_IMAGE).toString('base64');
75 76
         t.same(badges.length, 1);
76  
-        if (badges.length)
77  
-          t.same(badges[0].get('image_data'), expectedImageData);
78  
-        t.end();
  77
+        BadgeImage.findOne({badge_hash: badges[0].get('body_hash')}, function (err, image) {
  78
+          t.same(image.get('image_data'), expectedImageData);
  79
+          t.end();
  80
+        });
79 81
       })
80 82
     });
81 83
   });
9  test/badge-image-model.test.js
@@ -3,6 +3,7 @@ const User = require('../models/user');
3 3
 const Badge = require('../models/badge');
4 4
 const BadgeImage = require('../models/badge-image');
5 5
 const test = require('tap').test;
  6
+const images = require('./test-images');
6 7
 
7 8
 $.prepareDatabase({
8 9
   '1-user': new User({
@@ -15,7 +16,7 @@ $.prepareDatabase({
15 16
   }),
16 17
   '3-existing-badge-image': new BadgeImage({
17 18
     badge_hash: Badge.createHash($.makeAssertion()),
18  
-    image_data: 'image1'
  19
+    image_data: images.unbaked.toString('base64')
19 20
   }),
20 21
 }, function (fixtures) {
21 22
 
@@ -29,5 +30,11 @@ $.prepareDatabase({
29 30
     });
30 31
   });
31 32
 
  33
+  test('BadgeImage#toBuffer', function (t) {
  34
+    const badgeimage = fixtures['3-existing-badge-image'];
  35
+    t.same(badgeimage.toBuffer(), images.unbaked);
  36
+    t.end();
  37
+  });
  38
+
32 39
   $.finish(test);
33 40
 })

0 notes on commit 2c06db2

Please sign in to comment.
Something went wrong with that request. Please try again.