Esempio n. 1
0
    def test_add_invalid(self):
        # test an invalid resource (that is, with a slash)
        username = '******'
        with capture() as (stdout, stderr):
            try:
                restauth_user(['add', '--password', password1, username])
                self.fail('restauth-user allows invalid characters')
            except SystemExit as e:
                self.assertEqual(e.code, 2)
                self.assertEqual(stdout.getvalue(), '')
                self.assertTrue(stderr.getvalue().startswith('usage: '))

        self.assertItemsEqual(user_backend.list(), [])
        self.assertRaises(UserNotFound, user_backend.check_password, username, password1)

        # load a custom validator:
        load_username_validators(('Users.validators.MediaWikiValidator', ))
        username = '******'
        with capture() as (stdout, stderr):
            try:
                restauth_user(['add', '--password', password1, username])
                self.fail('restauth-user allows invalid characters')
            except SystemExit as e:
                self.assertEqual(e.code, 2)
                self.assertEqual(stdout.getvalue(), '')
                self.assertTrue(stderr.getvalue().startswith('usage: '))
        self.assertItemsEqual(user_backend.list(), [])
        self.assertRaises(UserNotFound, user_backend.check_password, username, password1)

        load_username_validators()
Esempio n. 2
0
    def test_dry_run_create_existing_user(self):
        user = self.create_user(username=username1)
        self.assertItemsEqual([user.username], list(user_backend.list()))

        resp = self.post('/test/users/', {'user': username1})
        self.assertEqual(resp.status_code, http_client.CONFLICT)
        self.assertItemsEqual([user.username], list(user_backend.list()))
Esempio n. 3
0
    def test_existing_properties(self):
        user = user_backend.create(username2, property_backend=property_backend)
        property_backend.create(user, propkey1, propval3)  # propval1 is in json file

        path = os.path.join(self.base, 'users1.json')
        with capture() as (stdout, stderr):
            cmd = [path]
            restauth_import(cmd)
            self.assertItemsEqual(user_backend.list(), [username1, username2, username3])
            user = user_backend.get(username2)

            pattern = '^%s: Property "%s" already exists\.$' % (username2, propkey1)
            self.assertHasLine(stdout, pattern)
            self.assertHasLine(stdout, '^%s: Property "date joined" already exists\.$' % username2)

            expected_props = {
                propkey1: propval3,  # propva1 is in json-file - we don't overwrite!
                propkey2: propval2,
                u'last login': u'2013-12-01 19:27:44',  # date from json file
            }

            props = property_backend.list(user)
            # delete 'date joined' prop because it was created by the user_backend and
            # restauth-import doesn't overwrite in this invocation:
            del props['date joined']

            self.assertEqual(props, expected_props)
Esempio n. 4
0
 def test_wrong_content_type_header(self):
     content = self.handler.marshal_dict({'user': username1})
     extra = self.extra
     del extra['content_type']
     resp = self.c.post('/users/', content, content_type='foo/bar', **extra)
     self.assertEqual(resp.status_code, http_client.UNSUPPORTED_MEDIA_TYPE)
     self.assertItemsEqual(user_backend.list(), [])
Esempio n. 5
0
 def test_dry_run_create_user_with_pass(self):
     resp = self.post('/test/users/', {
         'user': username1,
         'password': password1
     })
     self.assertEqual(resp.status_code, http_client.CREATED)
     self.assertEqual(list(user_backend.list()), [])
Esempio n. 6
0
 def test_rm(self):
     self.create_user(username1, password1)
     frm = username1 if six.PY3 else username1.encode('utf-8')
     with capture() as (stdout, stderr):
         restauth_user(['rm', frm])
         self.assertEqual(stdout.getvalue(), '')
         self.assertEqual(stderr.getvalue(), '')
     self.assertEqual(user_backend.list(), [])
