<commit_before><link rel="import" href="../../polymer-test-tools/tools.html">

<script src="../../polymer-test-tools/mocha-htmltest.js"></script>
<script src="../../chai-as-promised/lib/chai-as-promised.js"></script>
<script src="../vendor/sinon-master.js"></script>

<link rel="import" href="../polymer-devise.html">

<script>
(function () {

  function getValueForKey(obj, key) {
    for (var k in obj)
      if (obj.hasOwnProperty(k) && k.toLowerCase() === key.toLowerCase())
        return obj[k];
  }

  mocha.setup({ui: 'tdd', slow: 1000, timeout: 5000, htmlbase: ''});

  /**
   * Test that polymer-devise behaves properly when declared in HTML
   */
  htmlSuite('HTML tests', function() {
    htmlTest('polymer-devise-default-configuration.html');
    htmlTest('polymer-devise-can-configure.html');
    htmlTest('polymer-devise-global-state.html');
  });

  /**
   * Test that polymer-devise functions properly, creating the element via JavaScript
   */
  suite('Service tests', function () {

    suite('#login', function () {
      var xhr,
          devise,
          requests;

      setup(function () {
        devise = document.createElement('polymer-devise');
        document.body.appendChild(devise);

        xhr = sinon.useFakeXMLHttpRequest();
        requests = [];
        xhr.onCreate = function (xhr) {
          requests.push(xhr);
        };
      });

      teardown(function () {
        xhr.restore();
      });

      test('POSTS to /users/sign_in.json by default', function (done) {
        done();
      });

      test('POSTS to the configured path', function (done) {
        done();
      });

      test('Uses the configured HTTP method', function (done) {
        done();
      });

      test('Rejects with an error message upon failing to login', function (done) {
        done();
      });

      test('Fires "login" upon successful login', function (done) {
        done();
      });

      test('Fires "new-session" upon restoring an existing server session', function (done) {
        done();
      });

    });

    suite('#logout', function () {
      var xhr,
          devise,
          requests;

      setup(function () {
        devise = document.createElement('polymer-devise');
        document.body.appendChild(devise);

        xhr = sinon.useFakeXMLHttpRequest();
        requests = [];
        xhr.onCreate = function (xhr) {
          requests.push(xhr);
        };
      });

      teardown(function () {
        xhr.restore();
      });

      test('DELETES to /users/sign_out.json by default', function (done) {
        done();
      });

      test('DELETES to the configured path', function (done) {
        done();
      });

      test('Uses the configured HTTP method', function (done) {
        done();
      });

      test('Rejects with an error message upon failing to logout', function (done) {
        done();
      });

      test('Fires "logout" upon successful logout', function (done) {
        done();
      });

    });

    suite('#register', function () {
      var xhr,
          devise,
          requests;

      /*
       * Before each test, create a <polymer-devise> element and prepare FakeXMLHttpRequest
       */
      setup(function () {
        devise = document.createElement('polymer-devise');
        document.body.appendChild(devise);

        xhr = sinon.useFakeXMLHttpRequest();
        requests = [];
        xhr.onCreate = function (xhr) {
          requests.push(xhr);
        };
      });

      /*
       * After each test, restore the native XMLHttpRequest
       */
      teardown(function () {
        xhr.restore();
      });

      test('POSTS to /users.json by default', function (done) {
        var credentials = {
          email: '<EMAIL>',
          password: '<PASSWORD>',
          password_confirmation: '<PASSWORD>'
        };

        // Call #register()
        assert.becomes(devise.register(credentials), {
          id: 1,
          name: 'test',
          email: '<EMAIL>'
        }, 'resolves with registered user').notify(done);

        assert.lengthOf(requests, 1,
            'a request has been made');
        assert.equal(requests[0].method, 'POST',
            'the request is using POST');
        assert.equal(requests[0].url, '/users.json',
            'the request is using the correct default path');
        assert.isDefined(getValueForKey(requests[0].requestHeaders, 'Content-Type'),
            'the request has a Content-Type header');
        assert.include(getValueForKey(requests[0].requestHeaders, 'Content-Type'), 'application/json',
            'the request has Content-Type: application/json');
        assert.deepEqual(requests[0].requestBody, '{"user":{"email":"<EMAIL>","password":"<PASSWORD>","password_confirmation":"<PASSWORD>"}}',
            'the request body should match the passed credentials');

        requests[0].respond(200, null, '{"id": 1, "name": "test", "email": "<EMAIL>"}');
      });

      test('POSTS to the configured path', function (done) {
        // Set a custom path via an attribute change
        devise.setAttribute('registerPath', '/user/sign_up.json');

        var credentials = {
          email: '<EMAIL>',
          password: '<PASSWORD>',
          password_confirmation: '<PASSWORD>'
        };

        // Call #register()
        assert.becomes(devise.register(credentials), {
          id: 1,
          name: 'test',
          email: '<EMAIL>'
        }, 'resolves with registered user').notify(done);

        assert.lengthOf(requests, 1,
            'a request has been made');
        assert.equal(requests[0].url, '/user/sign_up.json',
            'the request is using the correct default path');

        requests[0].respond(200, null, '{"id": 1, "name": "test", "email": "<EMAIL>"}');
      });

      test('Uses the configured HTTP method', function (done) {
        // Set a custom HTTP method via an attribute change
        devise.setAttribute('registerMethod', 'PATCH');

        var credentials = {
          email: '<EMAIL>',
          password: '<PASSWORD>',
          password_confirmation: '<PASSWORD>'
        };

        // Call #register()
        assert.becomes(devise.register(credentials), {
          id: 1,
          name: 'test',
          email: '<EMAIL>'
        }, 'resolves with registered user').notify(done);

        assert.lengthOf(requests, 1,
            'a request has been made');
        assert.equal(requests[0].method, 'PATCH',
            'the request is using PATCH');

        requests[0].respond(200, null, '{"id": 1, "name": "test", "email": "<EMAIL>"}');
      });

      test('Rejects with an error message upon failing to register', function (done) {
        var credentials = {
          email: '<EMAIL>',
          password: '<PASSWORD>',
          password_confirmation: '<PASSWORD>'
        };

        // Call #register()
        assert.isRejected(devise.register(credentials), {
          id: 1,
          name: 'test',
          email: '<EMAIL>'
        }, {
          "errors": {
            "email": ["can't be blank"],
            "password": ["<PASSWORD>"]
          }
        }, 'rejects with the error object provided by the server').notify(done);

        requests[0].respond(422, null, '{"errors":{"email":["can\'t be blank"],"password":["<PASSWORD>"]}}');
      });

      test('Fires "new-registration" upon successful registration', function (done) {
        var credentials = {
          email: '<EMAIL>',
          password: '<PASSWORD>',
          password_confirmation: '<PASSWORD>'
        };

        var spy = sinon.spy();
        devise.addEventListener('new-registration', spy);

        // Call #register()
        devise.register(credentials).then(function () {
          sinon.assert.calledOnce(spy);
          sinon.assert.calledWith(spy, {
            id: 1,
            name: 'test',
            email: '<EMAIL>'
          });
          done();
        });

        assert.lengthOf(requests, 1,
            'a request has been made');

        requests[0].respond(200, null, '{"id": 1, "name": "test", "email": "<EMAIL>"}');
      });

    });

  });

  mocha.run();

}());<commit_msg>Finally got the promise test to work Ugh<commit_after><link rel="import" href="../../polymer-test-tools/tools.html">

