Exemple #1
0
    def test_post_valid_game(self):
        """
        Posting valid game data creates a game instance and redirects to
        `user-games` view. The user is automatically added as a participant of
        the game.
        """
        user = factories.UserFactory()
        self.client.force_authenticate(user=user)
        variant = factories.VariantFactory()

        data = {
            'name': 'Test Game',
        }
        url = reverse('create-game')
        response = self.client.post(url, data, format='json')

        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        self.assertTrue(
            models.Game.objects.get(
                name='Test Game',
                created_by=user,
                variant=variant,
                participants=user,
            )
        )
Exemple #2
0
 def test_ready_to_process_build(self):
     build_turn = models.Turn.objects.create(
         game=self.game,
         phase=Phase.BUILD,
         season=Season.FALL,
         year=1901,
     )
     england = self.variant.nations.get(name='England')
     france = self.variant.nations.get(name='France')
     other_user = factories.UserFactory()
     self.game.participants.add(other_user)
     models.NationState.objects.create(turn=build_turn,
                                       nation=england,
                                       user=other_user)
     france_state = models.NationState.objects.create(turn=build_turn,
                                                      nation=france,
                                                      user=self.user)
     territory = models.Territory.objects.get(name='Marseilles', )
     models.TerritoryState.objects.create(
         turn=build_turn,
         territory=territory,
         controlled_by=france,
     )
     self.assertFalse(build_turn.ready_to_process)
     # only nation states which have orders to submit must finalize
     france_state.orders_finalized = True
     france_state.save()
     self.assertTrue(build_turn.ready_to_process)
Exemple #3
0
 def test_other_user_orders(self):
     other_user = factories.UserFactory()
     other_nation = models.Nation.objects.create(
         variant=self.variant,
         name='Other Nation',
     )
     models.NationState.objects.create(
         nation=self.nation,
         turn=self.turn,
         user=other_user,
     )
     models.Order.objects.create(
         turn=self.turn,
         source=self.territory,
         nation=self.nation
     )
     models.Order.objects.create(
         turn=self.turn,
         source=self.territory,
         nation=other_nation
     )
     response = self.client.get(self.url, format='json')
     self.assertEqual(
         response.status_code,
         status.HTTP_200_OK,
     )
     self.assertEqual(len(response.data), 1)
     self.assertEqual(response.data[0]['nation'], self.nation.id)
Exemple #4
0
 def setUp(self):
     self.variant = factories.VariantFactory()
     self.user = factories.UserFactory()
     self.game = models.Game.objects.create(
         name='Test game',
         variant=self.variant,
         num_players=7,
         created_by=self.user,
     )
     self.game.participants.add(self.user)
     self.turn = models.Turn.objects.create(
         game=self.game,
         phase=Phase.BUILD,
         season=Season.FALL,
         year=1901,
     )
     self.nation = models.Nation.objects.create(
         variant=self.variant,
         name='France',
     )
     self.nation_state = models.NationState.objects.create(
         nation=self.nation,
         turn=self.turn,
         user=self.user,
     )
Exemple #5
0
 def test_ready_to_process_retreat(self):
     retreat_turn = models.Turn.objects.create(
         game=self.game,
         phase=Phase.RETREAT,
         season=Season.FALL,
         year=1901,
     )
     england = self.variant.nations.get(name='England')
     france = self.variant.nations.get(name='France')
     other_user = factories.UserFactory()
     self.game.participants.add(other_user)
     models.NationState.objects.create(turn=retreat_turn,
                                       nation=england,
                                       user=other_user)
     france_state = models.NationState.objects.create(turn=retreat_turn,
                                                      nation=france,
                                                      user=self.user)
     piece = models.Piece.objects.create(
         game=self.game,
         nation=france,
         type=PieceType.ARMY,
     )
     models.PieceState.objects.create(
         piece=piece,
         turn=retreat_turn,
         must_retreat=True,
     )
     self.assertFalse(retreat_turn.ready_to_process)
     # only nation states which have orders to submit must finalize
     france_state.orders_finalized = True
     france_state.save()
     self.assertTrue(retreat_turn.ready_to_process)
Exemple #6
0
    def setUp(self):
        self.user = factories.UserFactory()
        self.client.force_authenticate(user=self.user)

        self.variant = factories.VariantFactory()
        self.game = models.Game.objects.create(
            status=GameStatus.ACTIVE,
            variant=self.variant,
            name='Test Game',
            created_by=self.user,
            num_players=7
        )
        self.game.participants.add(self.user)
        self.turn = models.Turn.objects.create(
            game=self.game,
            year=1901,
            phase=Phase.ORDER,
            season=Season.SPRING,
        )
        self.nation = models.Nation.objects.create(
            variant=self.variant,
            name='Test Nation',
        )
        self.nation_state = models.NationState.objects.create(
            nation=self.nation,
            turn=self.turn,
            user=self.user,
        )
        self.territory = models.Territory.objects.create(
            variant=self.variant,
            name='Paris',
            nationality=self.nation,
            supply_center=True,
        )
        self.territory_state = models.TerritoryState.objects.create(
            territory=self.territory,
            turn=self.turn,
            controlled_by=self.nation_state.nation,
        )
        self.territory = self.territory_state.territory
        self.piece = models.Piece.objects.create(
            game=self.game,
            nation=self.nation_state.nation,
            type=PieceType.ARMY,
        )
        self.piece_state = models.PieceState.objects.create(
            turn=self.turn,
            piece=self.piece,
            territory=self.territory,
        )
        self.order = models.Order.objects.create(
            turn=self.turn,
            source=self.territory,
            nation=self.nation
        )
        self.url = reverse('order', args=[self.game.id, self.order.id])