Esempio n. 7
0
 def test_dry_run_create_user_with_props(self):
     resp = self.post('/test/users/', {
         'user': username1,
         'properties': {
             'foo': 'bar'
         }
     })
     self.assertEqual(resp.status_code, http_client.CREATED)
     self.assertEqual(list(user_backend.list()), [])
Esempio n. 8
0
    def test_add(self):
        with capture() as (stdout, stderr):
            restauth_user(['add', '--password', password1,
                           username1 if six.PY3 else username1.encode('utf-8')])
            self.assertEqual(stdout.getvalue(), '')
            self.assertEqual(stderr.getvalue(), '')
        self.assertItemsEqual(user_backend.list(), [username1])
        self.assertTrue(user_backend.check_password(username1, password1))
        self.assertFalse(user_backend.check_password(username1, password2))

        # create anotheruser with a generated password:
        gen_password = None
        with capture() as (stdout, stderr):
            restauth_user(['add', '--gen-password',
                           username2 if six.PY3 else username2.encode('utf-8')])
            gen_password = stdout.getvalue().strip()
            self.assertEqual(stderr.getvalue(), '')

        self.assertItemsEqual(user_backend.list(), [username1, username2])
        self.assertTrue(user_backend.check_password(username2, gen_password))
Esempio n. 9
0
    def test_users_django_hash(self, overwrite=False):
        path = os.path.join(self.base, 'users5.json')
        with capture() as (stdout, stderr):
            cmd = [path]
            if overwrite:
                cmd = ['--overwrite-properties', path]

            with override_settings(PASSWORD_HASHERS=PASSWORD_HASHERS):
                restauth_import(cmd)
                self.assertHasLine(stdout, '^\* %s: Set hash from input data\.$' % username3)
                self.assertTrue(user_backend.check_password(username3, 'foobar'))
                self.assertItemsEqual(user_backend.list(), [username3])
Esempio n. 10
0
    def test_users(self, overwrite=False):
        path = os.path.join(self.base, 'users1.json')
        with capture() as (stdout, stderr):
            cmd = [path]
            if overwrite:
                cmd = ['--overwrite-properties', path]

            with override_settings(PASSWORD_HASHERS=PASSWORD_HASHERS):
                restauth_import(cmd)
                self.assertHasLine(stdout, '^\* %s: Set hash from input data\.$' % username3)
                self.assertTrue(user_backend.check_password(username3, 'foobar'))

            self.assertItemsEqual(user_backend.list(), [username1, username2, username3])
            user = user_backend.get(username2)
            props = {
                propkey1: propval1,
                propkey2: propval2,
                # timestamps of when we wrote this test:
#                u'date joined': u'2013-12-01 19:27:50',
                u'last login': u'2013-12-01 19:27:44',
            }

            self.assertProperties(user, props)
Esempio n. 11
0
 def test_wrong_content(self):
     content = 'no_json_at_all}}}'
     resp = self.c.post('/users/', content, **self.extra)
     self.assertEqual(resp.status_code, http_client.BAD_REQUEST)
     self.assertItemsEqual(user_backend.list(), [])
Esempio n. 12
0
 def test_wrong_accept_header(self):
     extra = self.extra
     extra['HTTP_ACCEPT'] = 'foo/bar'
     resp = self.c.get('/users/', **extra)
     self.assertEqual(resp.status_code, http_client.NOT_ACCEPTABLE)
     self.assertItemsEqual(user_backend.list(), [])
Esempio n. 13
0
 def get_usernames(self):
     return user_backend.list()
Esempio n. 14
0
 def test_create_with_too_short_pass(self):
     resp = self.post('/test/users/', {'user': username1, 'password': '******'})
     self.assertEqual(resp.status_code, http_client.PRECONDITION_FAILED)
     self.assertEqual(list(user_backend.list()), [])
Esempio n. 15
0
 def test_dry_run_create_short_user(self):
     resp = self.post('/test/users/', {'user': '******'})
     self.assertEqual(resp.status_code, http_client.PRECONDITION_FAILED)
     self.assertEqual(list(user_backend.list()), [])