Skip to content

Commit

Permalink
Fix Issue #14
Browse files Browse the repository at this point in the history
- Fix bug that caused all default collection endpoints to be generated
  when excludedEndpoints were defined and no options were configured for
  any endpoints.
- Add a few tests to check that endpoints were being excluded in this
  context
  • Loading branch information
kahmali committed Mar 4, 2015
1 parent 68963d2 commit 7a52352
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
7 changes: 4 additions & 3 deletions lib/restivus.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class @Restivus
collectionEndpoints = @_collectionEndpoints

# Flatten the options and set defaults if necessary
endpointsAwaitingConfiguration = options.endpoints
endpointsAwaitingConfiguration = options.endpoints or {}
routeOptions = options.routeOptions or {}
excludedEndpoints = options.excludedEndpoints or []
# Use collection name as default path
Expand All @@ -87,9 +87,10 @@ class @Restivus
# for operating on a single entity within the collection)
collectionRouteEndpoints = {}
entityRouteEndpoints = {}
if not endpointsAwaitingConfiguration # Generate all endpoints on this collection
# Partition the endpoints into their respective routes
if _.isEmpty(endpointsAwaitingConfiguration) and _.isEmpty(excludedEndpoints)
# Generate all endpoints on this collection
_.each methods, (method) ->
# Partition the endpoints into their respective routes
if method in methodsOnCollection
_.extend collectionRouteEndpoints, collectionEndpoints[method].call(this, collection)
else _.extend entityRouteEndpoints, collectionEndpoints[method].call(this, collection)
Expand Down
30 changes: 28 additions & 2 deletions test/api_tests.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,11 @@ if Meteor.isServer
Restivus.configure
apiPath: 'api/v1'
useAuth: true
prettyJson: true
auth: token: 'apiKey'

config = Restivus.config
test.equal config.apiPath, 'api/v1/'
test.equal config.useAuth, true
test.equal config.prettyJson, true
test.equal config.auth.token, 'apiKey'

context 'that has been configured', ->
Expand All @@ -69,6 +67,34 @@ if Meteor.isServer
test.isTrue route.endpoints.get.authRequired
test.equal route.endpoints.get.roleRequired, ['admin']

describe 'A collection route', ->
it 'should be able to exclude endpoints using just the excludedEndpoints option', (test, next) ->
Restivus.addCollection new Mongo.Collection('tests2'),
excludedEndpoints: ['get', 'getAll']
# endpoints:
# post: false


HTTP.get 'http://localhost:3000/api/v1/tests2/10', (error, result) ->
response = JSON.parse result.content
test.isTrue error
test.equal result.statusCode, 404
test.equal response.status, 'error'
test.equal response.message, 'API endpoint not found'

HTTP.get 'http://localhost:3000/api/v1/tests2/', (error, result) ->
response = JSON.parse result.content
test.isTrue error
test.equal result.statusCode, 404
test.equal response.status, 'error'
test.equal response.message, 'API endpoint not found'
next()

# describe 'A route', ->
# context 'that has been authenticated', ->
# it 'should have access to this.user and this.userId', (test) ->




#Tinytest.add 'A route - should be configurable', (test)->
Expand Down

0 comments on commit 7a52352

Please sign in to comment.