Пример #1
0
    def test_failed_validation(self):
        from colander import Invalid
        from pyramid.httpexceptions import HTTPInternalServerError
        import json
        e = Mock(Invalid)
        e.asdict.return_value = {
            'query': 'Bad query field',
            'format': 'Something wrong in form'
        }
        request = testing.DummyRequest()
        request.exception = e
        # use alternate view callable argument convention
        # because exception is passed as context
        views = Views(request)

        response = views.failed_validation()

        self.assertIsInstance(response, HTTPInternalServerError)
        expected = {
            'success': False,
            'errors': {
                'query': 'Bad query field',
                'format': 'Something wrong in form'
            }
        }
        self.assertEqual(json.loads(response.body), expected)
Пример #2
0
    def test_access_token(self, time):
        time.return_value = 1355000000.000000
        request = testing.DummyRequest()
        request.user = User('bob', 'Bob Example', '*****@*****.**')

        # mock auth
        from pyramid.authorization import ACLAuthorizationPolicy
        from pyramid_macauth import MACAuthenticationPolicy
        from pyramid_multiauth import MultiAuthenticationPolicy
        self.config.set_authorization_policy(ACLAuthorizationPolicy())
        policy = Mock(MACAuthenticationPolicy)
        policy.encode_mac_id.return_value = 'id', 'key'
        mpolicy = MultiAuthenticationPolicy([policy])
        self.config.set_authentication_policy(mpolicy)

        views = Views(request)

        response = views.access_token()

        expected_response = {
            "acesss_token": 'id',
            "mac_key": 'key',
            "expires_in": 360,
            "token_type": "mac",
            "mac_algorithm": "hmac-sha-1"
        }
        self.assertDictEqual(response, expected_response)
        self.assertEqual(request.response.headers['Cache-Control'], "no-store")
        policy.encode_mac_id.assert_called_with(request,
                                                'bob',
                                                expires=1355000360)
Пример #3
0
    def test_allinone_no_jobmanager(self):
        from magmaweb.job import JobSubmissionError
        from pyramid.httpexceptions import HTTPInternalServerError
        import json
        post = {'ms_data': 'somexml', 'ms_data_file': ''}
        request = testing.DummyRequest(post=post)
        request.user = User('bob', 'Bob Example', '*****@*****.**')
        job = self.fake_job()
        jobquery = Mock(JobQuery)
        job.jobquery.return_value = jobquery
        views = Views(request)
        views.job_factory = Mock(JobFactory)
        views.job_factory.fromScratch = Mock(return_value=job)
        q = Mock(side_effect=JobSubmissionError())
        views.job_factory.submitQuery = q

        with self.assertRaises(HTTPInternalServerError) as e:
            views.allinone()

        expected_json = {'success': False, 'msg': 'Unable to submit query'}
        self.assertEquals(json.loads(e.exception.body), expected_json)
        views.job_factory.fromScratch.assert_called_with('bob')
        jobquery.allinone.assert_called_with(post)
        views.job_factory.submitQuery.assert_called_with(
            jobquery.allinone(), job)
Пример #4
0
    def test_login_post_incorrect_pw(self, user, remember):
        u = Mock(User)
        u.validate_password.return_value = False
        user.by_id.return_value = u
        self.config.add_route('login', '/login')
        self.config.add_route('workspace', '/workspace')
        post = {'userid': 'bob', 'password': '******'}
        params = {'came_from': 'http://example.com/workspace'}
        request = testing.DummyRequest(post=post, params=params)
        request.user = None
        route_mapper = self.config.get_routes_mapper()
        request.matched_route = route_mapper.get_route('workspace')

        views = Views(request)

        response = views.login()

        expected_response = {
            'came_from': 'http://example.com/workspace',
            'userid': 'bob',
            'password': '******'
        }
        self.assertDictEqual(response, expected_response)
        u.validate_password.assert_called_with('mypw')
        self.assertFalse(remember.called)
Пример #5
0
    def test_examplejson(self, jq):
        request = testing.DummyRequest(params={'selection': 'example'})
        views = Views(request)
        jq.defaults.return_value = 'foo'

        response = views.defaults()

        jq.defaults.assert_called_with('example')
        self.assertDictEqual(response, {'success': True, 'data': 'foo'})
Пример #6
0
    def test_defaultsjson(self, jq):
        request = testing.DummyRequest()
        views = Views(request)
        jq.defaults.return_value = 'foo'

        response = views.defaults()

        jq.defaults.assert_called_with(None)
        self.assertDictEqual(response, {'success': True, 'data': 'foo'})