Exemple #7
0
 def setUp(self):
     self.variant = factories.VariantFactory()
     self.user = factories.UserFactory()
     self.game = models.Game.objects.create(
         name='Test game',
         variant=self.variant,
         num_players=7,
         created_by=self.user,
     )
     self.game.participants.add(self.user)
Exemple #8
0
    def test_post_invalid_game(self):
        """
        Posting invalid game data causes a 400 error.
        """
        user = factories.UserFactory()
        self.client.force_authenticate(user=user)

        data = {}
        url = reverse('create-game')
        response = self.client.post(url, data, format='json')
        self.assertEqual(response.status_code, 400)
Exemple #9
0
    def test_join_game_full(self):
        """
        Cannot join a game when the game already has enough participants.
        """
        joined_user = factories.UserFactory()
        self.game.num_players = 1
        self.game.participants.add(joined_user)
        self.game.save()

        self.client.force_authenticate(user=self.user)
        response = self.client.put(self.url, self.data, format='json')
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
Exemple #10
0
 def setUp(self):
     self.data = {}
     self.user = factories.UserFactory()
     self.variant = factories.VariantFactory()
     self.game = models.Game.objects.create(
         status=GameStatus.PENDING,
         variant=self.variant,
         name='Test Game',
         created_by=self.user,
         num_players=7
     )
     self.url = reverse('toggle-join-game', args=[self.game.id])
Exemple #11
0
    def test_get_create_game(self):
        """
        Get create game returns a form.
        """
        user = factories.UserFactory()
        self.client.force_authenticate(user=user)

        url = reverse('create-game')
        response = self.client.get(url, format='json')
        self.assertEqual(
            response.status_code,
            status.HTTP_405_METHOD_NOT_ALLOWED,
        )
Exemple #12
0
 def setUp(self):
     self.variant = models.Variant.objects.get(id='standard')
     self.user = factories.UserFactory()
     self.game = models.Game.objects.create(
         name='Test game',
         variant=self.variant,
         num_players=7,
         created_by=self.user,
         order_deadline=DeadlineFrequency.TWENTY_FOUR_HOURS,
         retreat_deadline=DeadlineFrequency.TWELVE_HOURS,
         build_deadline=DeadlineFrequency.TWELVE_HOURS,
     )
     self.patch_process_turn_apply_async()
     self.patch_revoke_task_on_delete()
Exemple #13
0
 def test_delete_other_players_order(self):
     other_user = factories.UserFactory()
     self.game.participants.add(other_user)
     self.client.force_authenticate(user=other_user)
     nation = models.Nation.objects.create(
         variant=self.variant,
         name='Test Nation',
     )
     models.NationState.objects.create(
         nation=nation,
         turn=self.turn,
         user=other_user,
     )
     response = self.client.delete(self.url, format='json')
     self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
     self.assertTrue(models.Order.objects.get())
 def setUp(self):
     self.variant = models.Variant.objects.get(id='standard')
     self.user = factories.UserFactory()
     self.game = models.Game.objects.create(
         name='Test game',
         variant=self.variant,
         num_players=7,
         created_by=self.user,
     )
     self.game.participants.add(self.user)
     self.retreat_turn = models.Turn.objects.create(
         game=self.game,
         phase=Phase.RETREAT,
         season=Season.FALL,
         year=1901,
     )
Exemple #15
0
 def setUp(self):
     self.variant = models.Variant.objects.get(id='standard')
     self.users = []
     for i in range(7):
         self.users.append(factories.UserFactory())
     self.game = models.Game.objects.create(
         name='Test game',
         variant=self.variant,
         num_players=7,
         created_by=self.users[0],
         order_deadline=DeadlineFrequency.TWENTY_FOUR_HOURS,
         retreat_deadline=DeadlineFrequency.TWELVE_HOURS,
         build_deadline=DeadlineFrequency.TWELVE_HOURS,
     )
     self.game.participants.add(*self.users)
     self.patch_process_turn_apply_async()
