def test_can_update_by_user(self): user = UserFactory.create() key = KeyFactory.create(name='some.random') pref = UsersViolationsPrefsFactory.create(user=user, key=key, is_active=True) data = [{ 'key': pref.key.name, 'is_active': False }, { 'key': 'blah', 'is_active': False }] UsersViolationsPrefs.update_by_user(self.db, user, data) prefs = self.db.query(UsersViolationsPrefs).all() expect(prefs).to_length(1) expect(prefs[0].key.name).to_equal(key.name) expect(prefs[0].user).to_equal(user) expect(prefs[0].is_active).to_equal(False)
def test_can_save_prefs(self): user = UserFactory.create(is_superuser=True) key1 = KeyFactory.create(name='some.random.1') key2 = KeyFactory.create(name='some.random.2') UsersViolationsPrefsFactory.create(user=user, key=key1, is_active=True) UsersViolationsPrefsFactory.create(user=user, key=key2, is_active=False) loaded_prefs = UsersViolationsPrefs.get_prefs(self.db, user) expect(loaded_prefs).to_length(2) expect(loaded_prefs).to_be_like({ 'some.random.1': True, 'some.random.2': False }) yield self.authenticated_fetch( '/users/violations-prefs/', user_email=user.email, method='POST', body=dumps([ {'key': 'some.random.1', 'is_active': False}, {'key': 'some.random.2', 'is_active': True}, ]) ) loaded_prefs = UsersViolationsPrefs.get_prefs(self.db, user) expect(loaded_prefs).to_length(2) expect(loaded_prefs).to_be_like({ 'some.random.1': False, 'some.random.2': True })
def test_can_get_prefs(self): user = UserFactory.create() key1 = KeyFactory.create() key2 = KeyFactory.create() UsersViolationsPrefs.insert_pref(self.db, user, key1, True) UsersViolationsPrefs.insert_pref(self.db, user, key2, False) data = UsersViolationsPrefs.get_prefs(self.db, user) expect(data).to_length(2) expect(data).to_be_like({key1.name: True, key2.name: False})
def post(self): user = self.get_authenticated_user() if not user: return post_data = loads(self.request.body) UsersViolationsPrefs.update_by_user(self.db, user, post_data) self.write_json({ 'reason': self._('Preferences successfully saved!'), 'description': self._('Preferences successfully saved!') })
def test_can_get_prefs(self): user = UserFactory.create() key1 = KeyFactory.create() key2 = KeyFactory.create() UsersViolationsPrefs.insert_pref(self.db, user, key1, True) UsersViolationsPrefs.insert_pref(self.db, user, key2, False) data = UsersViolationsPrefs.get_prefs(self.db, user) expect(data).to_length(2) expect(data).to_be_like( {key1.name: True, key2.name: False} )
def test_can_insert_prefs_without_user(self): user = None prefs = self.db.query(UsersViolationsPrefs).all() expect(prefs).to_length(0) items = [] for x in range(3): name = 'key-test-%d' % x KeyFactory.create(name=name) items.append(name) UsersViolationsPrefs.insert_prefs(self.db, user, items) prefs = self.db.query(UsersViolationsPrefs).all() expect(prefs).to_length(0)
def test_can_update_by_user_with_no_data(self): user = UserFactory.create() key = KeyFactory.create(name='some.random') UsersViolationsPrefsFactory.create(user=user, key=key, is_active=True) data = [] UsersViolationsPrefs.update_by_user(self.db, user, data) prefs = self.db.query(UsersViolationsPrefs).all() expect(prefs).to_length(1) expect(prefs[0].key.name).to_equal(key.name) expect(prefs[0].user).to_equal(user) expect(prefs[0].is_active).to_equal(True)
def test_can_insert_pref(self): user = UserFactory.create() key1 = KeyFactory.create() key2 = KeyFactory.create() UsersViolationsPrefs.insert_pref(self.db, user, key1, True) UsersViolationsPrefs.insert_pref(self.db, user, key2, False) prefs = self.db.query(UsersViolationsPrefs).all() expect(prefs).to_length(2) expect(prefs[0].key).to_equal(key1) expect(prefs[0].user).to_equal(user) expect(prefs[0].is_active).to_equal(True) expect(prefs[1].key).to_equal(key2) expect(prefs[1].user).to_equal(user) expect(prefs[1].is_active).to_equal(False)
def test_can_delete_prefs(self): user = UserFactory.create() pref1 = UsersViolationsPrefsFactory.create(user=user) pref2 = UsersViolationsPrefsFactory.create(user=user) prefs = self.db.query(UsersViolationsPrefs).all() expect(prefs).to_length(2) items = [pref1.key.name] UsersViolationsPrefs.delete_prefs(self.db, user, items) prefs = self.db.query(UsersViolationsPrefs).all() expect(prefs).to_length(1) expect(prefs[0].key.name).to_equal(pref2.key.name) expect(prefs[0].user).to_equal(pref2.user) expect(prefs[0].is_active).to_equal(True)
def test_can_update_by_user(self): user = UserFactory.create() key = KeyFactory.create(name='some.random') pref = UsersViolationsPrefsFactory.create(user=user, key=key, is_active=True) data = [ {'key': pref.key.name, 'is_active': False}, {'key': 'blah', 'is_active': False} ] UsersViolationsPrefs.update_by_user(self.db, user, data) prefs = self.db.query(UsersViolationsPrefs).all() expect(prefs).to_length(1) expect(prefs[0].key.name).to_equal(key.name) expect(prefs[0].user).to_equal(user) expect(prefs[0].is_active).to_equal(False)
def get(self): user = self.get_authenticated_user() if not user: return user_prefs = UsersViolationsPrefs.get_prefs(self.db, user) violation_defs = self.application.violation_definitions user_prefs_keys = set(user_prefs.keys()) violation_defs_keys = set(violation_defs.keys()) insert_items = list(violation_defs_keys - user_prefs_keys) if insert_items: UsersViolationsPrefs.insert_prefs(self.db, user, insert_items) remove_items = list(user_prefs_keys - violation_defs_keys) if remove_items: UsersViolationsPrefs.delete_prefs(self.db, user, remove_items) result = [] for key_name in violation_defs: category = violation_defs[key_name]['category'] name = violation_defs[key_name]['title'] description = violation_defs[key_name]['generic_description'] is_active = user_prefs.get(key_name, True) result.append({ 'key': key_name, 'name': self._(name), 'description': self._(description), 'category': self._(category), 'is_active': is_active }) self.write_json(result)