var assert = require('assert'),
  Engine = require('../index');

var policies = [{
  "Version": "2012-10-17",
  "Statement": [{
    "Sid": "AllowUsersToDeactivateDeleteTheirOwnVirtualMFADevice",
    "Effect": "Allow",
    "Action": [
      "iam:DeactivateMFADevice",
      "iam:DeleteVirtualMFADevice"
    ],
    "Resource": [
      "arn:aws:iam:::mfa/${aws:username}",
      "arn:aws:iam:::user/${aws:username}"
    ],
    "Condition": {
      "Bool": {
        "aws:MultiFactorAuthPresent": true
      }
    }
  }]
}];

var engine = new Engine(policies, {
  conditions: _.extend(require('./conditions'), {

  }),
});

//engine.validate(policies);
//engine.validate(policies[0]);
//engine.validate(policies[1]);

describe('policies', function() {
  it('evaluate', function() {
    assert.ok(engine.evaluate({
      action: 'iam:CreateVirtualMFADevice',
      resource: 'arn:aws:iam:::mfa/moritzonken',
      variables: {
        aws: {
          CurrentTime: new Date(),
          MultiFactorAuthPresent: false,
          username: 'moritzonken',
        }
      }
    }));
  });

  it('evaluate condition', function() {
    assert.ok(engine.evaluate({
      action: 'iam:DeactivateMFADevice',
      resource: 'arn:aws:iam:::mfa/moritzonken',
      variables: {
        aws: {
          CurrentTime: new Date(),
          MultiFactorAuthPresent: true,
          username: 'moritzonken',
        }
      }
    }));

    assert.ok(!engine.evaluate({
      action: 'iam:DeactivateMFADevice',
      resource: 'arn:aws:iam:::mfa/moritzonken',
      variables: {
        aws: {
          CurrentTime: new Date(),
          MultiFactorAuthPresent: false,
          username: 'moritzonken',
        }
      }
    }));
  });

  it('embedded regex in variable', function() {
    assert.ok(engine.evaluate({
      action: 'iam:CreateVirtualMFADevice',
      resource: 'arn:aws:iam:::mfa/moritzonken',
      variables: {
        aws: {
          CurrentTime: new Date(),
          MultiFactorAuthPresent: true,
          username: 'moritz*',
        }
      }
    }));
  });
});

@@ -25,16 +25,41 @@
 sert'),%0A
+  _ = require('lodash'),%0A
   Engine
@@ -468,12 +468,17 @@
    %22
-B
+EqualsF
 oo
-l
 %22: %7B
@@ -491,38 +491,24 @@
    %22aws:
-MultiFactorAuthPresent
+username
 %22: true%0A
@@ -594,533 +594,203 @@
 end(
-require('./conditions'), 
 %7B%0A
-%0A
   
-%7D),%0A%7D);%0A%0A//engine.validate(policies);%0A//engine.validate(policies%5B0%5D);%0A//engine.validate(policies%5B1%5D);%0A%0Adescribe('policies', function() %7B%0A  it('evaluate', function() %7B%0A    assert.ok(engine.evaluate(%7B%0A      action: 'iam:CreateVirtualMFADevice',%0A      resource: 'arn:aws:iam:::mfa/moritzonken',%0A      variables: %7B%0A        aws: %7B%0A          CurrentTime: new Date(),%0A          MultiFactorAuthPresent: false,%0A          username: 'moritzonken',%0A        %7D%0A      %7D%0A    %7D));%0A  %7D);%0A%0A  it('evaluate condition
+  EqualsFoo: function EqualsFoo(a, b) %7B%0A      return a ? b === 'foo' : b !== 'foo';%0A    %7D%0A  %7D, require('../conditions')),%0A%7D);%0A%0Adescribe('custom condition', function() %7B%0A%0A  it('username is foo
 ', f
@@ -905,35 +905,27 @@
 s:iam:::mfa/
-moritzonken
+foo
 ',%0A      var
@@ -1041,35 +1041,27 @@
  username: '
-moritzonken
+foo
 ',%0A        %7D
@@ -1082,343 +1082,36 @@
 ));%0A
-%0A
   
-  assert.ok(!engine.evaluate(%7B%0A      action: 'iam:DeactivateMFADevice',%0A      resource: 'arn:aws:iam:::mfa/moritzonken',%0A      variables: %7B%0A        aws: %7B%0A          CurrentTime: new Date(),%0A          MultiFactorAuthPresent: false,%0A          username: 'moritzonken',%0A        %7D%0A      %7D%0A    %7D));%0A  %7D);%0A%0A  it('embedded regex in variable
+%7D);%0A%0A  it('username is bar
 ', f
@@ -1128,32 +1128,33 @@
 %7B%0A    assert.ok(
+!
 engine.evaluate(
@@ -1178,21 +1178,18 @@
 iam:
-CreateVirtual
+Deactivate
 MFAD
@@ -1235,19 +1235,11 @@
 mfa/
-moritzonken
+bar
 ',%0A 
@@ -1371,15 +1371,11 @@
 e: '
-moritz*
+bar
 ',%0A 

test for custom condition