Ejemplo n.º 1
0
class StaticViewTests(unittest.TestCase):
    def setUp(self):
        super(StaticViewTests, self).setUp()

        self.root = os.path.dirname(__file__)
        self.view = StaticView.as_view(show_indexes=False,
                                       document_root=self.root,
                                       use_request_path=True)
        self.client = TestClient(self.view)

    def test_show_indexes_disabled_by_default(self):
        self.assertFalse(StaticView().show_indexes)

    def test_directory_index_404s_when_disabled(self):
        with self.assertRaises(Http404):
            self.client.get('/')

    def test_directory_index(self):
        self.view = StaticView.as_view(show_indexes=True,
                                       document_root=self.root,
                                       use_request_path=True)
        self.client = TestClient(self.view)

        def strip(content):
            return content.replace('\n', '').replace(' ', '')

        response = self.client.get('/fixture')
        self.assertEqual(strip(response.content),
                strip(FIXTURE_DIRECTORY_INDEX))

    def test_file(self):
        response = self.client.get('/fixture/file1.py')
        self.assertEqual(response.content, b'Python!\n')
        self.assertEqual(response.headers['Content-Type'], 'text/x-python')
        self.assertEqual(response.headers['Content-Length'], '8')
Ejemplo n.º 2
0
class AuthorisationDetailViewTests(unittest.TestCase):
    def setUp(self):
        self.client = TestClient(router)

        self.device = Device.create(apns_token='ec1752bd70320e4763f7165d73e2636cca9e25cf')
        self.token = Token.create(device=self.device, token='e4763f7165d73e2636cca9e', scope=Token.ALL_SCOPE)

    def tearDown(self):
        if self.token:
            self.token.delete_instance()

        self.device.delete_instance()

    def test_get(self):
        headers = { 'AUTHORIZATION': 'token e4763f7165d73e2636cca9e' }
        response = self.client.get('/authorisations/636cca9e', {}, headers)

        self.assertEqual(response.status_code, 200)
        self.assertEqual(json.loads(response.content), {
            'url': '/authorisations/636cca9e',
            'scopes': ['all'],
            'token_last_eight': '636cca9e'
        })

    def test_delete(self):
        headers = { 'AUTHORIZATION': 'token e4763f7165d73e2636cca9e' }
        response = self.client.delete('/authorisations/636cca9e', {}, headers)

        self.assertEqual(response.status_code, 204)
        self.assertEqual(Token.select().count(), 0)

        self.token = None
Ejemplo n.º 3
0
class TestClientTests(unittest.TestCase):
    def setUp(self):
        def view(request):
            return Response('{} {}'.format(request.method, request.path))

        self.client = TestClient(view)

    def test_http(self):
        response = self.client.http('METHOD', '/path/')
        self.assertEqual(response.content, 'METHOD /path/')

    def test_get(self):
        response = self.client.get('/')
        self.assertEqual(response.content, 'GET /')

    def test_put(self):
        response = self.client.put('/')
        self.assertEqual(response.content, 'PUT /')

    def test_post(self):
        response = self.client.post('/')
        self.assertEqual(response.content, 'POST /')

    def test_delete(self):
        response = self.client.delete('/resource')
        self.assertEqual(response.content, 'DELETE /resource')
Ejemplo n.º 4
0
class ExampleTests(unittest.TestCase):
    def setUp(self):
        self.client = TestClient(hello_world)

    def test200(self):
       assert self.client.get('/').status_code is 200

    def testResponseContent(self):
        self.assertEqual(self.client.get('/').content, 'Hello World')
