<commit_before>from __future__ import division, print_function, unicode_literals

import mock
from gittip.testing import Harness, test_website as _test_website
from gittip.testing.client import TestClient
from gittip.elsewhere.bitbucket import BitbucketAccount
from gittip.elsewhere.twitter import TwitterAccount


class Tests(Harness):

    def setUp(self):
        Harness.setUp(self)
        self.website = _test_website
        self.client = TestClient()

    def tearDown(self):
        Harness.tearDown(self)
        self.website.oauth_cache = {}


    @mock.patch('requests.post')
    @mock.patch('requests.get')
    def test_associate_opts_in(self, track, get, post):
        self.website.oauth_cache = {"deadbeef": ("deadbeef", "opt-in", "")}

        post.return_value.status_code = 200
        post.return_value.text = "oauth_token=foo&oauth_token_secret=foo&user_id=foo"

        get.return_value.status_code = 200
        get.return_value.text = '{"id": 1234, "screen_name": "alice"}'

        response = self.client.get("/on/twitter/associate?oauth_token=deadbeef&"
                                   "oauth_verifier=donald_trump")
        assert response.code == 302, response.body
        assert response.headers['Location'] == "/alice/", response.headers


    @mock.patch('requests.post')
    @mock.patch('requests.get')
    def test_associate_connects(self, track, get, post):
        self.make_participant('alice')
        self.website.oauth_cache = {"deadbeef": ("deadbeef", "connect", "")}

        post.return_value.status_code = 200
        post.return_value.text = "oauth_token=foo&oauth_token_secret=foo&user_id=foo"

        get.return_value.status_code = 200
        get.return_value.text = '{"id": 1234, "screen_name": "alice"}'

        response = self.client.get("/on/twitter/associate?oauth_token=deadbeef&"
                                   "oauth_verifier=<PASSWORD>", user="alice")
        assert response.code == 302, response.body
        assert response.headers['Location'] == "/alice/", response.headers

        rec = self.db.one("SELECT * FROM elsewhere")
        assert rec.participant == 'alice', rec
        assert rec.platform == 'twitter', rec


    @mock.patch('requests.post')
    @mock.patch('requests.get')
    def test_associate_confirms_on_connect(self, track, get, post):
        TwitterAccount(self.db, '1234', {'screen_name': 'alice'}).opt_in('alice')

        self.make_participant('bob')
        self.website.oauth_cache = {"deadbeef": ("deadbeef", "connect", "")}

        post.return_value.status_code = 200
        post.return_value.text = "oauth_token=foo&oauth_token_secret=foo&user_id=foo"

        get.return_value.status_code = 200
        get.return_value.text = '{"id": 1234, "screen_name": "alice"}'

        self.client.get('/') # populates cookies['csrf_token']
        response = self.client.get("/on/twitter/associate?oauth_token=deadbeef&"
                                   "oauth_verifier=<PASSWORD>", user="bob")
        assert "Please Confirm" in response.body, response.body


    @mock.patch('requests.post')
    @mock.patch('requests.get')
    def test_confirmation_properly_displays_remaining_bitbucket(self, track, get, post):
        alice, foo = TwitterAccount(self.db, '1234', {'screen_name': 'alice'}).opt_in('alice')
        alice.participant.take_over(BitbucketAccount(self.db, '1234', {'username': 'alice_bb'}))

        self.make_participant('bob')
        self.website.oauth_cache = {"deadbeef": ("deadbeef", "connect", "")}

        post.return_value.status_code = 200
        post.return_value.text = "oauth_token=foo&oauth_token_secret=foo&user_id=foo"

        get.return_value.status_code = 200
        get.return_value.text = '{"id": 1234, "screen_name": "alice"}'

        self.client.get('/') # populates cookies['csrf_token']
        response = self.client.get("/on/twitter/associate?oauth_token=deadbeef&"
                                   "oauth_verifier=<PASSWORD>", user="bob")
        assert response.body.count("alice_bb<br />") == 2, response.body


    def test_can_post_to_take_over(self):
        TwitterAccount(self.db, '1234', {'screen_name': 'alice'}).opt_in('alice')

        self.make_participant('bob')<commit_msg>Fixing the tracking key missing error<commit_after>from __future__ import division, print_function, unicode_literals

