Skip to content
Browse files

Upgrading to v0.2.4

  • Loading branch information...
1 parent 4f4b09e commit 04114ae93bcbc6823d84da7d5679c55ec817f60b @mwawrusch mwawrusch committed Dec 6, 2012
View
7 README.md
@@ -9,6 +9,9 @@ This is a work in progress, and some indexes need to be defined. You have been w
## Release Notes
+### 0.2.4
+Added tests, and getByIds
+
### 0.2.3
destroy paranoid users too
@@ -26,8 +29,8 @@ added user.destroy
* Change version in package.json
git add . -A
-git commit -m "Upgrading to v0.2.3"
-git tag -a v0.2.3 -m 'version 0.2.3'
+git commit -m "Upgrading to v0.2.4"
+git tag -a v0.2.4 -m 'version 0.2.4'
git push --tags
npm publish
View
27 lib/methods/user-methods.js
@@ -87,6 +87,8 @@
this.get = __bind(this.get, this);
+ this.getByIds = __bind(this.getByIds, this);
+
this.all = __bind(this.all, this);
if (!this.models) {
@@ -119,6 +121,28 @@
};
/*
+ Retrieves users by passing a list of id's, which can be string or objectIds
+ */
+
+
+ UserMethods.prototype.getByIds = function(idList, cb) {
+ var _this = this;
+ if (idList == null) {
+ idList = [];
+ }
+ idList = _.map(idList, function(x) {
+ return new ObjectId(x.toString());
+ });
+ return this.models.User.find({}).where('_id')["in"](idList).exec(function(err, items) {
+ if (err) {
+ return cb(err);
+ }
+ items || (items = []);
+ return cb(null, new PageResult(items, items.length, 0, 99999999));
+ });
+ };
+
+ /*
Looks up a user by id.
*/
@@ -254,9 +278,6 @@
if (!item) {
return cb(new errors.NotFound("/users/" + usernameOrId));
}
- if (item.isDeleted) {
- return cb(null);
- }
return item.remove(function(err) {
if (err) {
return cb(err);
View
5 package.json
@@ -2,7 +2,7 @@
"author": "Martin Wawrusch <martin@wawrusch.com> (http://martinatsunset.com)",
"name": "mongoose-identity-store",
"description": "A bunch of mongoose schemas to implement identity management (users, accesstokens, oauth apps)",
- "version": "0.2.3",
+ "version": "0.2.4",
"main" : "lib/index.js",
"repository": {
"type": "git",
@@ -46,7 +46,8 @@
"async": "0.1.22",
"coffee-script": "1.4.0",
"should": "1.2.1",
- "mocha": "1.6.0"
+ "mocha": "1.6.0",
+ "mongoskin" : "0.4.4"
},
"bugs": { "url": "http://github.com/codedoctor/mongoose-identity-store/issues" },
"licenses": [
View
12 src/methods/user-methods.coffee
@@ -36,6 +36,18 @@ module.exports = class UserMethods
cb null, new PageResult(items || [], totalCount, offset, count)
###
+ Retrieves users by passing a list of id's, which can be string or objectIds
+ ###
+ getByIds:(idList = [], cb) =>
+ idList = _.map idList, (x) -> new ObjectId x.toString()
+
+ @models.User.find({}).where('_id').in(idList).exec (err, items) =>
+ return cb err if err
+ items or= []
+
+ cb null, new PageResult(items, items.length, 0, 99999999)
+
+ ###
Looks up a user by id.
###
get: (id, cb = ->) =>
View
4 test/basic-existenz-test.coffee
@@ -5,4 +5,6 @@ describe 'WHEN loading the module', ->
index = require '../lib/index'
it 'should exist', ->
- should.exist index
+ should.exist index
+ it 'should instantiate a store', ->
+ should.exist index.store()
View
100 test/method-users-get-by-ids-test.coffee
@@ -0,0 +1,100 @@
+should = require 'should'
+helper = require './support/helper'
+_ = require 'underscore'
+mongoose = require 'mongoose'
+ObjectId = mongoose.Types.ObjectId
+
+sampleUsers = null
+
+describe 'WHEN working with store.users.getByIds', ->
+
+ before (done) ->
+ helper.start null, done
+
+ after (done) ->
+ helper.stop done
+
+ it 'should exist', ->
+ should.exist helper.store.users
+
+ describe 'WHEN running against an empty database', ->
+ describe 'WHEN invoking getByIds', ->
+ it 'WITH empty parameters IT should return an empty list', (done) ->
+ helper.store.users.getByIds [], (err,result) ->
+ return done err if err
+ should.exist.result
+ result.should.have.property "items"
+ result.items.should.have.lengthOf 0
+ done()
+
+ describe 'WHEN running against a sample database', ->
+ it 'SETTING UP SAMPLE', (done) ->
+ sampleUsers = helper.addSampleUsers done
+
+ ###
+ it "DUMP", (done) ->
+ helper.dumpCollection('users') ->
+ done()
+ ###
+ describe 'WHEN invoking getByIds', ->
+ it 'WITH empty parameters IT should return an empty list', (done) ->
+ helper.store.users.getByIds [], (err,result) ->
+ return done err if err
+ should.exist.result
+ result.should.have.property "items"
+ result.items.should.have.lengthOf 0
+ done()
+
+ it 'WITH non existing object ids IT should return an empty list', (done) ->
+ helper.store.users.getByIds sampleUsers.nonExistingUserIds(3), (err,result) ->
+ return done err if err
+ should.exist.result
+ result.should.have.property "items"
+ result.items.should.have.lengthOf 0
+ done()
+
+ it 'WITH partially non existing object ids IT should return an only the matches', (done) ->
+ nonExisting = sampleUsers.nonExistingUserIds(3)
+ existing = sampleUsers.existingUserIds(3)
+
+ #helper.log _.union(nonExisting,existing)
+
+ helper.store.users.getByIds _.union(nonExisting,existing), (err,result) ->
+ return done err if err
+ should.exist.result
+ result.should.have.property "items"
+ result.items.should.have.lengthOf 3
+ done()
+
+ it 'WITH valid duplicates IT should only return one', (done) ->
+ existing = sampleUsers.existingUserIds(3)
+ existing.push existing[0]
+
+ helper.store.users.getByIds existing, (err,result) ->
+ return done err if err
+ should.exist.result
+ result.should.have.property "items"
+ result.items.should.have.lengthOf 3
+ done()
+
+ it 'WITH valid object ids (not strings) IT should return those', (done) ->
+ existing = sampleUsers.existingUserIds(3)
+ existing = _.map existing, (x) => new ObjectId(x)
+
+ helper.store.users.getByIds existing, (err,result) ->
+ return done err if err
+ should.exist.result
+ result.should.have.property "items"
+ result.items.should.have.lengthOf 3
+ done()
+
+ ###
+ NOTE: WE NEED TO ADD THIS, but no time today.
+ it 'WITH invalid object ids it should return an argument error', (done) ->
+ invalid = ['hallo','frank']
+
+ helper.store.users.getByIds invalid, (err,result) ->
+ should.exist err
+ # TODO: Ensure that this is the right kind of error
+ done()
+ ###
View
93 test/support/helper.coffee
@@ -0,0 +1,93 @@
+qs = require 'querystring'
+_ = require 'underscore'
+async = require 'async'
+mongoose = require 'mongoose'
+mongoskin = require 'mongoskin'
+ObjectId = mongoose.Types.ObjectId
+
+index = require '../../lib/index'
+SampleUsers = require './sample-users'
+
+class Helper
+ database : 'mongodb://localhost/codedoctor-test'
+ collections: ['oauthaccesstokens','oauthapps','oauthclients','organizations','users']
+
+ cleanDatabase : (cb) =>
+ cleanOne = (collection, cb) =>
+ @mongo.collection(collection).remove {}, cb
+
+ console.log "CLEANING Database #{@database}"
+ async.forEach @collections, cleanOne, (err) =>
+ console.log "ERROR: @{err}" if err
+ console.log "CLEANED Database #{@database}"
+ cb()
+
+ start: (obj = {}, done = ->) =>
+ _.defaults obj,
+ cleanDatabase : true
+
+ mongoose.connect @database
+ @mongo = mongoskin.db @database, safe:false
+ @store = index.store()
+
+ tasks = []
+
+ if obj.cleanDatabase
+ tasks.push (cb) =>
+ @cleanDatabase(cb)
+
+ async.series tasks, => done()
+
+
+ stop: (done = ->) =>
+ mongoose.disconnect (err) =>
+ done()
+
+ addSampleUsers: (cb) =>
+ x = new SampleUsers(@mongo)
+ x.setup(cb)
+ x
+
+ log: (obj) =>
+ console.log ""
+ console.log "+++++++++"
+ console.log JSON.stringify(obj)
+ console.log "---------"
+
+ mongoCount: (name, cb) =>
+ @mongo.collection(name).count cb
+
+
+ mongoFindOne: (name, id, cb) =>
+ id = new ObjectId id.toString()
+
+ @mongo.collection(name).findOne _id : id , (err, item) =>
+ return cb err if err
+ # Leave stuff in here for logging later.
+ cb null, item
+
+ dumpOne: (name, id, cb) =>
+ id = new ObjectId id.toString()
+ @mongo.collection(name).findOne _id : id , (err, item) =>
+ return cb err if err
+ console.log ""
+ console.log "========================== DUMPING #{name} FOR #{id} =========================="
+ console.log JSON.stringify(item)
+ console.log "--------------------------------------------------------------------------------"
+ cb null
+
+ dumpCollection: (name, cb) =>
+ console.log ""
+ @mongo.collection(name).find({}).toArray (err, items) =>
+ return cb err if err
+ console.log "========================== DUMPING #{name} =========================="
+ if items
+ _.each items, (item) =>
+ console.log JSON.stringify(item)
+ console.log "-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+"
+ else
+ console.log "NO ITEMS"
+ console.log "---------------------------------------------------------------------"
+ cb null
+
+module.exports = new Helper()
View
27 test/support/sample-users.coffee
@@ -0,0 +1,27 @@
+async = require 'async'
+_ = require 'underscore'
+mongoose = require 'mongoose'
+ObjectId = mongoose.Types.ObjectId
+
+module.exports = class SampleUsers
+
+ users: [
+ {_id: new ObjectId('50bf85a816b4f6bff4000001'), username: 'test1', primaryEmail: 'test1@test.com', password: 'test1'},
+ {_id: new ObjectId('50bf85a816b4f6bff4000002'), username: 'test2', primaryEmail: 'test2@test.com', password: 'test2'},
+ {_id: new ObjectId('50bf85a816b4f6bff4000003'), username: 'test3', primaryEmail: 'test3@test.com', password: 'test3'}
+ ]
+
+ constructor: (@mongo) ->
+
+ existingUserIds: (howMany = 3) =>
+ _.first _.map(_.pluck( @users, '_id'), (x) -> x.toString() ), howMany
+
+ nonExistingUserIds: (howMany = 3) =>
+ _.first ['500f85a816b4f6bff4000000','510f85a816b4f6bff4000000','520f85a816b4f6bff4000000'], howMany
+
+ setup: (cb) =>
+ addOneUser = (user,done) =>
+ @mongo.collection("users").save user,done
+
+ async.forEach @users, addOneUser, (err) =>
+ cb err

0 comments on commit 04114ae

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