예제 #1
0
    def test_fields_saved(self):
        ap = Permission.get_by(name=u'users-test1').id
        dp = Permission.get_by(name=u'users-test2').id
        gp = Group.get_by(name=u'test-group') or Group.add_iu(
            name=u'test-group',
            approved_permissions=[],
            denied_permissions=[],
            assigned_users=[])
        gp = gp.id
        topost = {
            'login_id': 'usersavedsu',
            'password': '******',
            'email_address': '*****@*****.**',
            'password-confirm': 'testtest',
            'email': '*****@*****.**',
            'user-submit-flag': 'submitted',
            'approved_permissions': ap,
            'denied_permissions': dp,
            'assigned_groups': gp,
            'super_user': 1,
            'inactive_flag': False,
            'inactive_date': '',
            'name_first': '',
            'name_last': ''
        }
        req, r = self.c.post('users/add', data=topost, follow_redirects=True)
        assert r.status_code == 200, r.status
        assert b'User added' in r.data
        assert req.url.endswith('users/manage')

        user = User.get_by_email(u'*****@*****.**')
        assert user.login_id == 'usersavedsu'
        assert user.reset_required
        assert user.super_user
        assert user.pass_hash
        assert user.groups[0].name == 'test-group'
        assert len(user.groups) == 1

        found = 3
        for permrow in user.permission_map:
            if permrow['permission_name'] == u'users-test1':
                assert permrow['resulting_approval']
                found -= 1
            if permrow['permission_name'] in (u'users-test2', u'auth-manage'):
                assert not permrow['resulting_approval']
                found -= 1
        assert found == 0
예제 #2
0
    def test_email_fail(self):
        userlogin = randchars(12)
        topost = {
            'login_id': userlogin,
            'email_address': '*****@*****.**' % userlogin,
            'user-submit-flag': 'submitted',
            'approved_permissions': [],
            'denied_permissions': [],
            'assigned_groups': [],
            'super_user': 1,
            'inactive_flag': False,
            'inactive_date': '10/11/2010',
            'name_first': 'test',
            'name_last': 'user',
            'email_notify': 1
        }

        # cause an email exception
        smtp_orig = smtplib.SMTP
        smtplib.SMTP = None

        req, r = self.c.post('users/add', data=topost, follow_redirects=True)
        assert b'User added successfully' in r.data
        assert b'An error occurred while sending the user notification email.' in r.data
        assert req.url.endswith('users/manage')

        topost['password'] = '******'
        topost['password-confirm'] = 'new_password'
        user = User.get_by_email('*****@*****.**' % userlogin)

        req, r = self.c.post('users/edit/%s' % user.id,
                             data=topost,
                             follow_redirects=True)
        assert b'User edited successfully' in r.data, r.data
        assert b'An error occurred while sending the user notification email.' in r.data
        assert req.url.endswith('users/manage')

        smtplib.SMTP = smtp_orig
예제 #3
0
    def validate_email(self, value):
        dbobj = orm_User.get_by_email(value)
        if (dbobj is None):
            raise ValueInvalid('email address is not associated with a user')

        return value
예제 #4
0
def test_user_get_by_email():
    u = create_user_with_permissions()
    obj = User.get_by_email(u.email_address)
    assert u.id == obj.id
    obj = User.get_by_email((u'%s' % u.email_address).upper())
    assert u.id == obj.id