Пример #7
0
    def test_login_authenticated(self):
        from pyramid.exceptions import Forbidden
        request = testing.DummyRequest()
        request.user = '******'
        request.exception = Forbidden()
        views = Views(request)

        response = views.login()

        self.assertIsInstance(response, Forbidden)
Пример #8
0
    def test_logout(self, forget):
        self.config.add_route('home', '/')
        forget.return_value = {'forget': 'headers'}
        request = testing.DummyRequest()
        views = Views(request)

        response = views.logout()

        forget.assert_called_with(request)
        self.assertEqual(response.location, 'http://example.com/')
        self.assertDictContainsSubset({'forget': 'headers'}, response.headers)
Пример #9
0
    def test_jobfromscratch(self):
        self.config.add_route('results', '/results/{jobid}')
        request = testing.DummyRequest()
        request.user = User('bob', 'Bob Example', '*****@*****.**')
        job = self.fake_job()
        views = Views(request)
        views.job_factory.fromScratch = Mock(return_value=job)

        response = views.jobfromscratch()

        views.job_factory.fromScratch.assert_called_with('bob')
        self.assertEqual(response.location, 'http://example.com/results/foo')
Пример #10
0
    def test_allinone_restricted(self):
        self.config.add_settings(restricted=True)
        post = {'ms_data': 'somexml', 'ms_data_file': ''}
        request = testing.DummyRequest(post=post)
        request.user = User('bob', 'Bob Example', '*****@*****.**')
        job = self.fake_job()
        jobquery = Mock(JobQuery)
        job.jobquery.return_value = jobquery
        views = Views(request)
        views.job_factory = Mock(JobFactory)
        views.job_factory.fromScratch = Mock(return_value=job)

        views.allinone()

        job.jobquery.assert_called_with('http://example.com/status/foo.json',
                                        True, 1)
Пример #11
0
    def test_uploaddb_post(self):
        self.config.add_route('results', '/results/{jobid}')
        from cgi import FieldStorage
        dbfile = FieldStorage()
        dbfile.file = StringIO()
        request = testing.DummyRequest(post={'db_file': dbfile})
        request.user = User('bob', 'Bob Example', '*****@*****.**')
        views = Views(request)
        views.job_factory = Mock(JobFactory)
        job = self.fake_job()
        views.job_factory.fromDb.return_value = job

        response = views.uploaddb()

        views.job_factory.fromDb.assert_called_with(dbfile.file, 'bob')
        self.assertEqual(response.location, 'http://example.com/results/foo')
Пример #12
0
    def test_login_auto_register_from_login(self, user, remember):
        user.generate.return_value = User('bob', 'bob dob',
                                          '*****@*****.**')
        from pyramid.httpexceptions import HTTPFound
        self.config.add_route('home', '/')
        self.config.add_route('login', '/login')
        request = testing.DummyRequest()
        request.user = None
        request.url = 'http://example.com/login'
        route_mapper = self.config.get_routes_mapper()
        request.matched_route = route_mapper.get_route('login')
        request.registry.settings['auto_register'] = True
        views = Views(request)

        response = views.login()

        self.assertIsInstance(response, HTTPFound)
        self.assertEqual(response.location, 'http://example.com/')
        user.generate.assert_called_with()
        remember.assert_called_with(request, 'bob')
Пример #13
0
    def test_login_get_from_loginpage(self):
        self.config.add_route('home', '/')
        self.config.add_route('login', '/login')
        request = testing.DummyRequest()
        request.user = None
        request.url = 'http://example.com/login'
        route_mapper = self.config.get_routes_mapper()
        request.matched_route = route_mapper.get_route('login')
        views = Views(request)

        response = views.login()

        expected_response = {
            'came_from': 'http://example.com/',
            'userid': '',
            'password': ''
        }
        self.assertDictEqual(response, expected_response)
        self.assertEqual(request.response.headers['WWW-Authenticate'], 'MAC')
        self.assertEqual(request.response.status_int, 401)
Пример #14
0
    def test_allinone_with_ms_data_as_text(self):
        post = {'ms_data': 'somexml', 'ms_data_file': ''}
        request = testing.DummyRequest(post=post)
        request.user = User('bob', 'Bob Example', '*****@*****.**')
        job = self.fake_job()
        jobquery = Mock(JobQuery)
        job.jobquery.return_value = jobquery
        views = Views(request)
        views.job_factory = Mock(JobFactory)
        views.job_factory.fromScratch = Mock(return_value=job)

        response = views.allinone()

        views.job_factory.fromScratch.assert_called_with('bob')
        jobquery.allinone.assert_called_with(post)
        views.job_factory.submitQuery.assert_called_with(
            jobquery.allinone(), job)
        self.assertEqual(response, {'success': True, 'jobid': 'foo'})
        self.assertEqual(job.ms_filename, 'Uploaded as text')
        job.jobquery.assert_called_with('http://example.com/status/foo.json',
                                        False, 1)
