def allow_access(email, bucket): """Give a user access to a bucket.""" db = get_database() repository = UserConfigRepository(db) config = repository.retrieve(email) or UserConfig(email) if bucket not in config.buckets: config = UserConfig(email, config.buckets + [bucket]) repository.save(config)
class TestUserRepositoryIntegration(object): def setUp(self): self.db = Database(HOST, PORT, DB_NAME) self.db._mongo.drop_database(DB_NAME) self.mongo_collection = self.db.get_collection("users")._collection self.mongo_collection.drop() self.repository = UserConfigRepository(self.db) def test_saving_a_config_with_no_buckets(self): config = UserConfig(email="*****@*****.**") self.repository.save(config) results = list(self.mongo_collection.find()) assert_that(len(results), is_(1)) assert_that(results[0], has_entries({ "email": "*****@*****.**", "buckets": [], }))
class TestUserConfigRepository(object): def setUp(self): self.db = Mock() self.mongo_collection = Mock() self.db.get_collection.return_value = self.mongo_collection self.repository = UserConfigRepository(self.db) def test_saving_a_user_config(self): user = UserConfig("*****@*****.**", buckets=["bucket_one", "bucket_two"]) self.repository.save(user) self.mongo_collection.save.assert_called_with( match_equality(has_entries({ "_id": "*****@*****.**", "buckets": ["bucket_one", "bucket_two"] })) ) def test_saving_fails_with_non_user_config_object(self): not_user = {"foo": "bar"} assert_raises(ValueError, self.repository.save, not_user) def test_retrieving_a_user_config(self): self.mongo_collection.find_one.return_value = { "_id": "*****@*****.**", "email": "*****@*****.**", "buckets": ["foo", "bar"], } user_config = self.repository.retrieve(email="*****@*****.**") expected_user_config = UserConfig("*****@*****.**", ["foo", "bar"]) assert_that(user_config, equal_to(expected_user_config)) def test_retrieving_non_existent_user_config_returns_none(self): self.mongo_collection.find_one.return_value = None user_config = self.repository.retrieve(email="*****@*****.**") assert_that(user_config, is_(None))
def setUp(self): self.db = Mock() self.mongo_collection = Mock() self.db.get_collection.return_value = self.mongo_collection self.repository = UserConfigRepository(self.db)
def setUp(self): self.db = Database(HOST, PORT, DB_NAME) self.db._mongo.drop_database(DB_NAME) self.mongo_collection = self.db.get_collection("users")._collection self.mongo_collection.drop() self.repository = UserConfigRepository(self.db)