def setUp(self): super(DrawResourceTest, self).setUp() django.setup() # mongodb instance self.mongo = mongodb.MongoDriver.instance() # Create a user for authentication test_user = bom.User('*****@*****.**') test_user.set_password('test') self.mongo.save_user(test_user) self.user = test_user # base url of the resource self.base_url = '/v1/draw/' # Create an object we will use to test item = RandomNumberDraw() item.users = [self.user.pk] item.title = "Titulo" item.description = "A description, it can be long. Rather long" self.mongo.save_draw(item) self.item = item # We also build a detail URI, since we will be using it all over. self.detail_url = self.base_url + '{0}/'.format( urllib.quote(self.item.pk))
def test_update_random_number_ok(self): self.login() data = { 'title': 'test_draw', 'is_shared': True, 'enable_chat': True, 'users': ['*****@*****.**'], 'range_min': 5, 'range_max': 6, 'allow_repeat': True, } draw = RandomNumberDraw(**data) draw.owner = self.user.pk self.mongo.save_draw(draw) update_data = { 'range_min': 50, 'range_max': 60 } resp = self.api_client.patch(self.detail_uri(draw), data=update_data) print(resp) self.assertHttpAccepted(resp) draw = self.mongo.retrieve_draw(draw.pk) for key, value in update_data.items(): self.assertEqual(value, getattr(draw, key)) self.mongo.remove_draw(draw.pk)
def setUp(self): super(FavouriteResourceTest, self).setUp() django.setup() # mongodb instance self.mongo = mongodb.MongoDriver.instance() # Create a user for authentication test_user = bom.User('*****@*****.**') test_user.set_password('test') self.mongo.save_user(test_user) self.user = test_user # base url of the resource self.base_url = '/v1/favourite/' # Create an object we will use to test item = RandomNumberDraw() self.mongo.save_draw(item) self.user.favourites.append(item.pk) self.mongo.save_user(self.user) self.item = item # We also build a detail URI, since we will be using it all over. self.detail_url = self.base_url + '{0}/'.format( urllib.quote(self.item.pk))
def test_update_not_owner_unauthorised(self): self.login() data = { 'title': 'test_draw', 'is_shared': True, 'enable_chat': True, 'users': ['*****@*****.**'], 'range_min': 5, 'range_max': 6, 'allow_repeat': True, } draw = RandomNumberDraw(**data) draw.owner = "*****@*****.**" self.mongo.save_draw(draw) resp = self.api_client.patch(self.detail_uri(draw), data={}) print(resp) self.assertHttpUnauthorized(resp)
def test_toss_random_number_ok(self): self.login() data = { 'title': 'test_draw', 'is_shared': True, 'enable_chat': True, 'users': ['*****@*****.**'], 'range_min': 5, 'range_max': 6, 'allow_repeat': True, } draw = RandomNumberDraw(**data) draw.owner = self.user.pk self.mongo.save_draw(draw) resp = self.toss(draw) print(resp) self.assertHttpOK(resp) draw = self.mongo.retrieve_draw(draw.pk) self.assertEqual(1, len(draw.results)) self.mongo.remove_draw(draw.pk)
def test_update_random_number_not_feasible(self): self.login() data = { 'title': 'test_draw', 'is_shared': True, 'enable_chat': True, 'users': ['*****@*****.**'], 'range_min': 5, 'range_max': 6, 'allow_repeat': True, } draw = RandomNumberDraw(**data) draw.owner = self.user.pk self.mongo.save_draw(draw) update_data = { 'range_min': 70, 'range_max': 60 } resp = self.api_client.patch(self.detail_uri(draw), data=update_data) print(resp) self.assertHttpBadRequest(resp) self.mongo.remove_draw(draw.pk)
def test_update_random_number_forbidden_att(self): self.login() data = { 'title': 'test_draw', 'is_shared': True, 'enable_chat': True, 'users': ['*****@*****.**'], 'range_min': 5, 'range_max': 6, 'allow_repeat': True, } draw = RandomNumberDraw(**data) draw.owner = self.user.pk self.mongo.save_draw(draw) for attr in ['results', 'owner', '_id', 'pk', 'creation_time', 'last_updated_time', 'audit']: data[attr] = "something" resp = self.api_client.patch(self.detail_uri(draw), format='json', data=data) self.assertHttpBadRequest(resp) data.pop(attr) self.mongo.remove_draw(draw.pk)
def test_update_random_number_fake_attributes(self): self.login() data = { 'title': 'test_draw', 'is_shared': True, 'enable_chat': True, 'users': ['*****@*****.**'], 'range_min': 5, 'range_max': 6, 'allow_repeat': True, } draw = RandomNumberDraw(**data) draw.owner = self.user.pk self.mongo.save_draw(draw) for attr in ['not_an_attribute', 'participants', 'a_test']: data[attr] = "something" resp = self.api_client.patch(self.detail_uri(draw), format='json', data=data) self.assertHttpAccepted(resp) updated_draw = self.mongo.retrieve_draw(draw.pk) self.assertFalse(hasattr(updated_draw, attr)) data.pop(attr) self.mongo.remove_draw(draw.pk)