import mock
from gittip.testing import Harness, test_website as _test_website
from gittip.testing.client import TestClient
from gittip.elsewhere.bitbucket import BitbucketAccount
from gittip.elsewhere.twitter import TwitterAccount


class Tests(Harness):

    def setUp(self):
        Harness.setUp(self)
        self.website = _test_website
        self.client = TestClient()

    def tearDown(self):
        Harness.tearDown(self)
        self.website.oauth_cache = {}


    @mock.patch('requests.post')
    @mock.patch('requests.get')
    def test_associate_opts_in(self, get, post):
        self.website.oauth_cache = {"deadbeef": ("deadbeef", "opt-in", "")}

        post.return_value.status_code = 200
        post.return_value.text = "oauth_token=foo&oauth_token_secret=foo&user_id=foo"

        get.return_value.status_code = 200
        get.return_value.text = '{"id": 1234, "screen_name": "alice"}'

        response = self.client.get("/on/twitter/associate?oauth_token=<PASSWORD>&"
                                   "oauth_verifier=<PASSWORD>")
        assert response.code == 302, response.body
        assert response.headers['Location'] == "/alice/", response.headers


    @mock.patch('requests.post')
    @mock.patch('requests.get')
    def test_associate_connects(self, get, post):
        self.make_participant('alice')
        self.website.oauth_cache = {"deadbeef": ("deadbeef", "connect", "")}

        post.return_value.status_code = 200
        post.return_value.text = "oauth_token=foo&oauth_token_secret=foo&user_id=foo"

        get.return_value.status_code = 200
        get.return_value.text = '{"id": 1234, "screen_name": "alice"}'

        response = self.client.get("/on/twitter/associate?oauth_token=<PASSWORD>&"
                                   "oauth_verifier=<PASSWORD>", user="alice")
        assert response.code == 302, response.body
        assert response.headers['Location'] == "/alice/", response.headers

        rec = self.db.one("SELECT * FROM elsewhere")
        assert rec.participant == 'alice', rec
        assert rec.platform == 'twitter', rec


    @mock.patch('requests.post')
    @mock.patch('requests.get')
    def test_associate_confirms_on_connect(self, get, post):
        TwitterAccount(self.db, '1234', {'screen_name': 'alice'}).opt_in('alice')

        self.make_participant('bob')
        self.website.oauth_cache = {"deadbeef": ("deadbeef", "connect", "")}

        post.return_value.status_code = 200
        post.return_value.text = "oauth_token=foo&oauth_token_secret=foo&user_id=foo"

        get.return_value.status_code = 200
        get.return_value.text = '{"id": 1234, "screen_name": "alice"}'

        self.client.get('/') # populates cookies['csrf_token']
        response = self.client.get("/on/twitter/associate?oauth_token=deadbeef&"
                                   "oauth_verifier=<PASSWORD>_<PASSWORD>", user="bob")
        assert "Please Confirm" in response.body, response.body


    @mock.patch('requests.post')
    @mock.patch('requests.get')
    def test_confirmation_properly_displays_remaining_bitbucket(self, get, post):
        alice, foo = TwitterAccount(self.db, '1234', {'screen_name': 'alice'}).opt_in('alice')
        alice.participant.take_over(BitbucketAccount(self.db, '1234', {'username': 'alice_bb'}))

        self.make_participant('bob')
        self.website.oauth_cache = {"deadbeef": ("deadbeef", "connect", "")}

        post.return_value.status_code = 200
        post.return_value.text = "oauth_token=foo&oauth_token_secret=foo&user_id=foo"

        get.return_value.status_code = 200
        get.return_value.text = '{"id": 1234, "screen_name": "alice"}'

        self.client.get('/') # populates cookies['csrf_token']
        response = self.client.get("/on/twitter/associate?oauth_token=deadbeef&"
                                   "oauth_verifier=donald_trump", user="bob")
        assert response.body.count("alice_bb<br />") == 2, response.body


    def test_can_post_to_take_over(self):
        TwitterAccount(self.db, '1234', {'screen_name': 'alice'}).opt_in('alice')

        self.make_participant('bob')
{'fraction_non_alphanumeric': 0.14182890855457228, 'fraction_numerical': 0.014867256637168142, 'mean_word_length': 3.2295409181636727, 'pattern_counts': {'":': 24, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1968497', 'n_tokens_mistral': 2892, 'n_tokens_neox': 2668, 'n_words': 422}