var assert = require('assert')
var PathTable = require('../')

describe('rules', function () {
  describe('areAnyAllow', function () {
    var tb = new PathTable()
    before(function () {
      var entries = [{
        action: 'deny',
        path: '/test/deny'
      }, {
        action: 'allow',
        path: '/test/:any*'
      }, {
        action: 'allow',
        method: ['get'],
        role: ['user'],
        path: '/test2/:any*'
      }, {
        action: 'deny',
        role: ['user'],
        path: '/test2/:any*'
      }, {
        action: 'allow',
        method: ['get'],
        role: ['user', 'admin'],
        path: '/test3/:any*'
      }, {
        action: 'allow',
        method: ['get'],
        role: ['user', 'admin'],
        path: '/test3/:any*'
      }, {
        action: 'allow',
        method: ['post'],
        role: ['admin'],
        path: '/test3/post'
      }]

      entries.forEach(function (entry) {
        tb.addEntry(entry)
      })
    })

    it('test', function () {
      var allow = tb.areAnyAllow('/test', 'GET', undefined)
      assert.ok(allow, 'should allow')

      allow = tb.areAnyAllow('/test/hello', 'GET', undefined)
      assert.ok(allow, 'should allow')

      allow = tb.areAnyAllow('/test/deny', 'GET', undefined)
      assert.ok(!allow, 'should not allow')
    })

    it('test2', function () {
      var allow = tb.areAnyAllow('/test2', 'GET', undefined)
      assert.ok(!allow, 'should not allow')

      allow = tb.areAnyAllow('/test2/hello', 'GET', ['guest'])
      assert.ok(!allow, 'should not allow')

      allow = tb.areAnyAllow('/test2/hello', 'GET', ['user'])
      assert.ok(allow, 'should allow')

      allow = tb.areAnyAllow('/test2/hello', 'PUT', ['user'])
      assert.ok(!allow, 'should not allow')
    })

    it('test3', function () {
      var allow = tb.areAnyAllow('/test3', 'GET', undefined)
      assert.ok(!allow, 'should not allow')

      allow = tb.areAnyAllow('/test3', 'GET', ['user'])
      assert.ok(allow, 'should allow')

      allow = tb.areAnyAllow('/test3/hello', 'GET', ['admin'])
      assert.ok(allow, 'should allow')

      allow = tb.areAnyAllow('/test3/post', 'POST', ['user'])
      assert.ok(!allow, 'should not allow')

      allow = tb.areAnyAllow('/test3/post', 'POST', ['admin'])
      assert.ok(allow, 'should allow')

      allow = tb.areAnyAllow('/test3/post', 'POST', ['user', 'admin'])
      assert.ok(allow, 'should allow')

      allow = tb.areAnyAllow('/test3/post', 'POST', ['admin', 'user'])
      assert.ok(allow, 'should allow')

      allow = tb.areAnyAllow('/test3/hello', 'POST', ['admin'])
      assert.ok(!allow, 'should not allow')
    })
  })
})

@@ -883,16 +883,470 @@
 3/post'%0A
+      %7D, %7B%0A        action: 'allow',%0A        method: %5B'get'%5D,%0A        role: %5B'admin'%5D,%0A        path: '/test4/get'%0A      %7D, %7B%0A        action: 'deny',%0A        method: %5B'get'%5D,%0A        role: %5B'admin'%5D,%0A        path: '/test4/get'%0A      %7D, %7B%0A        action: 'deny',%0A        method: %5B'post'%5D,%0A        role: %5B'admin'%5D,%0A        path: '/test4/post'%0A      %7D, %7B%0A        action: 'allow',%0A        method: %5B'post'%5D,%0A        role: %5B'admin'%5D,%0A        path: '/test4/post'%0A
       %7D%5D
@@ -1346,16 +1346,16 @@
      %7D%5D%0A
-
 %0A      e
@@ -1444,16 +1444,34 @@
 it('test
+: for default rule
 ', funct
@@ -1807,16 +1807,33 @@
 t('test2
+: for single role
 ', funct
@@ -2284,16 +2284,32 @@
 t('test3
+: for multi role
 ', funct
@@ -3046,32 +3046,32 @@
 should allow')%0A%0A
-
       allow = tb
@@ -3153,32 +3153,833 @@
 ould not allow')
+%0A%0A      allow = tb.areAnyAllow('/test3/hello', 'POST', %5B%5D)%0A      assert.ok(!allow, 'should not allow')%0A    %7D)%0A%0A    it('test4: for duplicate path rule', function () %7B%0A      var allow = tb.areAnyAllow('/test4/get', 'GET', %5B'admin'%5D)%0A      assert.ok(allow, 'should allow')%0A%0A      allow = tb.areAnyAllow('/test4/post', 'POSt', %5B'admin'%5D)%0A      assert.ok(!allow, 'should not allow')%0A%0A      allow = tb.areAnyAllow('/test4/get', 'GET', %5B'user'%5D)%0A      assert.ok(!allow, 'should not allow')%0A%0A      allow = tb.areAnyAllow('/test4/post', 'POST', %5B'user'%5D)%0A      assert.ok(!allow, 'should not allow')%0A%0A      allow = tb.areAnyAllow('/test4/get', 'POST', undefined)%0A      assert.ok(!allow, 'should not allow')%0A%0A      allow = tb.areAnyAllow('/test4/put', 'PUT', %5B'user'%5D)%0A      assert.ok(!allow, 'should not allow')
 %0A    %7D)%0A  %7D)%0A%7D)%0A

add test case