Exemple #16
0
 def setUp(self):
     self.user = factories.UserFactory()
     self.variant = factories.VariantFactory()
     self.game = models.Game.objects.create(
         status=GameStatus.ACTIVE,
         variant=self.variant,
         name='Test Game',
         created_by=self.user,
         num_players=7
     )
     self.game.participants.add(self.user)
     turn = models.Turn.objects.create(
         game=self.game,
         year=1901,
         phase=Phase.ORDER,
         season=Season.SPRING,
     )
     nation = models.Nation.objects.create(
         variant=self.game.variant,
         name='France',
     )
     self.nation_state = models.NationState.objects.create(
         nation=nation,
         turn=turn,
         user=self.user,
         orders_finalized=True,
     )
     territory_state = factories.TerritoryStateFactory(
         turn=self.game.get_current_turn(),
         controlled_by=self.nation_state.nation,
     )
     self.territory = territory_state.territory
     self.url = reverse(
         'toggle-finalize-orders',
         args=[self.nation_state.id]
     )
     self.data = {}
Exemple #17
0
 def setUp(self):
     self.variant = models.Variant.objects.get(id='standard')
     self.user = factories.UserFactory()
     self.england = self.variant.nations.get(name='England')
     self.france = self.variant.nations.get(name='France')
     self.london = self.variant.territories.get(name='London')
     self.paris = self.variant.territories.get(name='Paris')
     self.game = models.Game.objects.create(
         name='Test game',
         variant=self.variant,
         num_players=7,
         created_by=self.user,
     )
     self.game.participants.add(self.user)
     self.turn = models.Turn.objects.create(
         game=self.game,
         phase=Phase.ORDER,
         season=Season.SPRING,
         year=1901,
     )
     self.territory_state = self.create_test_territory_state(
         territory=self.london,
         turn=self.turn,
     )
     self.order = self.create_test_order(
         nation=self.england,
         turn=self.turn,
         source=self.london,
     )
     london_piece = self.create_test_piece(game=self.game,
                                           nation=self.england)
     self.piece_state = self.create_test_piece_state(
         piece=london_piece,
         territory=self.london,
         turn=self.turn,
     )
Exemple #18
0
 def setUp(self):
     user = factories.UserFactory()
     self.client.force_authenticate(user=user)
Exemple #19
0
 def setUp(self):
     self.variant = models.Variant.objects.get(id='standard')
     self.user = factories.UserFactory()
     self.game = self.create_test_game()
     self.game.participants.add(self.user)
     self.patch_process_turn_apply_async()
Exemple #20
0
 def setUp(self):
     self.variant = factories.StandardVariantFactory()
     self.user = factories.UserFactory()
     self.game_a = models.Game.objects.create(
         name='Test game A',
         variant=self.variant,
         num_players=7,
         created_by=self.user,
     )
     self.game_b = models.Game.objects.create(
         name='Test game B',
         variant=self.variant,
         num_players=7,
         created_by=self.user,
     )
     self.game_a_spring_order_turn = models.Turn.objects.create(
         game=self.game_a,
         phase=Phase.ORDER,
         season=Season.SPRING,
         year=1901,
     )
     self.game_b_spring_order_turn = models.Turn.objects.create(
         game=self.game_b,
         phase=Phase.ORDER,
         season=Season.SPRING,
         year=1901,
     )
     self.game_a_spring_retreat_turn = models.Turn.objects.create(
         game=self.game_a,
         phase=Phase.RETREAT,
         season=Season.SPRING,
         year=1901,
     )
     self.game_b_spring_retreat_turn = models.Turn.objects.create(
         game=self.game_b,
         phase=Phase.RETREAT,
         season=Season.SPRING,
         year=1901,
     )
     self.game_a_fall_order_turn = models.Turn.objects.create(
         game=self.game_a,
         phase=Phase.ORDER,
         season=Season.FALL,
         year=1901,
     )
     self.game_b_fall_order_turn = models.Turn.objects.create(
         game=self.game_b,
         phase=Phase.ORDER,
         season=Season.FALL,
         year=1901,
     )
     self.game_a_fall_retreat_turn = models.Turn.objects.create(
         game=self.game_a,
         phase=Phase.RETREAT,
         season=Season.FALL,
         year=1901,
     )
     self.game_b_fall_retreat_turn = models.Turn.objects.create(
         game=self.game_b,
         phase=Phase.RETREAT,
         season=Season.FALL,
         year=1901,
     )
     self.game_a_fall_build_turn = models.Turn.objects.create(
         game=self.game_a,
         phase=Phase.BUILD,
         season=Season.FALL,
         year=1901,
     )
     self.game_b_fall_build_turn = models.Turn.objects.create(
         game=self.game_b,
         phase=Phase.BUILD,
         season=Season.FALL,
         year=1901,
     )
     self.game_a_spring_order_turn_1902 = models.Turn.objects.create(
         game=self.game_a,
         phase=Phase.ORDER,
         season=Season.SPRING,
         year=1902,
     )
     self.game_b_spring_order_turn_1902 = models.Turn.objects.create(
         game=self.game_b,
         phase=Phase.ORDER,
         season=Season.SPRING,
         year=1902,
     )