def test_edit_user_account_details(self): """ Tests that the edit user account details page is rendered correctly, and the endpoint correctly redirects on valid form data and renders the account details page. """ edit_data1 = {"username": "******"} edit_data2 = {"username": "******"} # Test account_edit.html with self.client as c: self.login({"email": "*****@*****.**", "password": "******"}) with captured_templates(self.app) as templates: response = c.get(url_for("users.edit_user_account_details")) template, context = templates[0] self.assertEqual(response.status_code, 200) self.assertEqual(template.name, "account_edit.html") self.assertIsInstance(context["form"], EditUserAccountForm) # Test status code for redirect response = c.post(url_for("users.edit_user_account_details"), data=edit_data1) self.assertEqual(response.status_code, 302) # Test when redirect is followed with captured_templates(self.app) as templates: response = c.post(url_for("users.edit_user_account_details"), data=edit_data2, follow_redirects=True) template, context = templates[0] self.assertEqual(response.status_code, 200) self.assertEqual(template.name, "account.html")
def test_login(self): """ Tests that the login page is rendered correctly, and the login endpoint correctly redirects valid logins and renders the dashboard page. """ # Test login.html with self.client as c: with captured_templates(self.app) as templates: response = c.get(url_for("auth.login")) template, context = templates[0] self.assertEqual(response.status_code, 200) self.assertEqual(template.name, "login.html") self.assertIsInstance(context["form"], LogInForm) # Test status code for redirect response = self.login({ "email": "*****@*****.**", "password": "******" }) self.assertEqual(response.status_code, 302) self.logout() # Test when redirect is followed with captured_templates(self.app) as templates: response = self.login_follow({ "email": "*****@*****.**", "password": "******" }) template, context = templates[0] self.assertEqual(response.status_code, 200) self.assertEqual(template.name, "dashboard.html")
def test_view_pokemon_move(self): """ Tests that the move view page is rendered correctly. """ with self.client as c: # Test the view of a pokemon's move on a users team move = random.choice(Pokemon_Moves.query.all()) team_pokemon = Teams_Pokemon.query.filter_by(id=move.team_pokemon_id).first() self.login({"email": f"test{team_pokemon.team.owner_id}@test.com", "password": "******"}) with captured_templates(self.app) as templates: response = c.get(url_for("moves.view_pokemon_move", team_id=team_pokemon.team_id, team_index=team_pokemon.team_index, pokemon_move_index=move.pokemon_move_index)) template, context = templates[0] self.assertEqual(response.status_code, 200) self.assertEqual(template.name, "move_view.html") self.assertIsInstance(context["form"], RemoveMoveForm) self.assertIn(b"Change Move", response.data) self.assertIn(b"Remove Move", response.data) self.assertIn(bytes(team_pokemon.team.name, "utf-8"), response.data) self.logout() # Test the view move endpoint redirects to the move_list template when trying to view an empty move slot team_pokemon, move_index = self.get_empty_move_slot() self.login({"email": f"test{team_pokemon.team.owner_id}@test.com", "password": "******"}) response = c.get(url_for("moves.view_pokemon_move", team_id=team_pokemon.team_id, team_index=team_pokemon.team_index, pokemon_move_index=move_index)) self.assertEqual(response.status_code, 302) with captured_templates(self.app) as templates: response = c.get(url_for("moves.view_pokemon_move", team_id=team_pokemon.team_id, team_index=team_pokemon.team_index, pokemon_move_index=move_index), follow_redirects=True) template, context = templates[0] self.assertEqual(response.status_code, 200) self.assertEqual(template.name, "move_select.html") self.logout() # Test the view of a pokemon's move in a public team by another user or with anonymous user team_pokemon, move = self.get_move_slot_public() with captured_templates(self.app) as templates: response = c.get(url_for("moves.view_pokemon_move", team_id=team_pokemon.team_id, team_index=team_pokemon.team_index, pokemon_move_index=move.pokemon_move_index)) template, context = templates[0] self.assertEqual(response.status_code, 200) self.assertEqual(template.name, "move_view.html") self.assertNotIn(b"Change Move", response.data) self.assertNotIn(b"Remove Move", response.data) self.assertIn(bytes(team_pokemon.team.name, "utf-8"), response.data)
def test_edit_team_details(self): """ Tests that the edit team page is rendered correctly, and the endpoint correctly redirects on valid form data and renders the team view page. """ # "" is a false value for the boolean field edit_data1 = { "team_name": "edit 1", "description": "testing editing team details", "is_private": "" } edit_data2 = { "team_name": "edit 2", "description": "testing editing team details", "is_private": True } # Test team_edit.html with self.client as c: team = random.choice(Team.query.all()) self.login({ "email": f"test{team.owner_id}@test.com", "password": "******" }) with captured_templates(self.app) as templates: response = c.get( url_for("teams.edit_team_details", team_id=team.id)) template, context = templates[0] self.assertEqual(response.status_code, 200) self.assertEqual(template.name, "team_edit.html") self.assertIsInstance(context["form"], EditTeamForm) # Test status code for redirect response = c.post(url_for("teams.edit_team_details", team_id=team.id), data=edit_data1) self.assertEqual(response.status_code, 302) # Test when redirect is followed with captured_templates(self.app) as templates: response = c.post(url_for("teams.edit_team_details", team_id=team.id), data=edit_data2, follow_redirects=True) template, context = templates[0] self.assertEqual(response.status_code, 200) self.assertEqual(template.name, "team_view.html")
def test_view_team_pokemon(self): """ Tests that the team version of the pokemon view page is rendered correctly. """ with self.client as c: # Test the view of a pokemon on a users team team_pokemon = self.get_random_team_pokemon() self.login({ "email": f"test{team_pokemon.team.owner_id}@test.com", "password": "******" }) with captured_templates(self.app) as templates: response = c.get( url_for("pokemon.view_team_pokemon", team_id=team_pokemon.team_id, team_index=team_pokemon.team_index)) template, context = templates[0] self.assertEqual(response.status_code, 200) self.assertEqual(template.name, "pokemon_view.html") self.assertIsInstance(context["form"], RemovePokemonForm) self.assertIn(b"Change Pokemon", response.data) if b"Name: Empty" not in response.data: self.assertIn(b"Remove Pokemon", response.data) self.assertIn(bytes(team_pokemon.team.name, "utf-8"), response.data) self.logout() # Test the view of a pokemon in a public team by another user or with anonymous user team_pokemon = self.get_random_team_pokemon_public() with captured_templates(self.app) as templates: response = c.get( url_for("pokemon.view_team_pokemon", team_id=team_pokemon.team_id, team_index=team_pokemon.team_index)) template, context = templates[0] self.assertEqual(response.status_code, 200) self.assertEqual(template.name, "pokemon_view.html") self.assertIn(bytes(team_pokemon.team.owner.username, "utf-8"), response.data) self.assertNotIn(b"Change Pokemon", response.data) self.assertNotIn(b"Remove Pokemon", response.data) self.assertIn(bytes(team_pokemon.team.name, "utf-8"), response.data)
def test_edit_team_slot_pokemon(self): """ Tests that the edit team slot pokemon endpoint correctly redirects on confirming a pokemon change and renders the team pokemon view page for that pokemon. """ # Test status code for redirect with self.client as c: team_pokemon = self.get_random_team_pokemon() self.login({ "email": f"test{team_pokemon.team.owner_id}@test.com", "password": "******" }) response = c.post( url_for("pokemon.edit_team_slot_pokemon", team_id=team_pokemon.team_id, team_index=team_pokemon.team_index, pokeapi_id=random.randint(1, 898))) self.assertEqual(response.status_code, 302) # Test when redirect is followed with captured_templates(self.app) as templates: response = c.post(url_for("pokemon.edit_team_slot_pokemon", team_id=team_pokemon.team_id, team_index=team_pokemon.team_index, pokeapi_id=random.randint(1, 898)), follow_redirects=True) template, context = templates[0] self.assertEqual(response.status_code, 200) self.assertEqual(template.name, "pokemon_view.html")
def test_view_selected_team_pokemon(self): """ Tests that the pokemon view page for the selected pokemon is rendered correctly. """ with self.client as c: team_pokemon = self.get_random_team_pokemon() self.login({ "email": f"test{team_pokemon.team.owner_id}@test.com", "password": "******" }) with captured_templates(self.app) as templates: response = c.get( url_for("pokemon.view_selected_team_pokemon", team_id=team_pokemon.team_id, team_index=team_pokemon.team_index, pokeapi_id=random.randint(1, 898))) template, context = templates[0] self.assertEqual(response.status_code, 200) self.assertEqual(template.name, "pokemon_view.html") self.assertIsInstance(context["form"], ConfirmForm) self.assertIn(b"Confirm", response.data) self.assertIn(bytes(team_pokemon.team.name, "utf-8"), response.data)
def test_delete_pokemon_move_slot(self): """ Tests that the delete pokemon move slot endpoint correctly redirects and renders the pokemon view page. """ # Test status code for redirect with self.client as c: move = random.choice(Pokemon_Moves.query.all()) team_pokemon = Teams_Pokemon.query.filter_by(id=move.team_pokemon_id).first() self.login({"email": f"test{team_pokemon.team.owner_id}@test.com", "password": "******"}) response = c.post(url_for("moves.delete_pokemon_move_slot", team_id=team_pokemon.team_id, team_index=team_pokemon.team_index, pokemon_move_index=move.pokemon_move_index)) self.assertEqual(response.status_code, 302) self.logout() # Test when redirect is followed move = random.choice(Pokemon_Moves.query.all()) team_pokemon = Teams_Pokemon.query.filter_by(id=move.team_pokemon_id).first() self.login({"email": f"test{team_pokemon.team.owner_id}@test.com", "password": "******"}) with captured_templates(self.app) as templates: response = c.post(url_for("moves.delete_pokemon_move_slot", team_id=team_pokemon.team_id, team_index=team_pokemon.team_index, pokemon_move_index=move.pokemon_move_index), follow_redirects=True) template, context = templates[0] self.assertEqual(response.status_code, 200) self.assertEqual(template.name, "pokemon_view.html")
def test_delete_team(self): """ Tests that the delete team endpoint correctly redirects and renders the my teams page. """ # Test status code for redirect with self.client as c: team = random.choice(Team.query.all()) self.login({ "email": f"test{team.owner_id}@test.com", "password": "******" }) response = c.post(url_for("teams.delete_team", team_id=team.id)) self.assertEqual(response.status_code, 302) self.logout() # Test when redirect is followed team = random.choice(Team.query.all()) self.login({ "email": f"test{team.owner_id}@test.com", "password": "******" }) with captured_templates(self.app) as templates: response = c.post(url_for("teams.delete_team", team_id=team.id), follow_redirects=True) template, context = templates[0] self.assertEqual(response.status_code, 200) self.assertEqual(template.name, "team_list.html") self.assertIn(b"My Pokemon Teams", response.data)
def test_create_team(self): """ Tests that the create team page is rendered correctly, and the endpoint correctly redirects on valid form data and renders the team view page. """ # "" is a false value for the boolean field team_data1 = { "team_name": "test 1", "description": "first team testing creation", "is_private": "" } team_data2 = { "team_name": "test 2", "description": "second team testing creation", "is_private": True } # Test team_create.html with self.client as c: self.login({ "email": f"test{random.randint(1, 5)}@test.com", "password": "******" }) with captured_templates(self.app) as templates: response = c.get(url_for("teams.create_team")) template, context = templates[0] self.assertEqual(response.status_code, 200) self.assertEqual(template.name, "team_create.html") self.assertIsInstance(context["form"], CreateTeamForm) # Test status code for redirect response = c.post(url_for("teams.create_team"), data=team_data1) self.assertEqual(response.status_code, 302) # Test when redirect is followed with captured_templates(self.app) as templates: response = c.post(url_for("teams.create_team"), data=team_data2, follow_redirects=True) template, context = templates[0] self.assertEqual(response.status_code, 200) self.assertEqual(template.name, "team_view.html")
def test_signup(self): """ Tests that the signup page is rendered correctly, and the signup endpoint correctly redirects valid signups and renders the dashboard page. """ signup_data1 = { "username": "******", "email": "*****@*****.**", "password": "******", "confirm_password": "******" } signup_data2 = { "username": "******", "email": "*****@*****.**", "password": "******", "confirm_password": "******" } # Test signup.html with self.client as c: with captured_templates(self.app) as templates: response = c.get(url_for("auth.signup")) template, context = templates[0] self.assertEqual(response.status_code, 200) self.assertEqual(template.name, "signup.html") self.assertIsInstance(context["form"], SignUpForm) # Test status code for redirect response = c.post(url_for("auth.signup"), data=signup_data1) self.assertEqual(response.status_code, 302) # Test when redirect is followed with captured_templates(self.app) as templates: response = c.post(url_for("auth.signup"), data=signup_data2, follow_redirects=True) template, context = templates[0] self.assertEqual(response.status_code, 200) self.assertEqual(template.name, "dashboard.html")
def test_landing_page(self): """ Tests that the landing page is rendered correctly. """ with self.client as c: with captured_templates(self.app) as templates: response = c.get(url_for("auth.landing_page")) template, context = templates[0] self.assertEqual(response.status_code, 200) self.assertEqual(template.name, "landing.html")
def test_get_public_teams(self): """ Tests that the logic for populating empty pokemon slots is correctly returning data with the correct size. """ with self.client as c: with captured_templates(self.app) as templates: c.get(url_for("teams.get_public_teams")) template, context = templates[0] for team in context["teams"]: self.assertEqual(len(team["team_pokemon"]), 6)
def test_get_view_pokemon_list(self): """ Tests that the pokedex version of the pokemon list page is rendered correctly. """ with self.client as c: with captured_templates(self.app) as templates: response = c.get(url_for("pokemon.get_view_pokemon_list")) template, context = templates[0] self.assertEqual(response.status_code, 200) self.assertEqual(template.name, "pokemon_select.html") self.assertNotIn(b"Slot", response.data)
def test_get_public_teams(self): """ Tests that the public teams page is rendered correctly. """ with self.client as c: with captured_templates(self.app) as templates: response = c.get(url_for("teams.get_public_teams")) template, context = templates[0] self.assertEqual(response.status_code, 200) self.assertEqual(template.name, "team_list.html") self.assertIn(b"Public Pokemon Teams", response.data)
def test_dashboard(self): """ Tests that the dashboard page is rendered correctly. """ with self.client as c: self.login({"email": "*****@*****.**", "password": "******"}) with captured_templates(self.app) as templates: response = c.get(url_for("users.dashboard")) template, context = templates[0] self.assertEqual(response.status_code, 200) self.assertEqual(template.name, "dashboard.html")
def test_view_selected_pokemon(self): """ Tests that the pokedex version of the pokemon view page is rendered correctly. """ with self.client as c: with captured_templates(self.app) as templates: response = c.get( url_for("pokemon.view_selected_pokemon", pokeapi_id=random.randint(1, 898))) template, context = templates[0] self.assertEqual(response.status_code, 200) self.assertEqual(template.name, "pokemon_view.html") self.assertIn(b"Pokedex Entry", response.data)
def test_get_user_account_details(self): """ Tests that the user account details page is rendered correctly. """ with self.client as c: self.login({"email": "*****@*****.**", "password": "******"}) with captured_templates(self.app) as templates: response = c.get(url_for("users.get_user_account_details")) template, context = templates[0] self.assertEqual(response.status_code, 200) self.assertEqual(template.name, "account.html") self.assertIsInstance(context["form"], DeleteUserAccountForm)
def test_get_team(self): """ Tests that the logic for populating empty pokemon and move slots is correctly returning data with the correct size. """ with self.client as c: team = random.choice(Team.query.filter_by(is_private=False).all()) with captured_templates(self.app) as templates: c.get(url_for("teams.get_team", team_id=team.id)) template, context = templates[0] self.assertEqual(len(context["team"]["team_pokemon"]), 6) self.assertEqual(len(context["move_sets"]), 6) for move_set in context["move_sets"]: if move_set: self.assertEqual(len(move_set), 4)
def test_view_team_pokemon(self): """ Tests that the logic for populating empty move slots is correctly returning data with the correct size. """ with self.client as c: team_pokemon = self.get_random_team_pokemon_public() with captured_templates(self.app) as templates: c.get( url_for("pokemon.view_team_pokemon", team_id=team_pokemon.team_id, team_index=team_pokemon.team_index)) template, context = templates[0] if context["moves"]: self.assertEqual(len(context["moves"]), 4)
def test_get_pokemon_move_list(self): """ Tests that the move list page is rendered correctly. """ with self.client as c: team_pokemon = self.get_random_team_pokemon() self.login({"email": f"test{team_pokemon.team.owner_id}@test.com", "password": "******"}) with captured_templates(self.app) as templates: response = c.get(url_for("moves.get_pokemon_move_list", team_id=team_pokemon.team_id, team_index=team_pokemon.team_index, pokemon_move_index=random.randint(1, 4))) template, context = templates[0] self.assertEqual(response.status_code, 200) self.assertEqual(template.name, "move_select.html") self.assertIn(bytes(team_pokemon.team.name, "utf-8"), response.data) self.assertIn(bytes(team_pokemon.pokemon.pokemon_name.capitalize(), "utf-8"), response.data)
def test_get_users_teams(self): """ Tests that the my teams page is rendered correctly. """ with self.client as c: self.login({ "email": f"test{random.randint(1, 5)}@test.com", "password": "******" }) with captured_templates(self.app) as templates: response = c.get(url_for("teams.get_users_teams")) template, context = templates[0] self.assertEqual(response.status_code, 200) self.assertEqual(template.name, "team_list.html") self.assertIn(b"My Pokemon Teams", response.data)
def test_delete_user_account(self): """ Tests that the delete user account endpoint correctly redirects and renders the landing page. """ # Test status code for redirect with self.client as c: self.login({"email": "*****@*****.**", "password": "******"}) response = c.post(url_for("users.delete_user_account")) self.assertEqual(response.status_code, 302) # Test when redirect is followed self.login({"email": "*****@*****.**", "password": "******"}) with captured_templates(self.app) as templates: response = c.post(url_for("users.delete_user_account"), follow_redirects=True) template, context = templates[0] self.assertEqual(response.status_code, 200) self.assertEqual(template.name, "landing.html")
def test_delete_team_slot_pokemon(self): """ Tests that the delete team slot pokemon endpoint correctly redirects and renders the team view page. """ # Test status code for redirect with self.client as c: team_pokemon = self.get_random_team_pokemon() self.login({ "email": f"test{team_pokemon.team.owner_id}@test.com", "password": "******" }) response = c.post( url_for("pokemon.delete_team_slot_pokemon", team_id=team_pokemon.team_id, team_index=team_pokemon.team_index)) self.assertEqual(response.status_code, 302) self.logout() # Test when redirect is followed team_pokemon = self.get_random_team_pokemon() self.login({ "email": f"test{team_pokemon.team.owner_id}@test.com", "password": "******" }) with captured_templates(self.app) as templates: response = c.post(url_for("pokemon.delete_team_slot_pokemon", team_id=team_pokemon.team_id, team_index=team_pokemon.team_index), follow_redirects=True) template, context = templates[0] self.assertEqual(response.status_code, 200) self.assertEqual(template.name, "team_view.html") self.assertIn(bytes(team_pokemon.team.name, "utf-8"), response.data)
def test_logout(self): """ Tests that the logout endpoint correctly redirects and renders the landing page. """ # Test status code for redirect with self.client: self.login({"email": "*****@*****.**", "password": "******"}) response = self.logout() self.assertEqual(response.status_code, 302) # Test when redirect is followed self.login({"email": "*****@*****.**", "password": "******"}) with captured_templates(self.app) as templates: response = self.logout_follow() template, context = templates[0] self.assertEqual(response.status_code, 200) self.assertEqual(template.name, "landing.html") self.assertIn(b"You have been successfully logged out.", response.data)
def test_get_team_pokemon_list(self): """ Tests that the team version of the pokemon list page is rendered correctly. """ with self.client as c: team_pokemon = self.get_random_team_pokemon() self.login({ "email": f"test{team_pokemon.team.owner_id}@test.com", "password": "******" }) with captured_templates(self.app) as templates: response = c.get( url_for("pokemon.get_team_pokemon_list", team_id=team_pokemon.team_id, team_index=team_pokemon.team_index)) template, context = templates[0] self.assertEqual(response.status_code, 200) self.assertEqual(template.name, "pokemon_select.html") self.assertIn(bytes(team_pokemon.team.name, "utf-8"), response.data) self.assertIn(b"Slot", response.data)
def test_get_team(self): """ Tests that the team view page is rendered correctly. """ with self.client as c: # Test the team view of a users private team team = random.choice(Team.query.filter_by(is_private=True).all()) self.login({ "email": f"test{team.owner_id}@test.com", "password": "******" }) with captured_templates(self.app) as templates: response = c.get(url_for("teams.get_team", team_id=team.id)) template, context = templates[0] self.assertEqual(response.status_code, 200) self.assertEqual(template.name, "team_view.html") self.assertIsInstance(context["form"], DeleteTeamForm) self.assertIn(b"Private", response.data) self.assertIn(b"Edit Team Details", response.data) self.assertIn(b"Delete Team", response.data) self.assertIn(bytes(team.name, "utf-8"), response.data) self.logout() # Test the team view of a users public team team = random.choice(Team.query.filter_by(is_private=False).all()) self.login({ "email": f"test{team.owner_id}@test.com", "password": "******" }) with captured_templates(self.app) as templates: response = c.get(url_for("teams.get_team", team_id=team.id)) template, context = templates[0] self.assertEqual(response.status_code, 200) self.assertEqual(template.name, "team_view.html") self.assertIsInstance(context["form"], DeleteTeamForm) self.assertIn(b"Public", response.data) self.assertIn(b"Edit Team Details", response.data) self.assertIn(b"Delete Team", response.data) self.assertIn(bytes(team.name, "utf-8"), response.data) self.logout() # Test the team view of a public team by another user or anonymous user team = random.choice(Team.query.filter_by(is_private=False).all()) with captured_templates(self.app) as templates: response = c.get(url_for("teams.get_team", team_id=team.id)) template, context = templates[0] self.assertEqual(response.status_code, 200) self.assertEqual(template.name, "team_view.html") self.assertNotIn(b"Private", response.data) self.assertIn(bytes(team.owner.username, "utf-8"), response.data) self.assertNotIn(b"Edit Team Details", response.data) self.assertNotIn(b"Delete Team", response.data) self.assertIn(bytes(team.name, "utf-8"), response.data)