<script src="../../polymer-test-tools/mocha-htmltest.js"></script>
<script src="../../chai-as-promised/lib/chai-as-promised.js"></script>
<script src="../vendor/sinon-master.js"></script>

<link rel="import" href="../polymer-devise.html">

<script>
(function () {

  function getValueForKey(obj, key) {
    for (var k in obj)
      if (obj.hasOwnProperty(k) && k.toLowerCase() === key.toLowerCase())
        return obj[k];
  }

  mocha.setup({ui: 'tdd', slow: 1000, timeout: 5000, htmlbase: ''});

  /**
   * Test that polymer-devise behaves properly when declared in HTML
   */
  htmlSuite('HTML tests', function() {
    htmlTest('polymer-devise-default-configuration.html');
    htmlTest('polymer-devise-can-configure.html');
    htmlTest('polymer-devise-global-state.html');
  });

  /**
   * Test that polymer-devise functions properly, creating the element via JavaScript
   */
  suite('Service tests', function () {

    suite('#login', function () {
      var xhr,
          devise,
          requests;

      setup(function () {
        devise = document.createElement('polymer-devise');
        document.body.appendChild(devise);

        xhr = sinon.useFakeXMLHttpRequest();
        requests = [];
        xhr.onCreate = function (xhr) {
          requests.push(xhr);
        };
      });

      teardown(function () {
        xhr.restore();
      });

      test('POSTS to /users/sign_in.json by default', function (done) {
        done();
      });

      test('POSTS to the configured path', function (done) {
        done();
      });

      test('Uses the configured HTTP method', function (done) {
        done();
      });

      test('Rejects with an error message upon failing to login', function (done) {
        done();
      });

      test('Fires "login" upon successful login', function (done) {
        done();
      });

      test('Fires "new-session" upon restoring an existing server session', function (done) {
        done();
      });

    });

    suite('#logout', function () {
      var xhr,
          devise,
          requests;

      setup(function () {
        devise = document.createElement('polymer-devise');
        document.body.appendChild(devise);

        xhr = sinon.useFakeXMLHttpRequest();
        requests = [];
        xhr.onCreate = function (xhr) {
          requests.push(xhr);
        };
      });

      teardown(function () {
        xhr.restore();
      });

      test('DELETES to /users/sign_out.json by default', function (done) {
        done();
      });

      test('DELETES to the configured path', function (done) {
        done();
      });

      test('Uses the configured HTTP method', function (done) {
        done();
      });

      test('Rejects with an error message upon failing to logout', function (done) {
        done();
      });

      test('Fires "logout" upon successful logout', function (done) {
        done();
      });

    });

    suite('#register', function () {
      var xhr,
          devise,
          requests;

      /*
       * Before each test, create a <polymer-devise> element and prepare FakeXMLHttpRequest
       */
      setup(function () {
        devise = document.createElement('polymer-devise');
        document.body.appendChild(devise);

        xhr = sinon.useFakeXMLHttpRequest();
        requests = [];
        xhr.onCreate = function (xhr) {
          requests.push(xhr);
        };
      });

      /*
       * After each test, restore the native XMLHttpRequest
       */
      teardown(function () {
        xhr.restore();
      });

      test('POSTS to /users.json by default', function (done) {
        var credentials = {
          email: '<EMAIL>',
          password: '<PASSWORD>',
          password_confirmation: '<PASSWORD>'
        };

        // Call #register()
        assert.becomes(devise.register(credentials), {
          id: 1,
          name: 'test',
          email: '<EMAIL>'
        }, 'resolves with registered user').notify(done);

        assert.lengthOf(requests, 1,
            'a request has been made');
        assert.equal(requests[0].method, 'POST',
            'the request is using POST');
        assert.equal(requests[0].url, '/users.json',
            'the request is using the correct default path');
        assert.isDefined(getValueForKey(requests[0].requestHeaders, 'Content-Type'),
            'the request has a Content-Type header');
        assert.include(getValueForKey(requests[0].requestHeaders, 'Content-Type'), 'application/json',
            'the request has Content-Type: application/json');
        assert.deepEqual(requests[0].requestBody, '{"user":{"email":"<EMAIL>","password":"<PASSWORD>","password_confirmation":"<PASSWORD>"}}',
            'the request body should match the passed credentials');

        requests[0].respond(200, null, '{"id": 1, "name": "test", "email": "<EMAIL>"}');
      });

      test('POSTS to the configured path', function (done) {
        // Set a custom path via an attribute change
        devise.setAttribute('registerPath', '/user/sign_up.json');

        var credentials = {
          email: '<EMAIL>',
          password: '<PASSWORD>',
          password_confirmation: '<PASSWORD>'
        };

        // Call #register()
        assert.becomes(devise.register(credentials), {
          id: 1,
          name: 'test',
          email: '<EMAIL>'
        }, 'resolves with registered user').notify(done);

        assert.lengthOf(requests, 1,
            'a request has been made');
        assert.equal(requests[0].url, '/user/sign_up.json',
            'the request is using the correct default path');

        requests[0].respond(200, null, '{"id": 1, "name": "test", "email": "<EMAIL>"}');
      });

      test('Uses the configured HTTP method', function (done) {
        // Set a custom HTTP method via an attribute change
        devise.setAttribute('registerMethod', 'PATCH');

        var credentials = {
          email: '<EMAIL>',
          password: '<PASSWORD>',
          password_confirmation: '<PASSWORD>'
        };

        // Call #register()
        assert.becomes(devise.register(credentials), {
          id: 1,
          name: 'test',
          email: '<EMAIL>'
        }, 'resolves with registered user').notify(done);

        assert.lengthOf(requests, 1,
            'a request has been made');
        assert.equal(requests[0].method, 'PATCH',
            'the request is using PATCH');

        requests[0].respond(200, null, '{"id": 1, "name": "test", "email": "<EMAIL>"}');
      });

      test('Rejects with an error message upon failing to register', function (done) {
        var credentials = {
          email: '<EMAIL>',
          password: '<PASSWORD>',
          password_confirmation: '<PASSWORD>'
        };

        // Call #register()
        assert.isRejected(devise.register(credentials), {
          id: 1,
          name: 'test',
          email: '<EMAIL>'
        }, {
          "errors": {
            "email": ["can't be blank"],
            "password": ["<PASSWORD>"]
          }
        }, 'rejects with the error object provided by the server').notify(done);

        requests[0].respond(422, null, '{"errors":{"email":["can\'t be blank"],"password":["<PASSWORD>\'<PASSWORD>"]}}');
      });

      test('Fires "new-registration" upon successful registration', function (done) {
        var credentials = {
          email: '<EMAIL>',
          password: '<PASSWORD>',
          password_confirmation: '<PASSWORD>'
        };

        var spy = sinon.spy();
        devise.addEventListener('new-registration', spy);

        // Call #register()
        assert.isFulfilled(devise.register(credentials)).then(function () {
          assert.isTrue(spy.calledOnce, 'the "new-registration" callback was called');
          assert.lengthOf(spy.args[0], 1, 'the "new-registration" callback was called with one argument');
          assert.deepEqual(spy.args[0][0].detail, {
            id: 1,
            name: 'test',
            email: '<EMAIL>'
          }, 'the "new-registration" callback was called with user data in event.detail');
        }).then(function () {
          done();
        }, function (error) {
          done(error);
        });

        assert.lengthOf(requests, 1,
            'a request has been made');

        requests[0].respond(200, null, '{"id": 1, "name": "test", "email": "<EMAIL>"}');
      });

    });

  });

  mocha.run();

}());
{'fraction_non_alphanumeric': 0.15808383233532936, 'fraction_numerical': 0.005988023952095809, 'mean_word_length': 2.1708752610594266, 'pattern_counts': {'":': 44, '<': 82, '<?xml version=': 0, '>': 82, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7102775', 'n_tokens_mistral': 4842, 'n_tokens_neox': 4652, 'n_words': 1122}