Ejemplo n.º 5
0
class GitHubCallbackTests(unittest.TestCase):
    def setUp(self):
        self.client = TestClient(middleware)

    def test_successfully_authenticates_user_from_github(self):
        response = self.client.get('/callback', {'code': 5})
        self.assertEqual(response.status_code, 200)
        entrant = Entrant.select().where(Entrant.email == '*****@*****.**').get()
        self.assertEqual(entrant.name, 'kylef')
        entrant.delete_instance()

    def test_with_invitation(self):
        entrant = Entrant.create(email='*****@*****.**', name='Kyle', github_username='******')
        invitation = entrant.invite()
        invitation.save()

        response = self.client.get('/callback', {'code': 5})
        self.assertEqual(response.status_code, 302)
        self.assertEqual(
            response.headers['Location'],
            'https://sotu.cocoapods.org/invitation/{}'.format(invitation.code)
        )

        entrant.delete_instance()
        invitation.delete_instance()

    def test_with_rejected_invitation(self):
        entrant = Entrant.create(email='*****@*****.**', name='Kyle', github_username='******')
        invitation = entrant.invite()
        invitation.state = Invitation.REJECTED_STATE
        invitation.save()

        response = self.client.get('/callback', {'code': 5})
        self.assertEqual(response.status_code, 302)
        self.assertEqual(
            response.headers['Location'],
            'https://sotu.cocoapods.org/invitation/{}/reject'.format(invitation.code)
        )

        entrant.delete_instance()
        invitation.delete_instance()

    def test_with_accepted_invitation(self):
        entrant = Entrant.create(email='*****@*****.**', name='Kyle', github_username='******')
        invitation = entrant.invite()
        invitation.state = Invitation.ACCEPTED_STATE
        invitation.save()

        response = self.client.get('/callback', {'code': 5})
        self.assertEqual(response.status_code, 302)
        self.assertEqual(
            response.headers['Location'],
            'https://sotu.cocoapods.org/invitation/{}/accept'.format(invitation.code)
        )

        entrant.delete_instance()
        invitation.delete_instance()
Ejemplo n.º 6
0
 def setUp(self):
     self.client = TestClient(middleware)
     self.entrant = Entrant.create(email='*****@*****.**',
                                   name='Kyle',
                                   github_username='******')
     self.invitation = self.entrant.invite()
     self.path = '/invitation/{}'.format(self.invitation.code)
Ejemplo n.º 7
0
class IndexTests(unittest.TestCase):
    def setUp(self):
        self.client = TestClient(middleware)

    def test_index_links_to_github_authorization(self):
        response = self.client.get('/')
        self.assertEqual(response.status_code, 200)
        self.assertTrue('https://github.com/login/oauth/authorize' in response.content)
Ejemplo n.º 8
0
    def setUp(self):
        super(StaticViewTests, self).setUp()

        self.root = os.path.dirname(__file__)
        self.view = StaticView.as_view(show_indexes=False,
                                       document_root=self.root,
                                       use_request_path=True)
        self.client = TestClient(self.view)
Ejemplo n.º 9
0
class IndexTests(unittest.TestCase):
    def setUp(self):
        self.client = TestClient(middleware)

    def test_index_links_to_github_authorization(self):
        response = self.client.get('/')
        self.assertEqual(response.status_code, 200)
        self.assertTrue(
            'https://github.com/login/oauth/authorize' in response.content)
Ejemplo n.º 10
0
    def test_directory_index(self):
        self.view = StaticView.as_view(show_indexes=True,
                                       document_root=self.root,
                                       use_request_path=True)
        self.client = TestClient(self.view)

        def strip(content):
            return content.replace('\n', '').replace(' ', '')

        response = self.client.get('/fixture')
        self.assertEqual(strip(response.content),
                strip(FIXTURE_DIRECTORY_INDEX))
Ejemplo n.º 11
0
    def setUp(self):
        def view(request):
            return Response('{} {}'.format(request.method, request.path))

        self.client = TestClient(view)
Ejemplo n.º 12
0
 def setUp(self):
     self.client = TestClient(middleware)
