def test_unique_constraint(self): Session.add(UserProperty(key='foo', user_id=1)) Session.flush() with self.assertRaises(IntegrityError) as cm: Session.add(UserProperty(key='foo', user_id=1)) Session.flush() self.assertIn('key, user_id are not unique', cm.exception.message)
def test_skip_non_valid_to_properties(self, mocked_date, User): mocked_date.today.return_value = date(2013, 12, 30) user = self._make_user(valid_to=date(2013, 12, 31)) user.properties = [ UserProperty(key='foo', value='bar'), UserProperty(key='addon_1_last_payment', value='baz'), ] User.get_all.return_value = [ user, ] expire_subscriptions() self.assertEqual(user.groups.remove.call_count, 0)
def submit_success(self, appstruct): user = User( email=appstruct.get('email'), fullname=appstruct.get('fullname'), affiliate=appstruct.get('affiliate'), billing_email=appstruct.get('billing_email'), valid_to=appstruct.get('valid_to'), last_payment=appstruct.get('last_payment'), groups=[Group.by_id(group_id) for group_id in appstruct.get('groups', [])], # noqa properties=[UserProperty(key=prop.get('key'), value=prop.get('value')) # noqa for prop in appstruct.get('properties', [])], ) if appstruct.get('password'): # pragma: no branch user.password = encrypt(appstruct['password']) Session.add(user) Session.flush() self.request.registry.notify( UserCreated( self.request, user, appstruct.get('password'), u'Created manually by {}'.format(self.request.user.email) ) ) self.request.session.flash(u'User "{}" added.'.format(user.email)) return HTTPFound( location=self.request.route_path('user_view', user_id=user.id))
def add_users(): """Init the '*****@*****.**' and '*****@*****.**' user accounts.""" with transaction.manager: admins = Group.by_name('admins') staff = Group.by_name('staff') enabled = Group.by_name('enabled') trial = Group.by_name('trial') # Create the admin user account admin = User( email=u'*****@*****.**', password=SECRET_ENC, fullname=u'Ädmin', properties=[ UserProperty(key=u'bimt', value=u'on'), ], ) admin.groups.append(admins) admin.groups.append(enabled) Session.add(admin) # Create the staff member account staff_member = User( email=u'*****@*****.**', password=SECRET_ENC, fullname=u'Stäff Member', properties=[ UserProperty(key=u'bimt', value=u'on'), ], ) staff_member.groups.append(enabled) staff_member.groups.append(staff) Session.add(staff_member) # Create the normal user account one = User( email=u'*****@*****.**', billing_email=u'*****@*****.**', password=SECRET_ENC, fullname=u'Öne Bar', properties=[ UserProperty(key=u'bimt', value=u'on'), ], ) one.groups.append(enabled) one.groups.append(trial) Session.add(one)
def save_success(self, appstruct): user = self.request.context user.email = appstruct.get('email') user.fullname = appstruct.get('fullname') user.affiliate = appstruct.get('affiliate') user.billing_email = appstruct.get('billing_email') user.valid_to = appstruct.get('valid_to') user.last_payment = appstruct.get('last_payment') if appstruct.get('password'): user.password = encrypt(appstruct['password']) groups = [Group.by_id(group_id) for group_id in appstruct['groups']] enabled = Group.by_name('enabled') if enabled in user.groups: groups.append(enabled) user.groups = groups # remove properties that are not present in appstruct for prop in copy.copy(user.properties): if prop.key not in [p['key'] for p in appstruct['properties']]: user.properties.remove(prop) # update/create properties present in appstruct for prop in appstruct['properties']: if user.has_property(prop['key']): user.set_property(key=prop['key'], value=prop.get('value')) else: user.properties.append( UserProperty(key=prop['key'], value=prop.get('value'))) self.request.session.flash( u'User "{}" modified.'.format(user.email)) return HTTPFound( location=self.request.route_path( 'user_view', user_id=user.id))
def test__repr__(self): self.assertEqual( repr(UserProperty(id=1, key=u'foo', value=u'bar')), '<UserProperty:1 (key=u\'foo\', value=u\'bar\')>', )
def _make_property(id=1, key=u'foo', value=u'bar'): return UserProperty( id=id, key=key, value=value, )