Пример #1
0
    def test_no_email_notify(self):
        topost = {
            'login_id': 'usersaved_noemailnotify',
            'email_address': '*****@*****.**',
            'user-submit-flag': 'submitted',
            'approved_permissions': None,
            'denied_permissions': None,
            'assigned_groups': None,
            'super_user': 1,
            'inactive_flag': False,
            'inactive_date': '10/11/2010',
            'name_first': 'test',
            'name_last': 'user'
        }

        # 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')

        assert len(tt.dump()) == 0
        minimock.restore()
Пример #2
0
def test_gpsdshm_Shm_error():
    gpsdshm.shm.shm_get = minimock.Mock('gpsdshm.shm.shm_get')
    gpsdshm.shm.shm_get.mock_returns = None
    try:
        gpsdshm.Shm()
    except OSError:
        minimock.restore()
        return
    raise Exception('gpsdshm.shm.shm_get did nto raise OSError')
Пример #3
0
 def test_select_2_editions_with_same_content_selected_by_identity(self):
     # We once hit a bad issue where we used a comparison in
     # `select_edition` that was based on identity. However, due to the
     # fact that we have a special implementation of __eq__ we need to
     # ensure that we select based on identity rather than equality.
     # This test isn't bullet proof, but it keeps us from making the
     # exactly same mistake again.
     page = minimock.Mock('page')
     ed1 = asm.cms.edition.Edition()
     ed2 = asm.cms.edition.Edition()
     page.editions = [ed1, ed2]
     selector = minimock.Mock('selector', tracker=None)
     selector.select.mock_returns = ([ed1], [])
     minimock.mock('zope.component.subscribers',
                   returns=[selector],
                   tracker=self.tracker)
     self.assertTrue(
         ed1 is asm.cms.edition.select_edition(page, DummyRequest()))
     selector.select.mock_returns = ([ed2], [])
     self.assertTrue(
         ed2 is asm.cms.edition.select_edition(page, DummyRequest()))
Пример #4
0
 def testForecast(self):
     hours = range(12)
     wobj = minimock.Mock('obj')
     wobj.general = "sunny"
     wobj.temp = "27"
     minimock.mock("executor.meteo.get_current_weather", returns=wobj)
     minimock.mock("executor.meteo.get_forecast", returns=wobj)
     returns = []
     for h in hours:
         returns.append(executor.forecast(h))
     self.assertIn(
         "The weather in 3 hours will be sunny with a temperature of 27*. ",
         returns)
Пример #5
0
 def _check_make_server(self, backend):
     mocked_backend = minimock.Mock('Backend', returns='backend_impl', tracker=self.tt)
     minimock.mock('chaussette.server.get', returns=mocked_backend, tracker=self.tt)
     server = chaussette.server.make_server('app', 'host', 'port', backend)
     minimock.assert_same_trace(self.tt, '\n'.join([
         "Called chaussette.server.get('%s')" % backend,
         "Called Backend(",
         "    ('host', 'port'),",
         "   'app',",
         "   address_family=2,",
         "   backlog=2048,",
         "socket_type=1)"
     ]))
     self.assertEqual(server, 'backend_impl')
Пример #6
0
def test_nose_component_app_package_by_environ():
    import minimock
    import tempfile
    from minimal2.config.settings import Default
    new_script_test_path = tempfile.mkdtemp()
    Default.get_storage_dir = minimock.Mock('get_storage_dir')
    Default.get_storage_dir.mock_returns = os.path.join(
        new_script_test_path, 'minimal2')

    base_environ['BLAZEWEB_APP_PACKAGE'] = 'minimal2'
    newenv = BWTestFileEnvironment(new_script_test_path, environ=base_environ)
    res = newenv.run('nosetests', 'minimal2', expect_error=True, cwd=apps_path)
    assert 'Ran 1 test in' in res.stderr, res.stderr
    assert 'OK' in res.stderr, res.stderr
    minimock.restore()
