Esempio n. 1
0
    def test_authenticate_user_not_valid_for_this_app(self):
        self.db.query(User).delete()

        UserFactory(email='*****@*****.**')

        mock_response = Mock(
            code=200,
            body='{"issued_to": "222", "email": "*****@*****.**"}'
        )

        def handle_request(url, handler, proxy_host, proxy_port):
            handler(mock_response)

        fetch_mock = Mock()
        fetch_mock.side_effect = handle_request

        config = Config()
        config.GOOGLE_CLIENT_ID = '000'

        access_token = '111'

        User.authenticate(
            access_token,
            fetch_mock,
            self.db,
            config,
            callback=self.stop
        )

        response = self.wait()

        expect(response.get('status')).to_equal(401)
        expect(response.get('reason')).to_equal(
            "Token's client ID does not match app's."
        )
Esempio n. 2
0
    def test_authenticate_unauthorized_user(self):
        self.db.query(User).delete()

        mock_response = Mock(
            code=200,
            body='{"issued_to": "000", "email": "*****@*****.**"}'
        )

        def handle_request(url, handler, proxy_host, proxy_port):
            handler(mock_response)

        fetch_mock = Mock()
        fetch_mock.side_effect = handle_request

        config = Config()
        config.GOOGLE_CLIENT_ID = '000'

        access_token = '111'

        User.authenticate(
            access_token,
            fetch_mock,
            self.db,
            config,
            callback=self.stop
        )

        response = self.wait()

        expect(response.get('status')).to_equal(403)
        expect(response.get('reason')).to_equal('Unauthorized user')
Esempio n. 3
0
    def post(self):
        access_token = self.request.headers.get('X-AUTH-HOLMES', None)

        if access_token is None:
            self.set_status(403)
            self.write_json({'reason': 'Empty access token'})
            return

        result = yield User.authenticate(
            access_token,
            self.application.http_client.fetch,
            self.db,
            self.application.config
        )

        if result and result.get('user', None) is None:
            self.set_status(403)
            self.write_json({'reason': 'Not authorized user.'})
            return

        post_data = loads(self.request.body)
        url = post_data.get('url', None)
        connections = self.application.config.DEFAULT_NUMBER_OF_CONCURRENT_CONNECTIONS
        value = post_data.get('value', connections)

        if not url and not value:
            self.set_status(400)
            self.write_json({'reason': 'Not url or value'})
            return

        result = Limiter.add_or_update_limiter(self.db, url, value)

        yield self.cache.remove_domain_limiters_key()

        self.write_json(result)
Esempio n. 4
0
    def test_authenticate(self, datetime_mock):
        dt = datetime(2014, 2, 14, 15, 0, 30)
        datetime_mock.now.return_value = dt

        self.db.query(User).delete()

        UserFactory(email='*****@*****.**')

        mock_response = Mock(
            code=200,
            body='{"issued_to": "000", "email": "*****@*****.**"}'
        )

        def handle_request(url, handler, proxy_host, proxy_port):
            handler(mock_response)

        fetch_mock = Mock()
        fetch_mock.side_effect = handle_request

        config = Config()
        config.GOOGLE_CLIENT_ID = '000'

        access_token = '111'

        User.authenticate(
            access_token,
            fetch_mock,
            self.db,
            config,
            callback=self.stop
        )

        response = self.wait()

        expect(response).to_be_like({
            'status': 200,
            'user': {
                'is_superuser': True,
                'fullname': u'Marcelo Jorge Vieira',
                'last_login': dt,
                'email': u'*****@*****.**'
            }
        })

        loaded_user = User.by_email('*****@*****.**', self.db)
        expect(loaded_user.last_login).to_equal(dt)
Esempio n. 5
0
    def test_authenticate_invalid_token(self):
        self.db.query(User).delete()

        UserFactory(email='*****@*****.**')

        mock_response = Mock(
            code=400,
            body=dumps({
                "error": "invalid_token",
                "error_description": "Invalid Value"
            })
        )

        def handle_request(url, handler, proxy_host, proxy_port):
            handler(mock_response)

        fetch_mock = Mock()
        fetch_mock.side_effect = handle_request

        config = Config()
        config.GOOGLE_CLIENT_ID = '000'

        access_token = '111'

        User.authenticate(
            access_token,
            fetch_mock,
            self.db,
            config,
            callback=self.stop
        )

        response = self.wait()

        expect(response).to_be_like({
            'status': 400,
            'reason': 'Error',
            'details': '{"error_description":"Invalid Value", \
                         "error":"invalid_token"}'
        })