Beispiel #1
0
 def test_no_card(self):
     self.cardRoad.delete()
     path = reverse('PlayerActions', kwargs={'pk': 1})
     data = {"type": "play_road_building_card",
             "payload": [[{"level": 2, "index": 1},
                         {"level": 2, "index": 2}],
                         [{"level": 2, "index": 2},
                         {"level": 2, "index": 3}]]}
     request = RequestFactory().post(path, data,
                                     content_type='application/json')
     force_authenticate(request, user=self.user, token=self.token)
     view = PlayerActions.as_view()
     response = view(request, pk=1)
     path_game = reverse('GameInfo', kwargs={'pk': 1})
     request_game = RequestFactory().get(path_game)
     force_authenticate(request_game, user=self.user, token=self.token)
     view_game = GameInfo.as_view()
     response_game = view_game(request_game, pk=1)
     response_player = self.get_info_player(1)
     assert response_player.data['cards'] == []
     expected_data = [
         [{'level': 2, 'index': 0},
          {'level': 2, 'index': 1}]
     ]
     assert response_game.data['players'][0]['roads'] == expected_data
     assert response_game.data['players'][0]['development_cards'] == 0
     assert response.data == {'detail': 'Missing Road Building card'}
     assert response.status_code == 403
Beispiel #2
0
 def test_road_building_card_one_road(self):
     path = reverse('PlayerActions', kwargs={'pk': 1})
     data = {"type": "play_road_building_card",
             "payload": [[{"level": 2, "index": 1},
                         {"level": 2, "index": 2}],
                         [{"level": 2, "index": 2},
                         {"level": 2, "index": 1000}]]}
     request = RequestFactory().post(path, data,
                                     content_type='application/json')
     force_authenticate(request, user=self.user, token=self.token)
     view = PlayerActions.as_view()
     response = view(request, pk=1)
     path_game = reverse('GameInfo', kwargs={'pk': 1})
     request_game = RequestFactory().get(path_game)
     force_authenticate(request_game, user=self.user, token=self.token)
     view_game = GameInfo.as_view()
     response_game = view_game(request_game, pk=1)
     assert len(response_game.data['players'][0]['roads']) == 1
     expected_data = [
         [{'level': 2, 'index': 0},
          {'level': 2, 'index': 1}]
     ]
     assert response_game.data['players'][0]['roads'] == expected_data
     assert response_game.data['players'][0]['development_cards'] == 1
     assert response.data == {'detail': 'Non-existent vertexs positions'}
     assert response.status_code == 403
Beispiel #3
0
 def test_gain_resources(self):
     self.player1.gain_resources('wool', 1)
     self.player1.gain_resources('brick', 1)
     self.player1.gain_resources('lumber', 1)
     self.player1.gain_resources('ore', 2)
     self.player1.gain_resources('grain', 2)
     url_game = reverse('GameInfo', kwargs={'pk': 1})
     request_game = RequestFactory().get(url_game)
     force_authenticate(request_game, user=self.user1, token=self.token)
     view_game = GameInfo.as_view()
     response_game = view_game(request_game, pk=1)
     url_player = reverse('PlayerInfo', kwargs={'pk': 1})
     request_player = RequestFactory().get(url_player)
     force_authenticate(request_player, user=self.user1, token=self.token)
     view_player = PlayerInfo.as_view()
     response_player = view_player(request_player, pk=1)
     assert len(response_player.data['resources']) == 7
     assert response_player.data['resources'] == [
         'wool', 'brick', 'lumber', 'ore', 'ore', 'grain', 'grain'
     ]
     assert response_player.status_code == 200
     assert response_game.data['players'][0]['resources_cards'] == 7
     assert response_game.data['players'][0]['last_gained'] == [
         'wool', 'brick', 'lumber', 'ore', 'ore', 'grain', 'grain'
     ]
     assert response_game.status_code == 200
Beispiel #4
0
 def get_info_game(self, pk):
     path_game = reverse('GameInfo', kwargs={'pk': pk})
     request_game = RequestFactory().get(path_game)
     force_authenticate(request_game, user=self.user, token=self.token)
     view_game = GameInfo.as_view()
     response_game = view_game(request_game, pk=pk)
     return response_game
Beispiel #5
0
 def test_GameInfoNoExists(self):
     path = reverse('GameInfo', kwargs={'pk': 1})
     request = RequestFactory().get(path)
     force_authenticate(request, user=self.user, token=self.token)
     view = GameInfo.as_view()
     response = view(request, pk=1)
     response.render()
     assert response.status_code == 404