Ejemplo n.º 13
0
class ViewTests(unittest.TestCase):
    def setUp(self):
        self.client = TestClient(router)

    def test_status(self):
        assert self.client.get('/').status_code is 204

    def test_register(self):
        response = self.client.post('/1/devices', {'device_token': 'test_token'})
        self.assertEqual(response.status_code, 201)
        self.assertEqual(response.headers['Content-Type'], 'application/json')
        self.assertEqual(response.content, '{"device_token": "test_token", "push_token": "ec1752bd70320e4763f7165d73e2636cca9e25cf"}')

        device = Device.get(apns_token='test_token')
        assert device

        push_token = Token.select().where(Token.device == device, Token.token == 'ec1752bd70320e4763f7165d73e2636cca9e25cf').get()
        token = Token.select().where(Token.device == device, Token.token == 'test_token').get()

        token.delete_instance()
        push_token.delete_instance()
        device.delete_instance()

    def test_returns_200_when_re_registering(self):
        response = self.client.post('/1/devices', {'device_token': 'test_token'})
        response = self.client.post('/1/devices', {'device_token': 'test_token'})
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.headers['Content-Type'], 'application/json')
        self.assertEqual(response.content, '{"device_token": "test_token", "push_token": "ec1752bd70320e4763f7165d73e2636cca9e25cf"}')

        device = Device.get(apns_token='test_token')
        assert device

        push_token = Token.select().where(Token.device == device, Token.token == 'ec1752bd70320e4763f7165d73e2636cca9e25cf').get()
        token = Token.select().where(Token.device == device, Token.token == 'test_token').get()

        push_token.delete_instance()
        token.delete_instance()
        device.delete_instance()

    def test_push_401_missing_token(self):
        response = self.client.post('/1/push', {})
        self.assertEqual(response.status_code, 401)

    def test_push(self):
        enqueued = []

        def enqueue(*args):
            enqueued.append(args)
        queue.enqueue = enqueue

        device = Device.create(apns_token='ec1752bd70320e4763f7165d73e2636cca9e25cf')
        token = Token.create(device=device, token='valid', scope=Token.ALL_SCOPE)

        headers = { 'AUTHORIZATION': 'token valid' }
        response = self.client.post('/1/push', {}, headers)
        self.assertEqual(response.status_code, 202)
        self.assertEqual(len(enqueued), 1)

        token.delete_instance()
        device.delete_instance()
Ejemplo n.º 14
0
 def setUp(self):
     self.client = TestClient(middleware)
Ejemplo n.º 15
0
class AuthorisationListViewTests(unittest.TestCase):
    def setUp(self):
        self.client = TestClient(router)

        self.device = Device.create(apns_token='ec1752bd70320e4763f7165d73e2636cca9e25cf')
        self.token = Token.create(device=self.device, token='e4763f7165d73e2636cca9e', scope=Token.ALL_SCOPE)

    def tearDown(self):
        self.token.delete_instance()
        self.device.delete_instance()

    def test_list(self):
        headers = { 'AUTHORIZATION': 'token e4763f7165d73e2636cca9e' }
        response = self.client.get('/authorisations', {}, headers)

        self.assertEqual(response.status_code, 200)
        self.assertEqual(json.loads(response.content), [
            {
                'url': '/authorisations/636cca9e',
                'scopes': ['all'],
                'token_last_eight': '636cca9e'
            }
        ])

    def test_create(self):
        headers = { 'AUTHORIZATION': 'token e4763f7165d73e2636cca9e' }
        response = self.client.post('/authorisations', {}, headers)

        self.assertEqual(response.status_code, 201)

        content = json.loads(response.content)
        self.assertTrue('url' in content)
        self.assertTrue('token' in content)
        self.assertTrue('token_last_eight' in content)
        #self.assertEqual(len('token_last_eight'), 8)
        self.assertTrue('scopes' in content)
        self.assertEqual(content['scopes'], ['all'])

        token = Token.select().where(Token.token == content['token']).get()
        token.delete_instance()

    def test_create_with_scope(self):
        headers = { 'AUTHORIZATION': 'token e4763f7165d73e2636cca9e' }
        response = self.client.post('/authorisations', {'scopes': ['push']}, headers)

        self.assertEqual(response.status_code, 201)

        content = json.loads(response.content)
        self.assertTrue('url' in content)
        self.assertTrue('token' in content)
        self.assertTrue('token_last_eight' in content)
        #self.assertEqual(len('token_last_eight'), 8)
        self.assertTrue('scopes' in content)
        #self.assertEqual(content['scopes'], ['push'])

        token = Token.select().where(Token.token == content['token']).get()
        token.delete_instance()

    def test_create_with_token(self):
        headers = { 'AUTHORIZATION': 'token e4763f7165d73e2636cca9e' }
        response = self.client.post('/authorisations', {'token': '4876f9ca0d91362fae6cd4f9cde5d0044295682e'}, headers)

        self.assertEqual(response.status_code, 201)

        content = json.loads(response.content)
        self.assertTrue('url' in content)
        self.assertTrue('token' in content)
        self.assertTrue('token_last_eight' in content)
        #self.assertEqual(len('token_last_eight'), 8)
        self.assertTrue('scopes' in content)
        self.assertEqual(content['token'], '4876f9ca0d91362fae6cd4f9cde5d0044295682e')

        token = Token.select().where(Token.token == content['token']).get()
        token.delete_instance()