Пример #7
0
 def testShowCommands(self):
     mockom = minimock.Mock('obj')
     mockom.order = 'show'
     mockom.schedule = '2016-12-12 12:12:12'
     mockom.args = 'something'
     lmock = [mockom]
     for m in [lmock, 2 * lmock, 5 * lmock]:
         minimock.mock("executor.Todo.read_next_commands", returns=m)
         msg = 'The following commands will be executed:'
         for i in range(len(m)):
             msg = msg + '\nshow something on 2016-12-12 12:12:12'
         self.assertEqual(executor.show('commands'), msg)
         self.assertEqual(executor.show('commands 3'), msg)
         self.assertEqual(executor.show('commands 5'), msg)
     for p in ['commands today', 'commands commands', 'commands 100%%']:
         msg = 'Invalid parameter for commands. Will return the next %s commands.\n' % executor.defaults[
             'commands']
         msg += 'The following commands will be executed:'
         for i in range(executor.defaults['commands']):
             msg = msg + '\nshow something on 2016-12-12 12:12:12'
         minimock.mock("executor.Todo.read_next_commands",
                       returns=3 * lmock)
         self.assertEqual(executor.show(p), msg)
Пример #8
0
 def setUp(self):
     self.mockopener = minimock.Mock(self.odName)
     request.build_opener = minimock.Mock(self.boName)
     request.build_opener.mock_returns = self.mockopener
 def setUp(self):
     datetime.datetime = minimock.Mock(
         "datetime.datetime",
         now=lambda: 10,
     )
Пример #10
0
 def setUp(self):
     self.mockopener = minimock.Mock('urllib2.OpenerDirector')
     urllib2.build_opener = minimock.Mock('urllib2.build_opener')
     urllib2.build_opener.mock_returns = self.mockopener
Пример #11
0
    def test_password_reset(self):
        """ has to be done in the same test function so that order is assured"""

        user = create_user_with_permissions()

        r = self.c.get('users/recover-password')
        assert r.status_code == 200, r.status
        assert b'Recover Password' in r.data

        # 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)

        # test posting to the restore password view
        db.sess.expire(user)
        topost = {
            'email_address': user.email_address,
            'lost-password-form-submit-flag': 'submitted',
        }
        req, r = self.c.post('users/recover-password',
                             data=topost,
                             follow_redirects=True)
        assert r.status_code == 200, r.status
        assert b'email with a link to reset your password has been sent' in r.data, r.data
        assert req.url == 'http://localhost/'

        # test the mock strings (i.e. test the email that was sent out)
        db.sess.expire(user)
        assert tt.check('Called smtp_connection.sendmail(...%s...has been issu'
                        'ed to reset the password...' % user.email_address)
        # restore the mocked objects
        minimock.restore()

        # now test resetting the password
        r = self.c.get('/users/reset-password/%s/%s' %
                       (user.login_id, user.pass_reset_key))
        assert r.status_code == 200, r.status_code
        assert b'Reset Password' in r.data
        assert b'Please choose a new password to complete the reset request' in r.data

        # expire the date
        db.sess.expire(user)
        orig_reset_ts = user.pass_reset_ts
        user.pass_reset_ts = datetime.datetime(2000, 10, 10)
        db.sess.commit()

        # check expired message
        req, resp = self.c.get('/users/reset-password/%s/%s' %
                               (user.login_id, user.pass_reset_key),
                               follow_redirects=True)
        assert resp.status_code == 200, resp.status
        assert b'Recover Password' in resp.data
        assert b'password reset link expired, use the form below to resend reset link' in resp.data
        assert req.url.endswith('users/recover-password')

        # unexpire the date
        db.sess.expire(user)
        user.pass_reset_ts = orig_reset_ts
        db.sess.commit()

        # check posting the new passwords
        topost = {
            'password': '******',
            'password-confirm': 'TestPassword2',
            'new-password-form-submit-flag': 'submitted',
        }
        req, r = self.c.post('/users/reset-password/%s/%s' %
                             (user.login_id, user.pass_reset_key),
                             data=topost,
                             follow_redirects=True)
        assert r.status_code == 200, r.status
        assert b'Your password has been reset successfully' in r.data
        assert req.url == 'http://localhost/'
Пример #12
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')