'use strict';

import chai from 'chai';
import chaiAsPromised from 'chai-as-promised';
import Chance from 'chance';
import request from 'supertest';
import createapp from '../expressapp';
import TokenStore from '../oauth/tokenstore/inmemory';
import UserStore from '../oauth/userstore/inmemory';
import ConfigStore from '../oauth/configstore/inmemory';

chai.use(chaiAsPromised);
chai.should();

describe('web app', function () {
  var app = null;
  var chance = null;
  var clientId = null;
  var clientSecret = null;
  var config = null;
  var bearerToken = null;

  before(function () {
    chance = new Chance();
    clientId = chance.word({length: 10});
    clientSecret = chance.string();
    config = {a: 'config'};

    var tokenStore = new TokenStore();
    var userStore = new UserStore();
    var configStore = new ConfigStore(config);
    tokenStore.storeClient(clientId, clientSecret);
    app = createapp(tokenStore, userStore, configStore);
  });

  it('should respond with 200 on /', function (done) {
    request(app)
      .get('/')
      .expect(200, done);
  });

  it('should return a token when logging in with client-id and -secret', function (done) {
    request(app)
      .post('/oauth/token')
      .type('form')
      .send({
        grant_type: 'client_credentials',
        client_id: clientId,
        client_secret: clientSecret
      })
      .expect(function(res) {
        var token = JSON.parse(res.text);
        token.should.have.property('access_token').with.length(40);
        token.should.have.property('expires_in');
        token.token_type.should.equal('bearer');
        bearerToken = token.access_token;
      })
      .expect(200, done);
  });

  it('should return configuration when queried for it with a token', function(done) {
    request(app)
      .get('/configuration?token=' + bearerToken)
      .expect(function(res) {
        var returnedConfig = JSON.parse(res.text);
        returnedConfig.should.deep.equal(config);
      })
      .expect(200, done);
  });
});

@@ -508,24 +508,70 @@
 ret = null;%0A
+  var username = null;%0A  var password = null;%0A
   var config
@@ -575,24 +575,24 @@
 fig = null;%0A
-
   var bearer
@@ -730,24 +730,98 @@
 e.string();%0A
+    username = chance.word(%7Blength: 10%7D);%0A    password = chance.string();%0A
     config =
@@ -1008,24 +1008,69 @@
 entSecret);%0A
+    userStore.storeUser(username, password);%0A
     app = cr
@@ -1763,24 +1763,24 @@
 ('bearer');%0A
-
         bear
@@ -1843,32 +1843,602 @@
 , done);%0A  %7D);%0A%0A
+  it('should return a token when logging in with password', function (done) %7B%0A    request(app)%0A      .post('/oauth/token')%0A      .auth(clientId, clientSecret)%0A      .type('form')%0A      .send(%7B%0A        grant_type: 'password',%0A        username: username,%0A        password: password%0A      %7D)%0A      .expect(function(res) %7B%0A        var token = JSON.parse(res.text);%0A        token.should.have.property('access_token').with.length(40);%0A        token.should.have.property('expires_in');%0A        token.token_type.should.equal('bearer');%0A      %7D)%0A      .expect(200, done);%0A  %7D);%0A%0A
   it('should ret

add test for password auth