Ejemplo n.º 16
0
    def setUp(self):
        self.client = TestClient(router)

        self.device = Device.create(apns_token='ec1752bd70320e4763f7165d73e2636cca9e25cf')
        self.token = Token.create(device=self.device, token='e4763f7165d73e2636cca9e', scope=Token.ALL_SCOPE)
Ejemplo n.º 17
0
class GitHubCallbackTests(unittest.TestCase):
    def setUp(self):
        self.client = TestClient(middleware)

    def test_successfully_authenticates_user_from_github(self):
        response = self.client.get('/callback', {'code': 5})
        self.assertEqual(response.status_code, 200)
        entrant = Entrant.select().where(
            Entrant.email == '*****@*****.**').get()
        self.assertEqual(entrant.name, 'kylef')
        entrant.delete_instance()

    def test_with_invitation(self):
        entrant = Entrant.create(email='*****@*****.**',
                                 name='Kyle',
                                 github_username='******')
        invitation = entrant.invite()
        invitation.save()

        response = self.client.get('/callback', {'code': 5})
        self.assertEqual(response.status_code, 302)
        self.assertEqual(
            response.headers['Location'],
            'https://sotu.cocoapods.org/invitation/{}'.format(invitation.code))

        entrant.delete_instance()
        invitation.delete_instance()

    def test_with_rejected_invitation(self):
        entrant = Entrant.create(email='*****@*****.**',
                                 name='Kyle',
                                 github_username='******')
        invitation = entrant.invite()
        invitation.state = Invitation.REJECTED_STATE
        invitation.save()

        response = self.client.get('/callback', {'code': 5})
        self.assertEqual(response.status_code, 302)
        self.assertEqual(
            response.headers['Location'],
            'https://sotu.cocoapods.org/invitation/{}/reject'.format(
                invitation.code))

        entrant.delete_instance()
        invitation.delete_instance()

    def test_with_accepted_invitation(self):
        entrant = Entrant.create(email='*****@*****.**',
                                 name='Kyle',
                                 github_username='******')
        invitation = entrant.invite()
        invitation.state = Invitation.ACCEPTED_STATE
        invitation.save()

        response = self.client.get('/callback', {'code': 5})
        self.assertEqual(response.status_code, 302)
        self.assertEqual(
            response.headers['Location'],
            'https://sotu.cocoapods.org/invitation/{}/accept'.format(
                invitation.code))

        entrant.delete_instance()
        invitation.delete_instance()
Ejemplo n.º 18
0
 def setUp(self):
     self.client = TestClient(router)
Ejemplo n.º 19
0
 def setUp(self):
     self.client = TestClient(hello_world)