Beispiel #6
0
 def test_end_turn(self):
     self.turn.dices1 = 2
     self.turn.dices2 = 2
     self.turn.save()
     url = reverse('PlayerActions', kwargs={'pk': 1})
     data = {"type": "end_turn", "payload": None}
     request = RequestFactory().post(url,
                                     data,
                                     content_type='application/json')
     force_authenticate(request, user=self.user1, token=self.token)
     view_actions = PlayerActions.as_view()
     response = view_actions(request, pk=1)
     assert response.status_code == 204
     url = reverse('GameInfo', kwargs={'pk': 1})
     request = RequestFactory().get(url)
     force_authenticate(request, user=self.user1, token=self.token)
     view_game = GameInfo.as_view()
     response = view_game(request, pk=1)
     assert response.data['current_turn']['user'] == "user2"
Beispiel #7
0
 def test_throw_dices_7(self):
     board_test = generateBoardTest()
     game_test = Game.objects.create(name='Juego',
                                     board=board_test,
                                     robber=self.robber)
     current_turn = Current_Turn.objects.create(game=game_test,
                                                user=self.user1)
     player_test = mixer.blend(Player,
                               username=self.user1,
                               game=game_test,
                               colour='red')
     Building.objects.create(game=game_test,
                             owner=player_test,
                             name='city',
                             level=0,
                             index=0)
     Building.objects.create(game=game_test,
                             owner=player_test,
                             name='city',
                             level=2,
                             index=11)
     Building.objects.create(game=game_test,
                             owner=player_test,
                             name='settlement',
                             level=1,
                             index=2)
     Building.objects.create(game=game_test,
                             owner=player_test,
                             name='settlement',
                             level=2,
                             index=2)
     game_test.throw_dices(dice1=3, dice2=4)
     url_game = reverse('GameInfo', kwargs={'pk': 2})
     request_game = RequestFactory().get(url_game)
     force_authenticate(request_game, user=self.user1, token=self.token)
     view_game = GameInfo.as_view()
     response_game = view_game(request_game, pk=2)
     assert response_game.data['players'][0]['resources_cards'] == 0
     assert response_game.data['players'][0]['last_gained'] == []
     assert response_game.status_code == 200
Beispiel #8
0
 def test_end_turn_Not_in_turn(self):
     """
     If the user requesting the end of his shift is not in
     his turn, then he must indicate prohibited
     """
     url = reverse('PlayerActions', kwargs={'pk': 1})
     data = {"type": "end_turn", "payload": None}
     request = RequestFactory().post(url,
                                     data,
                                     content_type='application/json')
     # Set the user to make the post as self.user2,
     # but the user in turn is user1
     force_authenticate(request, user=self.user2, token=self.token)
     view_actions = PlayerActions.as_view()
     response = view_actions(request, pk=1)
     assert response.status_code == 403
     assert response.data['detail'] == 'Not in turn'
     url = reverse('GameInfo', kwargs={'pk': 1})
     request = RequestFactory().get(url)
     force_authenticate(request, user=self.user2, token=self.token)
     view_game = GameInfo.as_view()
     response = view_game(request, pk=1)
     assert response.data['current_turn']['user'] == 'user1'
Beispiel #9
0
 def test_end_turn_4(self):
     """
     A test to see the change of users in turn according
     to the user requesting the end of their shift
     """
     self.turn.dices1 = 2
     self.turn.dices2 = 2
     self.turn.user = self.user4
     self.turn.save()
     url = reverse('PlayerActions', kwargs={'pk': 1})
     data = {"type": "end_turn", "payload": None}
     request = RequestFactory().post(url,
                                     data,
                                     content_type='application/json')
     force_authenticate(request, user=self.user4, token=self.token)
     view_actions = PlayerActions.as_view()
     response = view_actions(request, pk=1)
     assert response.status_code == 204
     url = reverse('GameInfo', kwargs={'pk': 1})
     request = RequestFactory().get(url)
     force_authenticate(request, user=self.user4, token=self.token)
     view_game = GameInfo.as_view()
     response = view_game(request, pk=1)
     assert response.data['current_turn']['user'] == "user1"
