<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import urlparse

import tests.util

from google.appengine.api import urlfetch_stub
from minimock import mock, restore

def Dummy_RetrieveURL(self, url, payload, method, headers, request, response,
                      follow_redirects, deadline, validate_certificate):
  response.set_content("{}")

class OAuthTest(tests.util.TestCase):
  root_path = os.path.dirname(os.path.dirname( __file__ )) + "/gae"
  domain = "sample"

  def setUp(self):
    super(OAuthTest, self).setUp()
    mock("urlfetch_stub.URLFetchServiceStub._RetrieveURL", mock_obj=Dummy_RetrieveURL)

  def tearDown(self):
    restore()
    super(OAuthTest, self).tearDown()

  def test_oauth(self):
    response = self.app.get("/oauth/google", status=302)
    assert response.location.startswith("https://accounts.google.com/o/oauth2/auth?")
    state = urlparse.parse_qs(response.location)["state"][0]
    self.app.get("/oauth/google/callback?state={0}".format(state), status=500)
    self.expected_logs = [
      ('ERROR', 'lib/webapp2-2.5.2/webapp2.py', '_internal_error', "'access_token'"),
      ('WARNING', 'google/appengine/ext/ndb/tasklets.py', '_help_tasklet_along', "suspended generator _oauth2_callback(handler.py:230) raised KeyError('access_token')"),
    ]

class OAuthSecretsTest(tests.util.TestCase):
  root_path = os.path.dirname(os.path.dirname( __file__ )) + "/gae"

  def test_secrets(self):
    self.app.get("/oauth/google", status=500)
    self.expected_logs = [
      ('WARNING', 'gae/utils.py', '_get_consumer_info_for', "import_string() failed for 'oauth_config.localhost'. Possible ...found in '/Users/endoh/Documents/works/gogom/gae/oauth_config/__init__.pyc'.\n- 'oauth_config.localhost' not found."),
      ('ERROR', 'lib/webapp2-2.5.2/webapp2.py', '_internal_error', 'cannot import name default'),
    ]

class OAuthSignoutTest(tests.util.TestCase):
  root_path = os.path.dirname(os.path.dirname( __file__ )) + "/gae"
  domain = "sample"
  use_cookie = True

  def test_signout(self):
    assert len(self.app.cookiejar) == 0
    self.app.put("/test/sessions")
    assert len(self.app.cookiejar) == 1
    for cookie in self.app.cookiejar:
      if cookie.name == "__s":
        assert cookie.value is not None
        value = cookie.value
        break
      else:
        raise
    self.app.get("/oauth/signout", status=401)
    assert len(self.app.cookiejar) == 1
    for cookie in self.app.cookiejar:
      if cookie.name == "__s":
        assert cookie.value == value
        break
      else:
        raise
    self.app.get("/oauth/signout", headers={"Referer":"http://sample/"})
    assert len(self.app.cookiejar) == 1
    for cookie in self.app.cookiejar:
      if cookie.name == "__s":
        assert cookie.value != value
        assert len(cookie.value) == 61
        break
      else:
        raise
    self.expected_logs = [
      ('WARNING', 'lib/webapp2-2.5.2/webapp2_extras/securecookie.py', 'deserialize', "Invalid cookie signature u'...'"),
    ]
<commit_msg>fix test<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import urlparse

import tests.util

from google.appengine.api import urlfetch_stub
from minimock import mock, restore

def Dummy_RetrieveURL(self, url, payload, method, headers, request, response,
                      follow_redirects, deadline, validate_certificate):
  response.set_content("{}")

class OAuthTest(tests.util.TestCase):
  root_path = os.path.dirname(os.path.dirname( __file__ )) + "/gae"
  domain = "sample"

  def setUp(self):
    super(OAuthTest, self).setUp()
    mock("urlfetch_stub.URLFetchServiceStub._RetrieveURL", mock_obj=Dummy_RetrieveURL)

  def tearDown(self):
    restore()
    super(OAuthTest, self).tearDown()

  def test_oauth(self):
    response = self.app.get("/oauth/google", status=302)
    assert response.location.startswith("https://accounts.google.com/o/oauth2/auth?")
    state = urlparse.parse_qs(response.location)["state"][0]
    self.app.get("/oauth/google/callback?state={0}".format(state), status=500)
    self.expected_logs = [
      ('ERROR', 'lib/webapp2-2.5.2/webapp2.py', '_internal_error', "'access_token'"),
      ('WARNING', 'google/appengine/ext/ndb/tasklets.py', '_help_tasklet_along', "suspended generator _oauth2_callback(handler.py:230) raised KeyError('access_token')"),
    ]

class OAuthSecretsTest(tests.util.TestCase):
  root_path = os.path.dirname(os.path.dirname( __file__ )) + "/gae"

  def test_secrets(self):
    self.app.get("/oauth/google", status=500)
    self.expected_logs = [
      ('WARNING', 'gae/utils.py', 'oauth_config', "import_string() failed for 'oauth_config.localhost'. Possible reasons are...g' found in '.../gae/oauth_config/__init__.py...'.\n- 'oauth_config.localhost' not found."),
      ('ERROR', 'lib/webapp2-2.5.2/webapp2.py', '_internal_error', 'cannot import name default'),
    ]

class OAuthSignoutTest(tests.util.TestCase):
  root_path = os.path.dirname(os.path.dirname( __file__ )) + "/gae"
  domain = "sample"
  use_cookie = True

  def test_signout(self):
    assert len(self.app.cookiejar) == 0
    self.app.put("/test/sessions")
    assert len(self.app.cookiejar) == 1
    for cookie in self.app.cookiejar:
      if cookie.name == "__s":
        assert cookie.value is not None
        value = cookie.value
        break
      else:
        raise
    self.app.get("/oauth/signout", status=401)
    assert len(self.app.cookiejar) == 1
    for cookie in self.app.cookiejar:
      if cookie.name == "__s":
        assert cookie.value == value
        break
      else:
        raise
    self.app.get("/oauth/signout", headers={"Referer":"http://sample/"})
    assert len(self.app.cookiejar) == 1
    for cookie in self.app.cookiejar:
      if cookie.name == "__s":
        assert cookie.value != value
        assert len(cookie.value) == 61
        break
      else:
        raise
    self.expected_logs = [
      ('WARNING', 'lib/webapp2-2.5.2/webapp2_extras/securecookie.py', 'deserialize', "Invalid cookie signature u'...'"),
    ]

{'fraction_non_alphanumeric': 0.15209501814582646, 'fraction_numerical': 0.013526888815572418, 'mean_word_length': 3.3839479392624727, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '2806469', 'n_tokens_mistral': 2088, 'n_tokens_neox': 2020, 'n_words': 383}