<commit_before>describe('SatellizerOauth2', function() {

  beforeEach(module('satellizer'));

  beforeEach(inject(function($timeout, $httpBackend, $interval, SatellizerOauth2, SatellizerConfig) {
    this.$timeout = $timeout;
    this.$httpBackend = $httpBackend;
    this.$interval = $interval;
    this.oauth2 = new SatellizerOauth2();
    this.config = SatellizerConfig;
  }));

  describe('open()', function() {

    it('should be defined', function() {
      expect(this.oauth2.open).toBeDefined();
    });

    it('should start the oauth 2.0 flow', function() {
      //this.oauth2.open();
    });

  });

  describe('exchangeForToken()', function() {

    it('should be defined', function() {
      expect(this.oauth2.exchangeForToken).toBeDefined();
    });

    it('should exchange code for token', function() {
      this.oauth2.exchangeForToken('code=foo');
    });

    it('should exchange code for token with custom responseParams', function() {
      this.config.providers.facebook.responseParams = {
        code: 'code',
        client_id: 'clientId',
        redirect_uri: 'redirectUri',
        custom: 'custom'
      };
      this.oauth2.open(this.config.providers.facebook);
      this.$timeout.flush();
      this.$timeout.verifyNoPendingTasks();
    });

    it('should handle state param', function() {
      this.oauth2.open(this.config.providers.facebook);
      this.oauth2.exchangeForToken({ state: 'STATE' });
    });

  });

  describe('buildQueryString()', function() {
    it('should be defined', function() {
      expect(this.oauth2.buildQueryString).toBeDefined();
    });

    it('should URI-encode state value', function() {
      var oauth2 = this.oauth2.open({defaultUrlParams: ['state'], state: 'foo+bar'});

      oauth2.then(function () {
        expect(this.oauth2.buildQueryString()).toContain('state=foo%2Bbar');
      });

      this.$timeout.flush();
      this.$timeout.verifyNoPendingTasks();
    });

    it('should use scopePrefix if provided', function() {
      this.oauth2.open(this.config.providers.google)
        .then(function () {
          expect(this.oauth2.buildQueryString()).toContain('scope=openid profile email');
        });
      this.$timeout.flush();
      this.$timeout.verifyNoPendingTasks();
    });

    it('should remove redirect_uri if param redirectUrl is null', function() {
      var tempProvider = angular.copy(this.config.providers.google);
      tempProvider.redirectUri = null;
      this.oauth2.open(tempProvider)
        .then(function () {
          expect(this.oauth2.buildQueryString()).not.toContain('redirect_uri=');
        });
      this.$timeout.flush();
      this.$timeout.verifyNoPendingTasks();
    });

  });

});

<commit_msg>Add test for Popup promise rejections<commit_after>describe('SatellizerOauth2', function() {

  beforeEach(module('satellizer'));

  beforeEach(inject(function($timeout, $httpBackend, $interval, SatellizerOauth2, SatellizerConfig) {
    this.$timeout = $timeout;
    this.$httpBackend = $httpBackend;
    this.$interval = $interval;
    this.oauth2 = new SatellizerOauth2();
    this.config = SatellizerConfig;
  }));

  describe('open()', function() {

    it('should be defined', function() {
      expect(this.oauth2.open).toBeDefined();
    });

    it('should start the oauth 2.0 flow', function() {
      //this.oauth2.open();
    });

    it('should propagate promise rejections', inject(function($q, SatellizerPopup) {
      var error;
      var message = 'Popup Rejected';
      var defer = $q.defer();

      spyOn(SatellizerPopup, 'pollPopup').and.returnValue(defer.promise);

      this.oauth2.open(this.config.providers.github).catch(function (err) {
        error = err;
      });

      defer.reject(message);
      this.$timeout.flush();
      this.$timeout.verifyNoPendingTasks();

      expect(error).toEqual(message);
    }));

  });

  describe('exchangeForToken()', function() {

    it('should be defined', function() {
      expect(this.oauth2.exchangeForToken).toBeDefined();
    });

    it('should exchange code for token', function() {
      this.oauth2.exchangeForToken('code=foo');
    });

    it('should exchange code for token with custom responseParams', function() {
      this.config.providers.facebook.responseParams = {
        code: 'code',
        client_id: 'clientId',
        redirect_uri: 'redirectUri',
        custom: 'custom'
      };
      this.oauth2.open(this.config.providers.facebook);
      this.$timeout.flush();
      this.$timeout.verifyNoPendingTasks();
    });

    it('should handle state param', function() {
      this.oauth2.open(this.config.providers.facebook);
      this.oauth2.exchangeForToken({ state: 'STATE' });
    });

  });

  describe('buildQueryString()', function() {
    it('should be defined', function() {
      expect(this.oauth2.buildQueryString).toBeDefined();
    });

    it('should URI-encode state value', function() {
      var oauth2 = this.oauth2.open({defaultUrlParams: ['state'], state: 'foo+bar'});

      oauth2.then(function () {
        expect(this.oauth2.buildQueryString()).toContain('state=foo%2Bbar');
      });

      this.$timeout.flush();
      this.$timeout.verifyNoPendingTasks();
    });

    it('should use scopePrefix if provided', function() {
      this.oauth2.open(this.config.providers.google)
        .then(function () {
          expect(this.oauth2.buildQueryString()).toContain('scope=openid profile email');
        });
      this.$timeout.flush();
      this.$timeout.verifyNoPendingTasks();
    });

    it('should remove redirect_uri if param redirectUrl is null', function() {
      var tempProvider = angular.copy(this.config.providers.google);
      tempProvider.redirectUri = null;
      this.oauth2.open(tempProvider)
        .then(function () {
          expect(this.oauth2.buildQueryString()).not.toContain('redirect_uri=');
        });
      this.$timeout.flush();
      this.$timeout.verifyNoPendingTasks();
    });

  });

});


{'fraction_non_alphanumeric': 0.15905669844455594, 'fraction_numerical': 0.007860846295367119, 'mean_word_length': 3.1964912280701756, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '767150', 'n_tokens_mistral': 1824, 'n_tokens_neox': 1784, 'n_words': 294}