def setUp(self): self.c = Client() user = User(username='******', password='******') user.save() data = {'user': '******', 'pass': '******'} request = self.c.post('/users/login/', data, content_type='application/json').json() global USER0TOKEN USER0TOKEN = 'Token ' + request['token'] user = User(username='******', password='******') user.save() data = {'user': '******', 'pass': '******'} request = self.c.post('/users/login/', data, content_type='application/json').json() global USER1TOKEN USER1TOKEN = 'Token ' + request['token'] user = User(username='******', password='******') user.save() data = {'user': '******', 'pass': '******'} request = self.c.post('/users/login/', data, content_type='application/json').json() global USER2TOKEN USER2TOKEN = 'Token ' + request['token'] user = User(username='******', password='******') user.save() data = {'user': '******', 'pass': '******'} request = self.c.post('/users/login/', data, content_type='application/json').json() global USER3TOKEN USER3TOKEN = 'Token ' + request['token'] self.user1 = User.objects.get(username='******') self.user2 = User.objects.get(username='******') self.user3 = User.objects.get(username='******') self.user4 = User.objects.get(username='******') self.player1 = Player(username=self.user1, colour='green') self.player1.save() self.player2 = Player(username=self.user2, colour='green') self.player2.save() self.player3 = Player(username=self.user3, colour='green') self.player3.save() self.player4 = Player(username=self.user4, colour='green') self.player4.save() self.room = Room(id=1, name="Cool Name", owner=self.user1, max_players=4) self.room.save() self.room.players.add(self.player1) self.room.players.add(self.player2) self.room.players.add(self.player3) self.room.players.add(self.player4)
def test_IniciarPartida_menosde4(self): user = User.objects.get(username='******') player = Player(username=user, colour='green') player.save() self.add_board() board = Board.objects.get(id=0) room = Room(id=1, name="Room Test", owner=user, max_players=4, board=board) room.players.add(player) room.save() user = User.objects.get(username='******') player = Player(username=user, colour='blue') player.save() room.players.add(player) room.save() user = User.objects.get(username='******') player = Player(username=user, colour='yellow') player.save() room.players.add(player) room.save() c = Client() print('Testeando iniciar partida con menos jugadores') request = c.patch('/rooms/1/', HTTP_AUTHORIZATION=USER0TOKEN, content_type='application/json') self.assertEqual(request.status_code, 400) print('OK')
def setUp(self): user = User(username='******', password='******') user.save() player = Player(username=user, colour='red') player.save() user = User(username='******', password='******') user.save() player = Player(username=user, colour='blue') player.save() user = User(username='******', password='******') user.save() player = Player(username=user, colour='green') player.save() self.add_board() board = Board.objects.get(id=0) room = Room(id=1, name='ahah', board=board, owner=user, max_players=2) room.save()
def test_getParticularsRooms(self): print('Testeando GET a /rooms/<id>/...') user = User.objects.get(username='******') player = Player(username=user, colour='green') player.save() room = Room(id=1, name="Cool Name", owner=user, max_players=2) room.save() c = Client() request = c.get('/rooms/1/', HTTP_AUTHORIZATION=USER0TOKEN, content_type='application/json') self.assertEqual(request.status_code, 200) request = request.json() self.assertEqual(request['name'], "Cool Name") request = c.get('/rooms/45/', HTTP_AUTHORIZATION=USER0TOKEN, content_type='application/json') self.assertEqual(request.status_code, 400) print('-OK!-')
def test_Put(self): print('Testeando PUT a /rooms/<id>...') user = User.objects.get(username='******') player = Player(username=user, colour='green') player.save() room = Room(name="Cool Name", owner=user, max_players=2) room.save() c = Client() request = c.put('/rooms/1/', HTTP_AUTHORIZATION=USER0TOKEN, content_type='application/json') self.assertEqual(request.status_code, 200) request = c.get('/rooms/', HTTP_AUTHORIZATION=USER0TOKEN, content_type='application/json') self.assertEqual(request.status_code, 200) request = request.json() self.assertEqual(len(list(request[0]['players'])), 1) print('-OK!-')
def test_stress(self): print('Estresando server...') r = Room.objects.all().delete() for x in range(0, 1000): user = User.objects.get(username='******') player = Player(id=1, username=user, colour='green') player.save() room = Room(id=1, name="Cool Name", owner=user, max_players=2) room.save() c = Client() request = c.put('/rooms/1/', HTTP_AUTHORIZATION=USER0TOKEN, content_type='application/json') self.assertEqual(request.status_code, 200) request = c.get('/rooms/', HTTP_AUTHORIZATION=USER0TOKEN, content_type='application/json') self.assertEqual(request.status_code, 200) request = request.json() self.assertEqual(len(list(request[0]['players'])), 1) print('-OK!-')
def test_addMorePermited(self): print('Testeando el agregado de jugadores:', 'Cuando la Room esta llena...') user = User.objects.get(username='******') player = Player(username=user, colour='green') player.save() room = Room(id=2, name="Cool Name", owner=user, max_players=2) room.save() c = Client() tokenlist = [USER0TOKEN, USER1TOKEN, USER2TOKEN] for username in range(0, 2): if username < 3: request = c.put('/rooms/2/', HTTP_AUTHORIZATION=tokenlist[username], content_type='application/json') self.assertEqual(request.status_code, 200) else: request = c.put('/rooms/2/', HTTP_AUTHORIZATION=tokenlist[username], content_type='application/json') self.assertEqual(request.status_code, 400) print('-OK!-')
class TestsEliminarRoom(TestCase): def setUp(self): self.c = Client() user = User(username='******', password='******') user.save() data = {'user': '******', 'pass': '******'} request = self.c.post('/users/login/', data, content_type='application/json').json() global USER0TOKEN USER0TOKEN = 'Token ' + request['token'] user = User(username='******', password='******') user.save() data = {'user': '******', 'pass': '******'} request = self.c.post('/users/login/', data, content_type='application/json').json() global USER1TOKEN USER1TOKEN = 'Token ' + request['token'] user = User(username='******', password='******') user.save() data = {'user': '******', 'pass': '******'} request = self.c.post('/users/login/', data, content_type='application/json').json() global USER2TOKEN USER2TOKEN = 'Token ' + request['token'] user = User(username='******', password='******') user.save() data = {'user': '******', 'pass': '******'} request = self.c.post('/users/login/', data, content_type='application/json').json() global USER3TOKEN USER3TOKEN = 'Token ' + request['token'] self.user1 = User.objects.get(username='******') self.user2 = User.objects.get(username='******') self.user3 = User.objects.get(username='******') self.user4 = User.objects.get(username='******') self.player1 = Player(username=self.user1, colour='green') self.player1.save() self.player2 = Player(username=self.user2, colour='green') self.player2.save() self.player3 = Player(username=self.user3, colour='green') self.player3.save() self.player4 = Player(username=self.user4, colour='green') self.player4.save() self.room = Room(id=1, name="Cool Name", owner=self.user1, max_players=4) self.room.save() self.room.players.add(self.player1) self.room.players.add(self.player2) self.room.players.add(self.player3) self.room.players.add(self.player4) def test_BorrarRoom(self): print('Testeando DELETE a /rooms/<id>/', 'tratando de borrar una room con 4 jugadores', 'siendo el owner de la misma.') request = self.c.delete('/rooms/1/', HTTP_AUTHORIZATION=USER0TOKEN, content_type='application/json') self.assertEqual(request.status_code, 200) request = request.json() self.assertEqual(request['OK'], "Room eliminada") print('OK') print('Testeando volver a hacer DELETE a la room recien eliminada...', 'Asegurandose de que efectivamente se haya borrado.') request = self.c.delete('/rooms/1/', HTTP_AUTHORIZATION=USER0TOKEN, content_type='application/json') self.assertEqual(request.status_code, 400) request = request.json() self.assertEqual(request['error'], "No existe Lobby con id: 1") print('OK') def test_BorrarRoomSinSerOwner(self): print('Testeando DELETE a /rooms/<id>/', 'tratando de borrar una room sin ser el owner de la misma.') request = self.c.delete('/rooms/1/', HTTP_AUTHORIZATION=USER1TOKEN, content_type='application/json') self.assertEqual(request.status_code, 401) request = request.json() self.assertEqual(request['error'], "Sólo el dueño puede eliminar la partida") print('OK') def test_BorrarRoomInexistente(self): print('Testeando DELETE a /rooms/<id>/', 'tratando de borrar una room', 'sin que exista una room con ese id.') request = self.c.delete('/rooms/25/', HTTP_AUTHORIZATION=USER0TOKEN, content_type='application/json') self.assertEqual(request.status_code, 400) request = request.json() self.assertEqual(request['error'], "No existe Lobby con id: 25") print('OK') def test_BorrarPartidaComenzada(self): print('Testeando DELETE a /rooms/<id>/', 'tratando de borrar una room cuyo game ya comenzó') self.room.game_has_started = True self.room.save() request = self.c.delete('/rooms/1/', HTTP_AUTHORIZATION=USER0TOKEN, content_type='application/json') self.assertEqual(request.status_code, 400) request = request.json() self.assertEqual(request['error'], "No se puede borrar una partida ya comenzada") print('OK')