예제 #5
0
    def test_fields_saved(self):
        ap = Permission.get_by(name=u'users-test1').id
        dp = Permission.get_by(name=u'users-test2').id
        gp = Group.get_by(name=u'test-group') or Group.add_iu(
            name=u'test-group',
            approved_permissions=[],
            denied_permissions=[],
            assigned_users=[])
        gp = gp.id
        topost = {
            'login_id': 'usersaved',
            'email_address': '*****@*****.**',
            'user-submit-flag': 'submitted',
            'approved_permissions': ap,
            'denied_permissions': dp,
            'assigned_groups': gp,
            'super_user': 1,
            'inactive_flag': False,
            'inactive_date': '10/11/2010',
            'name_first': 'test',
            'name_last': 'user',
            'email_notify': 1
        }

        # setup the mock objects so we can test the email getting sent out
        tt = minimock.TraceTracker()
        smtplib.SMTP = minimock.Mock('smtplib.SMTP', tracker=None)
        smtplib.SMTP.mock_returns = minimock.Mock('smtp_connection',
                                                  tracker=tt)

        req, r = self.c.post('users/add', data=topost, follow_redirects=True)
        assert r.status_code == 200, r.status
        assert b'User added' in r.data
        assert req.url.endswith('users/manage')

        mmdump = tt.dump()
        assert 'To: [email protected]' in mmdump
        assert 'You have been added to our system of registered users.' in mmdump
        assert 'user name: usersaved' in mmdump
        assert re.search(r'password: [a-zA-Z0-9]*', mmdump) is not None
        assert re.search(r'password: None', mmdump) is None
        minimock.restore()

        user = User.get_by_email(u'*****@*****.**')
        assert user.login_id == 'usersaved'
        assert user.reset_required
        assert not user.super_user
        assert user.pass_hash
        assert user.groups[0].name == 'test-group'
        assert len(user.groups) == 1
        assert user.inactive_date == datetime.datetime(2010, 10,
                                                       11), user.inactive_date
        assert user.name_first == 'test'
        assert user.name_last == 'user'

        found = 3
        for permrow in user.permission_map:
            if permrow['permission_name'] == u'users-test1':
                assert permrow['resulting_approval']
                found -= 1
            if permrow['permission_name'] in (u'users-test2', u'auth-manage'):
                assert not permrow['resulting_approval']
                found -= 1
        assert found == 0

        # now test an edit
        topost = {
            'login_id': 'usersaved',
            'email_address': '*****@*****.**',
            'user-submit-flag': 'submitted',
            'approved_permissions': dp,
            'denied_permissions': ap,
            'assigned_groups': None,
            'super_user': 1,
            'inactive_flag': False,
            'inactive_date': '10/10/2010',
            'name_first': 'test2',
            'name_last': 'user2',
            'email_notify': 1,
            'password': '******',
            'password-confirm': 'test_new_password'
        }

        # setup the mock objects so we can test the email getting sent out
        tt = minimock.TraceTracker()
        smtplib.SMTP = minimock.Mock('smtplib.SMTP', tracker=None)
        smtplib.SMTP.mock_returns = minimock.Mock('smtp_connection',
                                                  tracker=tt)

        req, r = self.c.post('users/edit/%s' % user.id,
                             data=topost,
                             follow_redirects=True)
        assert b'User edited successfully' in r.data
        assert req.url.endswith('users/manage')

        assert tt.check(
            'Called smtp_connection.sendmail([email protected] '
            'password for this site has been reset'
            '...first successful login...')
        # restore the mocked objects
        minimock.restore()

        mmdump = tt.dump()
        assert 'To: [email protected]' in mmdump
        assert 'Your password for this site has been reset by an administrator.' in mmdump
        assert 'user name: usersaved' in mmdump
        assert re.search(r'password: [a-zA-Z0-9]*', mmdump) is not None
        assert re.search(r'password: None', mmdump) is None
        minimock.restore()

        db.sess.expire(user)
        assert user.login_id == 'usersaved'
        assert user.reset_required
        assert not user.super_user
        assert user.pass_hash
        assert len(user.groups) == 0
        assert user.inactive_date == datetime.datetime(2010, 10,
                                                       10), user.inactive_date
        assert user.name_first == 'test2'
        assert user.name_last == 'user2'

        found = 3
        for permrow in user.permission_map:
            if permrow['permission_name'] == u'users-test2':
                assert permrow['resulting_approval']
                found -= 1
            if permrow['permission_name'] in (u'users-test1', u'auth-manage'):
                assert not permrow['resulting_approval']
                found -= 1
        assert found == 0

        # test edit w/ reset required and email notify (no email sent)
        topost = {
            'login_id': 'usersaved',
            'email_address': '*****@*****.**',
            'user-submit-flag': 'submitted',
            'approved_permissions': dp,
            'denied_permissions': ap,
            'assigned_groups': None,
            'super_user': 1,
            'reset_required': 1,
            'inactive_flag': False,
            'inactive_date': '',
            'name_first': '',
            'name_last': '',
            'email_notify': 1
        }
        req, r = self.c.post('users/edit/%s' % user.id,
                             data=topost,
                             follow_redirects=True)
        assert b'User edited successfully' in r.data
        assert req.url.endswith('users/manage')

        db.sess.expire(user)
        assert user.login_id == 'usersaved'
        assert user.reset_required
        assert not user.super_user
        assert user.pass_hash
        assert len(user.groups) == 0

        # now test a delete
        req, r = self.c.get('users/delete/%s' % user.id, follow_redirects=True)
        assert r.status_code == 200, r.status
        assert b'User deleted' in r.data
        assert req.url.endswith('users/manage')