const expect = require('expect');
const request = require('supertest');
const express = require('express');

const middlewares = require('../src/middlewares');


describe('#middlewares', function() {
  describe('#dashboardAdmins', function() {
    it('should throw "Domain is required" error', function(done) {
      expect(function() {
        middlewares.dashboardAdmins();
      })
        .toThrow(/Domain is required/);

      done();
    });

    it('should throw "title is required" error', function(done) {
      expect(function() {
        middlewares.dashboardAdmins('domain');
      })
        .toThrow(/title is required/);

      done();
    });

    it('should work', function(done) {
      const app = express();
      const middle = middlewares.dashboardAdmins('example.com', 'Title');

      app.use('/', middle, function(req, res) {
        res.send();
      });

      request(app)
        .get('/')
        .expect(200)
        .end(function(err) {
          if (err) throw err;
          done();
        });
    });
  });

  describe('#requireUser', function() {
    it('should return "Authentication required for this endpoint" error', function(done) {
      const app = express();

      app.use('/', middlewares.requireUser);

      request(app)
        .get('/')
        .expect(401)
        .end(function(err, res) {
          if (err) throw err;
          expect(res.error).toMatch(/UnauthorizedError: Authentication required for this endpoint/);
          done();
        });
    });

    it('should work', function(done) {
      const app = express();
      const addUser = function(req, res, next) {
        req.user = { name: 'User' };
        next();
      };

      app.use('/', addUser, middlewares.requireUser, function(req, res) {
        res.send();
      });

      request(app)
        .get('/')
        .expect(200)
        .end(function(err) {
          if (err) throw err;
          done();
        });
    });
  });
});

@@ -1240,16 +1240,33 @@
 uireUser
+('foo.auth0.com')
 );%0A%0A    
@@ -1404,21 +1404,20 @@
 ect(res.
-error
+text
 ).toMatc
@@ -1672,16 +1672,58 @@
 : 'User'
+, aud: %5B 'https://foo.auth0.com/api/v2/' %5D
  %7D;%0A    
@@ -1795,16 +1795,33 @@
 uireUser
+('foo.auth0.com')
 , functi
@@ -2016,23 +2016,648 @@
  %7D);%0A    %7D);
+%0A%0A    it('should validate the audience', function(done) %7B%0A      const app = express();%0A      const addUser = function(req, res, next) %7B%0A        req.user = %7B name: 'User', aud: %5B 'https://somethingelse.auth0.com/api/v2/' %5D %7D;%0A        next();%0A      %7D;%0A%0A      app.use('/', addUser, middlewares.requireUser('foo.auth0.com'), function(req, res) %7B%0A        res.send();%0A      %7D);%0A%0A      request(app)%0A        .get('/')%0A        .expect(401)%0A        .end(function(err, res) %7B%0A          if (err) throw err;%0A          expect(res.text).toMatch(/Invalid token. Audience does not match: foo.auth0.com/);%0A          done();%0A        %7D);%0A    %7D);
 %0A  %7D);%0A%7D);%0A

Test aud validation in source control tools