Пример #15
0
    def test_login_post_correct_pw(self, user, remember):
        from pyramid.httpexceptions import HTTPFound
        u = Mock(User)
        u.validate_password.return_value = True
        user.by_id.return_value = u
        self.config.add_route('login', '/login')
        self.config.add_route('workspace', '/workspace')
        post = {'userid': 'bob', 'password': '******'}
        params = {'came_from': 'http://example.com/workspace'}
        request = testing.DummyRequest(post=post, params=params)
        request.user = None
        route_mapper = self.config.get_routes_mapper()
        request.matched_route = route_mapper.get_route('workspace')
        views = Views(request)

        response = views.login()

        self.assertIsInstance(response, HTTPFound)
        self.assertEqual(response.location, 'http://example.com/workspace')
        u.validate_password.assert_called_with('mypw')
        remember.assert_called_with(request, 'bob')
Пример #16
0
    def test_login_auto_register_mac_challenge(self):
        self.config.add_route('status.json', '/status')
        self.config.add_route('home', '/')
        self.config.add_route('login', '/login')
        request = testing.DummyRequest()
        request.user = None
        request.url = 'http://example.com/status'
        request.method = u'PUT'
        route_mapper = self.config.get_routes_mapper()
        request.matched_route = route_mapper.get_route('status.json')
        request.registry.settings['auto_register'] = True
        views = Views(request)

        response = views.login()

        expected_response = {
            'came_from': 'http://example.com/status',
            'userid': '',
            'password': ''
        }
        self.assertDictEqual(response, expected_response)
        self.assertEqual(request.response.headers['WWW-Authenticate'], 'MAC')
        self.assertEqual(request.response.status_int, 401)
Пример #17
0
    def test_allinone_with_ms_data_as_file(self):
        from cgi import FieldStorage
        ms_file = FieldStorage()
        ms_file.filename = r'c:\bla\bla\F1234.mzxml'
        post = {'ms_data_file': ms_file}
        request = testing.DummyRequest(post=post)
        request.user = User('bob', 'Bob Example', '*****@*****.**')
        job = self.fake_job()
        jobquery = Mock(JobQuery)
        job.jobquery.return_value = jobquery
        views = Views(request)
        views.job_factory = Mock(JobFactory)
        views.job_factory.fromScratch = Mock(return_value=job)

        response = views.allinone()

        views.job_factory.fromScratch.assert_called_with('bob')
        jobquery.allinone.assert_called_with(post)
        views.job_factory.submitQuery.assert_called_with(
            jobquery.allinone(), job)
        self.assertEqual(response, {'success': True, 'jobid': 'foo'})
        self.assertEqual(job.ms_filename, r'c:\bla\bla\F1234.mzxml')
        job.jobquery.assert_called_with('http://example.com/status/foo.json',
                                        False, 1)
Пример #18
0
    def test_workspace(self):
        import uuid
        self.config.add_route('results', '/results/{jobid}')
        request = testing.DummyRequest()
        request.user = User('bob', 'Bob Example', '*****@*****.**')
        created_at = datetime.datetime(2012, 11, 14, 10, 48, 26, 504478)
        jobs = [
            JobMeta(
                uuid.UUID('11111111-1111-1111-1111-111111111111'),
                'bob',
                description='My job',
                created_at=created_at,
                ms_filename='F1234.mzxml',
                state='STOPPED',
                is_public=False,
            )
        ]
        request.user.jobs = jobs
        views = Views(request)
        views.job_factory.dbSize = Mock(return_value=1234)

        response = views.workspace()

        id1 = '11111111-1111-1111-1111-111111111111'
        url1 = 'http://example.com/results/' + id1
        expected_jobs = [{
            'id': '11111111-1111-1111-1111-111111111111',
            'url': url1,
            'description': 'My job',
            'is_public': False,
            'ms_filename': 'F1234.mzxml',
            'state': 'STOPPED',
            'created_at': '2012-11-14T10:48:26',
            'size': 1234,
        }]
        self.assertEqual(response, {'jobs': expected_jobs})
Пример #19
0
    def test_help(self):
        request = testing.DummyRequest()
        views = Views(request)
        response = views.help()

        self.assertEqual(response, {})
Пример #20
0
    def test_uploaddb_get(self):
        request = testing.DummyRequest()
        views = Views(request)

        response = views.uploaddb()
        self.assertEqual(response, {})