Beispiel #10
0
 def test_throw_dices(self):
     board_test = generateBoardTest()
     game_test = Game.objects.create(name='Juego',
                                     board=board_test,
                                     robber=self.robber)
     current_turn = Current_Turn.objects.create(game=game_test,
                                                user=self.user1)
     player_test = mixer.blend(Player,
                               username=self.user1,
                               game=game_test,
                               colour='red')
     Building.objects.create(game=game_test,
                             owner=player_test,
                             name='city',
                             level=0,
                             index=0)
     Building.objects.create(game=game_test,
                             owner=player_test,
                             name='city',
                             level=1,
                             index=12)
     Building.objects.create(game=game_test,
                             owner=player_test,
                             name='city',
                             level=1,
                             index=6)
     Building.objects.create(game=game_test,
                             owner=player_test,
                             name='city',
                             level=2,
                             index=4)
     Building.objects.create(game=game_test,
                             owner=player_test,
                             name='city',
                             level=2,
                             index=29)
     Building.objects.create(game=game_test,
                             owner=player_test,
                             name='settlement',
                             level=0,
                             index=3)
     Building.objects.create(game=game_test,
                             owner=player_test,
                             name='settlement',
                             level=1,
                             index=3)
     Building.objects.create(game=game_test,
                             owner=player_test,
                             name='settlement',
                             level=1,
                             index=15)
     Building.objects.create(game=game_test,
                             owner=player_test,
                             name='settlement',
                             level=2,
                             index=1)
     throws = [(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (2, 6), (3, 6),
               (5, 5), (6, 5), (6, 6)]
     expected_resources = TYPE_RESOURCE + TYPE_RESOURCE
     hexes_positions = [[0, 0], [1, 0], [1, 1], [1, 2], [1, 3], [1, 4],
                        [1, 5], [2, 0], [2, 1], [2, 2]]
     for i in range(10):
         hexe = Hexe.objects.get(board=board_test,
                                 level=hexes_positions[i][0],
                                 index=hexes_positions[i][1])
         game_test.robber = hexe
         game_test.save()
         game_test.throw_dices(dice1=throws[i][0], dice2=throws[i][1])
         url_game = reverse('GameInfo', kwargs={'pk': 2})
         request_game = RequestFactory().get(url_game)
         force_authenticate(request_game, user=self.user1, token=self.token)
         view_game = GameInfo.as_view()
         response_game = view_game(request_game, pk=2)
         print(hexe.index)
         print(hexe.level)
         assert response_game.data['players'][0]['resources_cards'] == 0
         assert response_game.data['players'][0]['last_gained'] == []
     game_test.robber = self.robber
     game_test.save()
     for i in range(10):
         self.player1.set_not_last_gained()
         game_test.throw_dices(dice1=throws[i][0], dice2=throws[i][1])
         url_game = reverse('GameInfo', kwargs={'pk': 2})
         request_game = RequestFactory().get(url_game)
         force_authenticate(request_game, user=self.user1, token=self.token)
         view_game = GameInfo.as_view()
         response_game = view_game(request_game, pk=2)
         assert response_game.data['players'][0]['resources_cards'] == (
             i + 1) * 3
         assert response_game.data['players'][0]['last_gained'] == [
             expected_resources[i]
         ] * 3
         assert response_game.status_code == 200
Beispiel #11
0
 def test_GameInfo(self):
     robber = mixer.blend('catan.Hexe', level=1, index=2)
     board = Board.objects.create(name='Colonos')
     game1 = Game.objects.create(name='Juego', board=board, robber=robber)
     user1 = mixer.blend(User, username='******', password='******')
     user2 = mixer.blend(User, username='******', password='******')
     player1 = mixer.blend('catan.Player',
                           username=user1,
                           game=game1,
                           colour='yellow',
                           development_cards=1,
                           resources_cards=2,
                           turn=1)
     player2 = mixer.blend('catan.Player',
                           username=user2,
                           game=game1,
                           colour='green',
                           turn=2,
                           development_cards=1,
                           resources_cards=2)
     current_turn = mixer.blend('catan.Current_Turn',
                                game=game1,
                                user=user1)
     resource1 = mixer.blend('catan.Resource',
                             owner=player1,
                             game=game1,
                             name='wool',
                             last_gained=True)
     building = mixer.blend('catan.Building',
                            name="settlement",
                            owner=player2,
                            level=2,
                            index=6,
                            game=game1)
     road = mixer.blend('catan.Road',
                        owner=player1,
                        game=game1,
                        level_1=1,
                        level_2=1,
                        index_1=2,
                        index_2=1)
     path = reverse('GameInfo', kwargs={'pk': 1})
     request = RequestFactory().get(path)
     force_authenticate(request, user=self.user, token=self.token)
     view = GameInfo.as_view()
     response = view(request, pk=1)
     expected_data = {
         'robber': {
             'level': 1,
             'index': 2
         },
         'current_turn': {
             'user': '******',
             'dice': [1, 1]
         },
         'winner':
         None,
         'players': [{
             'username':
             '******',
             'colour':
             'yellow',
             'victory_points':
             0,
             'resources_cards':
             1,
             'development_cards':
             0,
             'roads': [[{
                 'level': 1,
                 'index': 2
             }, {
                 'level': 1,
                 'index': 1
             }]],
             'last_gained': ['wool'],
             'settlements': [],
             'cities': []
         }, {
             'username': '******',
             'colour': 'green',
             'victory_points': 0,
             'resources_cards': 0,
             'development_cards': 0,
             'roads': [],
             'last_gained': [],
             'settlements': [{
                 'level': 2,
                 'index': 6
             }],
             'cities': []
         }]
     }
     assert response.data == expected_data
     assert response.status_code == 200