1

I want to write a unit test than checks where I was redirected.

There are 2 behaviours depending on number of players in a room. I want to check this function:

@login_required
def game_join(request, id):
    game = Game.objects.get(pk=id)
    if game.players_ready == game.max_players:
        return redirect('home',name="full")
    else:
        return redirect('detail', game.id)

Test:

def test_join_empty_room(self):

game = Game.objects.create(name='empty',host='dottore',is_played=False,max_players=4,players_ready=0)
self.join_url_empty = reverse('game_join', args=[game.id])
response = self.client.get(self.join_url_empty, follow=True)
print(response.redirect_chain)

self.assertEquals(response.status_code,200)

Test answer is ok.

Redirect chain: [('/player/login?next=/game/1/join', 302)]

So it gives me no info, since url patterns are like this:

path('<int:id>/', detail, name = 'detail'),
path('<int:id>/join', game_join, name = 'game_join'),

So correct answer should be sth like:

http://127.0.0.1:8000/game/64/

How to do that correctly?

DevVile
  • 59
  • 8
  • It does, since the path of the response contains the one that was visited, the last one can be obtained with `response.request['PATH_INFO']`. – Willem Van Onsem May 11 '20 at 15:23

1 Answers1

0

Your view requires the client to be logged in before you can access it. Here's how to log in the test client. self.client.login(username='username', password='password')

Airith
  • 2,024
  • 1
  • 14
  • 10