def test_recover_password(self): try: old_tokens = [password_token.token for password_token in PasswordToken.all()] self.assertEqual(len(old_tokens), 2) response = app.request("/recover/password", method="POST", data={"email" : UserData.franck_p.email}) #@UndefinedVariable self.assertEqual(response.status, HTTP_OK) self.assertIn(UserData.franck_p.email, response.data) new_password_token = config.orm.query(PasswordToken).filter(~PasswordToken.token.in_(old_tokens)).one() #@UndefinedVariable self.assertEquals(new_password_token.user, UserData.franck_p) self.assertFalse(new_password_token.expired) response = app.request("/recover/password", method="POST", data={"email" : UserData.franck_p.email}) #@UndefinedVariable self.assertEqual(response.status, HTTP_FORBIDDEN) self.assertIn("Demande similaire", response.data) finally: #TODO: should be done by the fixture new_password_token = config.orm.query(PasswordToken).filter(~PasswordToken.token.in_(old_tokens)).one() #@UndefinedVariable config.orm.delete(new_password_token) config.orm.commit()
def test_vote_expired(self): self.login() expired_poll = config.orm.query(Poll).filter(Poll.start_dt == datetime.date(2011, 5, 28)).one() #@UndefinedVariable self.assertTrue(expired_poll.expired) self.assertEqual(len(expired_poll.choices_by_user), 2) response = app.request("/poll/vote", method="POST", data=urllib.urlencode({"poll_id": expired_poll.id, "poll_user_choices": [0,1]}, True)) #@UndefinedVariable self.assertEqual(response.status, HTTP_FORBIDDEN) self.assertEqual(response.data, u"Le sondage a expire") response = app.request("/poll/vote", method="POST", data=urllib.urlencode({"poll_id": expired_poll.id, "poll_user_choices": [0]}, True)) #@UndefinedVariable self.assertEqual(response.status, HTTP_FORBIDDEN) self.assertEqual(response.data, u"Le sondage a expire") response = app.request("/poll/vote", method="POST", data=urllib.urlencode({"poll_id": expired_poll.id, "poll_user_choices": []}, True)) #@UndefinedVariable self.assertEqual(response.status, HTTP_FORBIDDEN) self.assertEqual(response.data, u"Le sondage a expire") response = app.request("/poll/vote", method="POST", data=urllib.urlencode({"poll_id": expired_poll.id, "poll_user_choices": []}, True)) #@UndefinedVariable self.assertEqual(response.status, HTTP_FORBIDDEN) self.assertEqual(response.data, u"Le sondage a expire") self.assertEqual(len(expired_poll.choices_by_user), 2)
def test_reset_password_POST(self): NEW_PASSWORD = "******" updated_user = User( first_name=UserData.jo.first_name, last_name=UserData.jo.last_name, pseudonym=UserData.jo.pseudonym, email=UserData.jo.email, password=to_md5(NEW_PASSWORD), level=UserData.jo.level ) db_user = config.orm.query(User).filter(User.first_name == "Jonathan").one() #@UndefinedVariable updated_user_data = { "token" : PasswordTokenData.password_token_active.token, "User-4-new_password" : NEW_PASSWORD, "User-4-new_password_confirm" : NEW_PASSWORD } # Makes sure that the update fails with invalid data (1) invalid_user_data = copy.deepcopy(updated_user_data) invalid_user_data["User-4-new_password"] = "" response = app.request("/reset/password", method="POST", data=invalid_user_data) #@UndefinedVariable self.assertEqual(response.status, HTTP_OK) self.assertIn("Please enter a value", response.data) self.assertIn("Passwords do not match", response.data) self.assertEqual(db_user, UserData.jo) self.assertNotEqual(db_user, updated_user) # Makes sure that the update fails with invalid data (2) invalid_user_data = copy.deepcopy(updated_user_data) invalid_user_data["User-4-new_password"] = "******" response = app.request("/reset/password", method="POST", data=invalid_user_data) #@UndefinedVariable self.assertEqual(response.status, HTTP_OK) self.assertIn("Value must be at least 4 characters long", response.data) self.assertIn("Passwords do not match", response.data) self.assertEqual(db_user, UserData.jo) self.assertNotEqual(db_user, updated_user) # Makes sure that the update fails with invalid data (3) invalid_user_data = copy.deepcopy(updated_user_data) invalid_user_data["User-4-new_password"] = NEW_PASSWORD + "a" response = app.request("/reset/password", method="POST", data=invalid_user_data) #@UndefinedVariable self.assertEqual(response.status, HTTP_OK) self.assertNotIn("Value must be at least 4 characters long", response.data) self.assertIn("Passwords do not match", response.data) self.assertEqual(db_user, UserData.jo) self.assertNotEqual(db_user, updated_user) # Makes sure that the update works with valid data response = app.request("/reset/password", method="POST", data=updated_user_data) #@UndefinedVariable self.assertEqual(response.status, HTTP_SEE_OTHER) self.assertEqual(db_user, updated_user) self.assertNotEqual(db_user, UserData.jo)
def test_create_account_POST(self): try: old_user_emails = [user.email for user in User.all()] self.assertEqual(len(old_user_emails), 7) NEW_PASSWORD = "******" expected_new_user = User( first_name="Dorian", last_name="Gray", pseudonym="Dorian G", email=UserTokenData.user_token_active.email, password=to_md5(NEW_PASSWORD), level=UserTokenData.user_token_active.level ) new_user_data = { "token" : UserTokenData.user_token_active.token, "User--pseudonym" : expected_new_user.pseudonym, "User--first_name" : expected_new_user.first_name, "User--last_name" : expected_new_user.last_name, "User--new_email" : expected_new_user.email, "User--new_password" : NEW_PASSWORD, "User--new_password_confirm" : NEW_PASSWORD } # Makes sure that the insert fails with invalid data alterable_keys = [key for key in new_user_data.keys() if key != "token"] for key in alterable_keys: # Alters the submitted data invalid_user_data = copy.deepcopy(new_user_data) invalid_user_data[key] = "" # Makes sure the server rejects the insert response = app.request("/create/account", method="POST", data=invalid_user_data) #@UndefinedVariable self.assertEqual(response.status, HTTP_OK) self.assertIn("Please enter a value", response.data) self.assertEqual(len(User.all()), len(old_user_emails)) self.assertRaises(NoResultFound, config.orm.query(User).filter(~User.email.in_(old_user_emails)).one) #@UndefinedVariable # Makes sure that the insert works with valid data response = app.request("/create/account", method="POST", data=new_user_data) #@UndefinedVariable self.assertEqual(response.status, HTTP_SEE_OTHER) self.assertEqual(len(User.all()), 1 + len(old_user_emails)) new_user = config.orm.query(User).filter(~User.email.in_(old_user_emails)).one() #@UndefinedVariable self.assertEquals(new_user, expected_new_user) finally: #TODO: should be done by the fixture new_user = config.orm.query(User).filter(~User.email.in_(old_user_emails)).one() #@UndefinedVariable config.orm.delete(new_user) config.orm.commit()
def test_vote_expired(self): self.login() expired_poll = config.orm.query(Poll).filter( Poll.start_dt == datetime.date(2011, 5, 28)).one() #@UndefinedVariable self.assertTrue(expired_poll.expired) self.assertEqual(len(expired_poll.choices_by_user), 2) response = app.request("/poll/vote", method="POST", data=urllib.urlencode( { "poll_id": expired_poll.id, "poll_user_choices": [0, 1] }, True)) #@UndefinedVariable self.assertEqual(response.status, HTTP_FORBIDDEN) self.assertEqual(response.data, u"Le sondage a expire") response = app.request("/poll/vote", method="POST", data=urllib.urlencode( { "poll_id": expired_poll.id, "poll_user_choices": [0] }, True)) #@UndefinedVariable self.assertEqual(response.status, HTTP_FORBIDDEN) self.assertEqual(response.data, u"Le sondage a expire") response = app.request("/poll/vote", method="POST", data=urllib.urlencode( { "poll_id": expired_poll.id, "poll_user_choices": [] }, True)) #@UndefinedVariable self.assertEqual(response.status, HTTP_FORBIDDEN) self.assertEqual(response.data, u"Le sondage a expire") response = app.request("/poll/vote", method="POST", data=urllib.urlencode( { "poll_id": expired_poll.id, "poll_user_choices": [] }, True)) #@UndefinedVariable self.assertEqual(response.status, HTTP_FORBIDDEN) self.assertEqual(response.data, u"Le sondage a expire") self.assertEqual(len(expired_poll.choices_by_user), 2)
def test_add_comment_existing(self): try: poll_1 = config.orm.query(Poll).filter(Poll.start_dt == datetime.date(2012, 4, 5)).one() #@UndefinedVariable poll_2 = config.orm.query(Poll).filter(Poll.start_dt == datetime.date(2011, 5, 28)).one() #@UndefinedVariable poll_3 = config.orm.query(Poll).filter(Poll.start_dt == datetime.date(2020, 10, 1)).one() #@UndefinedVariable self.assertEqual(len(poll_1.comments), 2) self.assertEqual(len(poll_2.comments), 1) self.assertEqual(len(poll_3.comments), 0) self.login() response = app.request("/poll/comment", method="POST", data={"poll_id": poll_1.id, "comment": "Sondage : salut les copains"}) #@UndefinedVariable self.assertEqual(response.status, HTTP_OK) self.assertIn("Sondage : salut les copains", response.data) self.assertEqual(len(poll_1.comments), 3) self.assertEqual(len(poll_2.comments), 1) self.assertEqual(len(poll_3.comments), 0) finally: #TODO: should be done by the fixture config.orm.delete(poll_1.comments[2]) config.orm.commit()
def test_logout(self): self.login() self.assertEquals(config.session_manager.user, UserData.franck_l) response = app.request("/logout") #@UndefinedVariable self.assertEqual(response.status, HTTP_SEE_OTHER) self.assertIsNone(config.session_manager.user)
def test_admin_results_POST_notadmin(self): self.login("*****@*****.**", "secret1") response = app.request("/admin/results", method="POST", data={"tournament_id": "1"}) #@UndefinedVariable self.assertEqual(response.status, HTTP_FORBIDDEN)
def test_update_status_existing(self): tournament_11 = config.orm.query(Tournament).filter(Tournament.tournament_dt == datetime.date(2009, 9, 1)).one() #@UndefinedVariable self.assertEqual(len(tournament_11.results), 5) self.assertTrue(config.orm.query(Result).filter(Result.tournament_id == 1).filter(Result.user_id == 1).one()) #@UndefinedVariable self.login("*****@*****.**", "secret1") response = app.request("/update/status", method="POST", data={"tournament_id" : 1, "status" : Result.STATUSES.M}) #@UndefinedVariable self.assertEqual(response.status, HTTP_OK) self.assertEqual(len(tournament_11.results), 5) updated_row = config.orm.query(Result).filter(Result.tournament_id == 1).filter(Result.user_id == 1).one() #@UndefinedVariable self.assertEquals(updated_row.status, Result.STATUSES.M) # Checks that the JSON response is well-formed decoded_json_response = json.loads(response.data) self.assertEqual(len(decoded_json_response), 2) self.assertIn("statistics", decoded_json_response) self.assertIn("results", decoded_json_response) # Checks the statistics statistics = decoded_json_response["statistics"] self.assertIn("<td id=\"mutable_nb_attending_players\">3</td>", statistics) self.assertIn("<td id=\"mutable_players_M\">%s,%s</td>" % (UserData.fx.pseudonym, UserData.franck_l.pseudonym), statistics) self.assertIn(u"<td id=\"mutable_sum_in_play\">40 €</td>", statistics) # Checks the results results = decoded_json_response["results"] self.assertNotIn(UserData.franck_l.pseudonym, results) self.assertIn(UserData.franck_p.pseudonym, results) self.assertIn(UserData.jo.pseudonym, results) self.assertIn(UserData.nico.pseudonym, results) self.assertNotIn(UserData.fx.pseudonym, results) self.assertNotIn(UserData.rolland.pseudonym, results) self.assertNotIn("</button>", results)
def test_add_comment_new(self): poll_1 = config.orm.query(Poll).filter(Poll.start_dt == datetime.date( 2012, 4, 5)).one() #@UndefinedVariable poll_2 = config.orm.query(Poll).filter(Poll.start_dt == datetime.date( 2011, 5, 28)).one() #@UndefinedVariable poll_3 = config.orm.query(Poll).filter(Poll.start_dt == datetime.date( 2020, 10, 1)).one() #@UndefinedVariable self.assertEqual(len(poll_1.comments), 2) self.assertEqual(len(poll_2.comments), 1) self.assertEqual(len(poll_3.comments), 0) self.login() response = app.request("/poll/comment", method="POST", data={ "poll_id": poll_3.id, "comment": "Sondage : salut les copains" }) #@UndefinedVariable self.assertEqual(response.status, HTTP_OK) self.assertIn("Sondage : salut les copains", response.data) self.assertEqual(len(poll_1.comments), 2) self.assertEqual(len(poll_2.comments), 1) self.assertEqual(len(poll_3.comments), 1)
def test_admin_sessions_GET(self): self.login("*****@*****.**", "secret2") response = app.request("/admin/sessions") #@UndefinedVariable self.assertEqual(response.status, HTTP_OK) self.assertIn(SessionData.session.session_id, response.data) self.assertIn("ip:127.0.0.1", response.data) self.assertIn("Modifier", response.data) self.assertNotIn("Créer", response.data)
def test_view(self): self.login() response = app.request("/tournament/2/1") #@UndefinedVariable self.assertEqual(response.status, HTTP_OK) self.assertIn( "Tournoi 1 (<span class=\"primary_label\">dimanche 01 août 2010</span>", response.data)
def test_admin_users_GET(self): self.login("*****@*****.**", "secret2") response = app.request("/admin/users") #@UndefinedVariable self.assertEqual(response.status, HTTP_OK) for user in (UserData.franck_l, UserData.franck_p, UserData.fx, UserData.jo, UserData.nico, UserData.rolland, UserData.zoe): self.assertIn(user.email, response.data) self.assertIn("Modifier", response.data) self.assertIn("Créer", response.data)
def test_add_comment_new(self): tournament_11 = config.orm.query(Tournament).filter(Tournament.tournament_dt == datetime.date(2009, 9, 1)).one() #@UndefinedVariable self.assertEqual(len(tournament_11.comments), 0) self.login() response = app.request("/add/comment", method="POST", data={"tournament_id" : 1, "comment" : "Salut les copains"}) #@UndefinedVariable self.assertEqual(response.status, HTTP_OK) self.assertIn("Salut les copains", response.data) self.assertEqual(len(tournament_11.comments), 1)
def test_vote_invalid_choices(self): self.login("*****@*****.**", "secret4") poll_3 = config.orm.query(Poll).filter(Poll.start_dt == datetime.date(2020, 10, 1)).one() #@UndefinedVariable self.assertFalse(poll_3.expired) self.assertEqual(len(poll_3.choices_by_user), 0) response = app.request("/poll/vote", method="POST", data=urllib.urlencode({"poll_id": poll_3.id, "poll_user_choices": [0,2]}, True)) #@UndefinedVariable self.assertEqual(response.status, HTTP_FORBIDDEN) self.assertEqual(response.data, u"Un des entiers passes a la methode /poll/vote n'est pas compris dans l'intervalle [0, 1]") response = app.request("/poll/vote", method="POST", data=urllib.urlencode({"poll_id": poll_3.id, "poll_user_choices": [3]}, True)) #@UndefinedVariable self.assertEqual(response.status, HTTP_FORBIDDEN) self.assertEqual(response.data, u"Un des entiers passes a la methode /poll/vote n'est pas compris dans l'intervalle [0, 1]") self.assertEqual(len(poll_3.choices_by_user), 0)
def test_update_status_new(self): try: tournament_11 = config.orm.query(Tournament).filter( Tournament.tournament_dt == datetime.date( 2009, 9, 1)).one() #@UndefinedVariable self.assertEqual(len(tournament_11.results), 5) self.assertFalse( config.orm.query(Result).filter( Result.tournament_id == 1).filter( Result.user_id == 6).all()) #@UndefinedVariable self.login("*****@*****.**", "secret6") response = app.request("/update/status", method="POST", data={ "tournament_id": 1, "status": Result.STATUSES.P }) #@UndefinedVariable self.assertEqual(response.status, HTTP_OK) self.assertEqual(len(tournament_11.results), 6) inserted_row = config.orm.query(Result).filter( Result.tournament_id == 1).filter( Result.user_id == 6).one() #@UndefinedVariable self.assertEquals(inserted_row.status, Result.STATUSES.P) # Checks that the JSON response is well-formed decoded_json_response = json.loads(response.data) self.assertEqual(len(decoded_json_response), 2) self.assertIn("statistics", decoded_json_response) self.assertIn("results", decoded_json_response) # Checks the statistics statistics = decoded_json_response["statistics"] self.assertIn("<td id=\"mutable_nb_attending_players\">4</td>", statistics) self.assertIn( "<td id=\"mutable_players_A\">%s</td>" % UserData.franck_l.pseudonym, statistics) self.assertIn(u"<td id=\"mutable_sum_in_play\">50 €</td>", statistics) # Checks the results results = decoded_json_response["results"] self.assertNotIn(UserData.franck_l.pseudonym, results) self.assertIn(UserData.franck_p.pseudonym, results) self.assertIn(UserData.jo.pseudonym, results) self.assertIn(UserData.nico.pseudonym, results) self.assertNotIn(UserData.fx.pseudonym, results) self.assertIn(UserData.rolland.pseudonym, results) self.assertNotIn("</button>", results) finally: #TODO: should be done by the fixture config.orm.delete(inserted_row) config.orm.commit()
def test_admin_seasons_GET(self): self.login("*****@*****.**", "secret2") response = app.request("/admin/seasons") #@UndefinedVariable self.assertEqual(response.status, HTTP_OK) for season in (SeasonData.season_1, SeasonData.season_2): self.assertIn(str(season.start_year), response.data) self.assertIn(str(season.end_year), response.data) self.assertIn("Modifier", response.data) self.assertIn("Créer", response.data)
def test_admin_tournaments_GET(self): self.login("*****@*****.**", "secret2") response = app.request("/admin/tournaments") #@UndefinedVariable self.assertEqual(response.status, HTTP_OK) # TODO: automate by looping over the fixture self.assertIn("01/09/2009", response.data) self.assertIn("01/01/2010", response.data) self.assertIn("01/08/2010", response.data) self.assertIn("Modifier", response.data) self.assertIn("Créer", response.data)
def test_add_comment_404(self): self.login() response = app.request("/poll/comment", method="POST", data={ "poll_id": 42, "comment": "Sondage : salut les copains" }) #@UndefinedVariable self.assertEqual(response.status, HTTP_NOT_FOUND)
def test_admin_news_GET(self): self.login("*****@*****.**", "secret2") response = app.request("/admin/news") #@UndefinedVariable self.assertEqual(response.status, HTTP_OK) # TODO: automate by looping over the fixture self.assertIn("27/05/2011", response.data) self.assertIn("10/06/2011", response.data) self.assertIn("13/07/2011", response.data) self.assertIn("15/08/2011", response.data) self.assertIn("Modifier", response.data) self.assertIn("Créer", response.data)
def test_admin_results_GET(self): self.login("*****@*****.**", "secret2") response = app.request("/admin/results?tournament_id=1") #@UndefinedVariable self.assertEqual(response.status, HTTP_OK) self.assertIn(UserData.franck_l.pseudonym, response.data) self.assertIn(UserData.franck_p.pseudonym, response.data) self.assertIn(UserData.jo.pseudonym, response.data) self.assertIn(UserData.nico.pseudonym, response.data) self.assertIn(UserData.fx.pseudonym, response.data) self.assertNotIn(UserData.rolland.pseudonym, response.data) self.assertIn("</button>", response.data)
def test_admin_results_GET(self): self.login("*****@*****.**", "secret2") response = app.request( "/admin/results?tournament_id=1") #@UndefinedVariable self.assertEqual(response.status, HTTP_OK) self.assertIn(UserData.franck_l.pseudonym, response.data) self.assertIn(UserData.franck_p.pseudonym, response.data) self.assertIn(UserData.jo.pseudonym, response.data) self.assertIn(UserData.nico.pseudonym, response.data) self.assertIn(UserData.fx.pseudonym, response.data) self.assertNotIn(UserData.rolland.pseudonym, response.data) self.assertIn("</button>", response.data)
def test_update_user(self): updated_user = User( first_name=UserData.franck_l.first_name + "_new", last_name=UserData.franck_l.last_name + "_new", pseudonym=UserData.franck_l.pseudonym+ "_new", email="new." + UserData.franck_l.email, password=UserData.franck_l.password, level=UserData.franck_l.level ) updated_user_data = { "User-1-pseudonym" : updated_user.pseudonym, "User-1-first_name" : updated_user.first_name, "User-1-last_name" : updated_user.last_name, "User-1-email" : updated_user.email } self.login() # Makes sure that the update fails with invalid data for key in updated_user_data.keys(): # Alters the submitted data invalid_user_data = copy.deepcopy(updated_user_data) invalid_user_data[key] = "" # Makes sure the server rejects the update response = app.request("/update/user", method="POST", data=invalid_user_data) #@UndefinedVariable self.assertEqual(response.status, HTTP_OK) self.assertIn("Please enter a value", response.data) self.assertEqual(config.session_manager.user, UserData.franck_l) self.assertNotEqual(config.session_manager.user, updated_user) # Makes sure that the update works with valid data response = app.request("/update/user", method="POST", data=updated_user_data) #@UndefinedVariable self.assertEqual(response.status, HTTP_SEE_OTHER) self.assertEqual(config.session_manager.user, updated_user) self.assertNotEqual(config.session_manager.user, UserData.franck_l)
def test_view_account(self): self.login() response = app.request("/admin/account") #@UndefinedVariable self.assertEqual(response.status, HTTP_OK) self.assertIn("Mon compte", response.data) self.assertIn("Mon mot de passe", response.data) self.assertIn(UserData.franck_l.pseudonym, response.data) self.assertIn(UserData.franck_l.first_name, response.data) self.assertIn(UserData.franck_l.last_name, response.data) self.assertIn(UserData.franck_l.email, response.data) self.assertNotIn(UserData.franck_l.password, response.data) self.assertNotIn("Please enter a value", response.data)
def test_vote_invalid_choices(self): self.login("*****@*****.**", "secret4") poll_3 = config.orm.query(Poll).filter(Poll.start_dt == datetime.date( 2020, 10, 1)).one() #@UndefinedVariable self.assertFalse(poll_3.expired) self.assertEqual(len(poll_3.choices_by_user), 0) response = app.request("/poll/vote", method="POST", data=urllib.urlencode( { "poll_id": poll_3.id, "poll_user_choices": [0, 2] }, True)) #@UndefinedVariable self.assertEqual(response.status, HTTP_FORBIDDEN) self.assertEqual( response.data, u"Un des entiers passes a la methode /poll/vote n'est pas compris dans l'intervalle [0, 1]" ) response = app.request("/poll/vote", method="POST", data=urllib.urlencode( { "poll_id": poll_3.id, "poll_user_choices": [3] }, True)) #@UndefinedVariable self.assertEqual(response.status, HTTP_FORBIDDEN) self.assertEqual( response.data, u"Un des entiers passes a la methode /poll/vote n'est pas compris dans l'intervalle [0, 1]" ) self.assertEqual(len(poll_3.choices_by_user), 0)
def test_add_comment_new(self): tournament_11 = config.orm.query(Tournament).filter( Tournament.tournament_dt == datetime.date( 2009, 9, 1)).one() #@UndefinedVariable self.assertEqual(len(tournament_11.comments), 0) self.login() response = app.request("/add/comment", method="POST", data={ "tournament_id": 1, "comment": "Salut les copains" }) #@UndefinedVariable self.assertEqual(response.status, HTTP_OK) self.assertIn("Salut les copains", response.data) self.assertEqual(len(tournament_11.comments), 1)
def test_update_status_existing(self): tournament_11 = config.orm.query(Tournament).filter( Tournament.tournament_dt == datetime.date( 2009, 9, 1)).one() #@UndefinedVariable self.assertEqual(len(tournament_11.results), 5) self.assertTrue( config.orm.query(Result).filter(Result.tournament_id == 1).filter( Result.user_id == 1).one()) #@UndefinedVariable self.login("*****@*****.**", "secret1") response = app.request("/update/status", method="POST", data={ "tournament_id": 1, "status": Result.STATUSES.M }) #@UndefinedVariable self.assertEqual(response.status, HTTP_OK) self.assertEqual(len(tournament_11.results), 5) updated_row = config.orm.query(Result).filter( Result.tournament_id == 1).filter( Result.user_id == 1).one() #@UndefinedVariable self.assertEquals(updated_row.status, Result.STATUSES.M) # Checks that the JSON response is well-formed decoded_json_response = json.loads(response.data) self.assertEqual(len(decoded_json_response), 2) self.assertIn("statistics", decoded_json_response) self.assertIn("results", decoded_json_response) # Checks the statistics statistics = decoded_json_response["statistics"] self.assertIn("<td id=\"mutable_nb_attending_players\">3</td>", statistics) self.assertIn( "<td id=\"mutable_players_M\">%s,%s</td>" % (UserData.fx.pseudonym, UserData.franck_l.pseudonym), statistics) self.assertIn(u"<td id=\"mutable_sum_in_play\">40 €</td>", statistics) # Checks the results results = decoded_json_response["results"] self.assertNotIn(UserData.franck_l.pseudonym, results) self.assertIn(UserData.franck_p.pseudonym, results) self.assertIn(UserData.jo.pseudonym, results) self.assertIn(UserData.nico.pseudonym, results) self.assertNotIn(UserData.fx.pseudonym, results) self.assertNotIn(UserData.rolland.pseudonym, results) self.assertNotIn("</button>", results)
def test_add_comment_existing(self): try: tournament_21 = config.orm.query(Tournament).join(Tournament.season).filter(Season.id == 2).one() #@UndefinedVariable self.assertEqual(len(tournament_21.comments), 1) self.login() response = app.request("/add/comment", method="POST", data={"tournament_id" : 3, "comment" : "Salut les copains"}) #@UndefinedVariable self.assertEqual(response.status, HTTP_OK) self.assertIn("Salut les copains", response.data) self.assertEqual(len(tournament_21.comments), 2) finally: #TODO: should be done by the fixture config.orm.delete(tournament_21.comments[1]) config.orm.commit()
def test_add_comment_new(self): poll_1 = config.orm.query(Poll).filter(Poll.start_dt == datetime.date(2012, 4, 5)).one() #@UndefinedVariable poll_2 = config.orm.query(Poll).filter(Poll.start_dt == datetime.date(2011, 5, 28)).one() #@UndefinedVariable poll_3 = config.orm.query(Poll).filter(Poll.start_dt == datetime.date(2020, 10, 1)).one() #@UndefinedVariable self.assertEqual(len(poll_1.comments), 2) self.assertEqual(len(poll_2.comments), 1) self.assertEqual(len(poll_3.comments), 0) self.login() response = app.request("/poll/comment", method="POST", data={"poll_id": poll_3.id, "comment": "Sondage : salut les copains"}) #@UndefinedVariable self.assertEqual(response.status, HTTP_OK) self.assertIn("Sondage : salut les copains", response.data) self.assertEqual(len(poll_1.comments), 2) self.assertEqual(len(poll_2.comments), 1) self.assertEqual(len(poll_3.comments), 1)
def test_view_season_results(self): self.login() response = app.request("/season/1/results") #@UndefinedVariable self.assertEqual(response.status, HTTP_OK) # Checks that the JSON response is well-formed decoded_json_response = json.loads(response.data) self.assertEqual(len(decoded_json_response), 2) self.assertIn("players", decoded_json_response) self.assertIn("results", decoded_json_response) # Checks the actual JSON response players = decoded_json_response["players"] self.assertEqual(len(players), 3) self.assertIn(UserData.franck_p.pseudonym, players.values()) self.assertIn(UserData.jo.pseudonym, players.values()) self.assertIn(UserData.nico.pseudonym, players.values()) results = decoded_json_response["results"] self.assertEqual(len(results), 2) self.assertEqual(len(results[0]), 3) self.assertEqual(len(results[1]), 2)
def test_update_status_new(self): try: tournament_11 = config.orm.query(Tournament).filter(Tournament.tournament_dt == datetime.date(2009, 9, 1)).one() #@UndefinedVariable self.assertEqual(len(tournament_11.results), 5) self.assertFalse(config.orm.query(Result).filter(Result.tournament_id == 1).filter(Result.user_id == 6).all()) #@UndefinedVariable self.login("*****@*****.**", "secret6") response = app.request("/update/status", method="POST", data={"tournament_id" : 1, "status" : Result.STATUSES.P}) #@UndefinedVariable self.assertEqual(response.status, HTTP_OK) self.assertEqual(len(tournament_11.results), 6) inserted_row = config.orm.query(Result).filter(Result.tournament_id == 1).filter(Result.user_id == 6).one() #@UndefinedVariable self.assertEquals(inserted_row.status, Result.STATUSES.P) # Checks that the JSON response is well-formed decoded_json_response = json.loads(response.data) self.assertEqual(len(decoded_json_response), 2) self.assertIn("statistics", decoded_json_response) self.assertIn("results", decoded_json_response) # Checks the statistics statistics = decoded_json_response["statistics"] self.assertIn("<td id=\"mutable_nb_attending_players\">4</td>", statistics) self.assertIn("<td id=\"mutable_players_A\">%s</td>" % UserData.franck_l.pseudonym, statistics) self.assertIn(u"<td id=\"mutable_sum_in_play\">50 €</td>", statistics) # Checks the results results = decoded_json_response["results"] self.assertNotIn(UserData.franck_l.pseudonym, results) self.assertIn(UserData.franck_p.pseudonym, results) self.assertIn(UserData.jo.pseudonym, results) self.assertIn(UserData.nico.pseudonym, results) self.assertNotIn(UserData.fx.pseudonym, results) self.assertIn(UserData.rolland.pseudonym, results) self.assertNotIn("</button>", results) finally: #TODO: should be done by the fixture config.orm.delete(inserted_row) config.orm.commit()
def test_add_comment_existing(self): try: tournament_21 = config.orm.query(Tournament).join( Tournament.season).filter( Season.id == 2).one() #@UndefinedVariable self.assertEqual(len(tournament_21.comments), 1) self.login() response = app.request("/add/comment", method="POST", data={ "tournament_id": 3, "comment": "Salut les copains" }) #@UndefinedVariable self.assertEqual(response.status, HTTP_OK) self.assertIn("Salut les copains", response.data) self.assertEqual(len(tournament_21.comments), 2) finally: #TODO: should be done by the fixture config.orm.delete(tournament_21.comments[1]) config.orm.commit()
def test_add_comment_existing(self): try: poll_1 = config.orm.query(Poll).filter( Poll.start_dt == datetime.date(2012, 4, 5)).one() #@UndefinedVariable poll_2 = config.orm.query(Poll).filter( Poll.start_dt == datetime.date(2011, 5, 28)).one() #@UndefinedVariable poll_3 = config.orm.query(Poll).filter( Poll.start_dt == datetime.date(2020, 10, 1)).one() #@UndefinedVariable self.assertEqual(len(poll_1.comments), 2) self.assertEqual(len(poll_2.comments), 1) self.assertEqual(len(poll_3.comments), 0) self.login() response = app.request("/poll/comment", method="POST", data={ "poll_id": poll_1.id, "comment": "Sondage : salut les copains" }) #@UndefinedVariable self.assertEqual(response.status, HTTP_OK) self.assertIn("Sondage : salut les copains", response.data) self.assertEqual(len(poll_1.comments), 3) self.assertEqual(len(poll_2.comments), 1) self.assertEqual(len(poll_3.comments), 0) finally: #TODO: should be done by the fixture config.orm.delete(poll_1.comments[2]) config.orm.commit()
def test_view(self): self.login() response = app.request("/tournament/2/1") #@UndefinedVariable self.assertEqual(response.status, HTTP_OK) self.assertIn("Tournoi 1 (<span class=\"primary_label\">dimanche 01 août 2010</span>", response.data)
def test_view_404(self): self.login() response = app.request("/tournament/2/9") #@UndefinedVariable self.assertEqual(response.status, HTTP_NOT_FOUND)
def test_view_notlogged(self): response = app.request("/tournament/2/1") #@UndefinedVariable self.assertEqual(response.status, HTTP_SEE_OTHER)
def test_admin_results_POST(self): tournament_21 = config.orm.query(Tournament).join(Tournament.season).filter(Season.id == 2).one() #@UndefinedVariable jo = config.orm.query(User).filter(User.first_name == "Jonathan").one() #@UndefinedVariable fx = config.orm.query(User).filter(User.pseudonym == "FX").one() #@UndefinedVariable updated_results = [ Result(user=jo, status=Result.STATUSES.P, buyin=10, rank=1, profit=30), Result(user=fx, status=Result.STATUSES.P, buyin=20, rank=2) ] updated_results_data = { "tournament_id" : "3", "Result-9-status" : "P", "Result-9-buyin" : "20", "Result-9-rank" : "2", "Result-9-profit" : "", "Result-10-status" : "P", "Result-10-buyin" : "10", "Result-10-rank" : "1", "Result-10-profit" : "30" } self.login("*****@*****.**", "secret2") # Makes sure that the update fails with invalid data (1) invalid_results_data = copy.deepcopy(updated_results_data) invalid_results_data["Result-9-buyin"] = "" response = app.request("/admin/results", method="POST", data=invalid_results_data) #@UndefinedVariable self.assertEqual(response.status, HTTP_OK) self.assertIn("Mandatory value (status=P)", response.data) decoded_json_response = json.loads(response.data) self.assertEqual(len(decoded_json_response), 2) self.assertIn("results", decoded_json_response) self.assertIn("statistics", decoded_json_response) self.assertIsNone(decoded_json_response["statistics"]) self.assertNotEqual(tournament_21.results, updated_results) self.assertEqual(tournament_21.results, [ResultData.result21_jo, ResultData.result21_fx]) # Makes sure that the update fails with invalid data (2) invalid_results_data = copy.deepcopy(updated_results_data) invalid_results_data["Result-9-status"] = "M" invalid_results_data["Result-9-buyin"] = "20" invalid_results_data["Result-9-rank"] = "" invalid_results_data["Result-9-profit"] = "" response = app.request("/admin/results", method="POST", data=invalid_results_data) #@UndefinedVariable self.assertEqual(response.status, HTTP_OK) self.assertIn("Forbidden value (status=M)", response.data) decoded_json_response = json.loads(response.data) self.assertEqual(len(decoded_json_response), 2) self.assertIn("results", decoded_json_response) self.assertIn("statistics", decoded_json_response) self.assertIsNone(decoded_json_response["statistics"]) self.assertNotEqual(tournament_21.results, updated_results) self.assertEqual(tournament_21.results, [ResultData.result21_jo, ResultData.result21_fx]) # Makes sure that the update fails with invalid data (3) invalid_results_data = copy.deepcopy(updated_results_data) invalid_results_data["Result-9-status"] = "M" invalid_results_data["Result-9-buyin"] = "" invalid_results_data["Result-9-rank"] = "2" invalid_results_data["Result-9-profit"] = "" response = app.request("/admin/results", method="POST", data=invalid_results_data) #@UndefinedVariable self.assertEqual(response.status, HTTP_OK) self.assertIn("Forbidden value (status=M)", response.data) decoded_json_response = json.loads(response.data) self.assertEqual(len(decoded_json_response), 2) self.assertIn("results", decoded_json_response) self.assertIn("statistics", decoded_json_response) self.assertIsNone(decoded_json_response["statistics"]) self.assertNotEqual(tournament_21.results, updated_results) self.assertEqual(tournament_21.results, [ResultData.result21_jo, ResultData.result21_fx]) # Makes sure that the update fails with invalid data (4) invalid_results_data = copy.deepcopy(updated_results_data) invalid_results_data["Result-9-status"] = "M" invalid_results_data["Result-9-buyin"] = "" invalid_results_data["Result-9-rank"] = "" invalid_results_data["Result-9-profit"] = "0" response = app.request("/admin/results", method="POST", data=invalid_results_data) #@UndefinedVariable self.assertEqual(response.status, HTTP_OK) self.assertIn("Forbidden value (status=M)", response.data) decoded_json_response = json.loads(response.data) self.assertEqual(len(decoded_json_response), 2) self.assertIn("results", decoded_json_response) self.assertIn("statistics", decoded_json_response) self.assertIsNone(decoded_json_response["statistics"]) self.assertNotEqual(tournament_21.results, updated_results) self.assertEqual(tournament_21.results, [ResultData.result21_jo, ResultData.result21_fx]) # Makes sure that the update fails with invalid data (5) invalid_results_data = copy.deepcopy(updated_results_data) invalid_results_data["Result-9-status"] = "A" invalid_results_data["Result-9-buyin"] = "20" invalid_results_data["Result-9-rank"] = "" invalid_results_data["Result-9-profit"] = "" response = app.request("/admin/results", method="POST", data=invalid_results_data) #@UndefinedVariable self.assertEqual(response.status, HTTP_OK) self.assertIn("Forbidden value (status=A)", response.data) decoded_json_response = json.loads(response.data) self.assertEqual(len(decoded_json_response), 2) self.assertIn("results", decoded_json_response) self.assertIn("statistics", decoded_json_response) self.assertIsNone(decoded_json_response["statistics"]) self.assertNotEqual(tournament_21.results, updated_results) self.assertEqual(tournament_21.results, [ResultData.result21_jo, ResultData.result21_fx]) # Makes sure that the update fails with invalid data (6) invalid_results_data = copy.deepcopy(updated_results_data) invalid_results_data["Result-9-status"] = "A" invalid_results_data["Result-9-buyin"] = "" invalid_results_data["Result-9-rank"] = "2" invalid_results_data["Result-9-profit"] = "" response = app.request("/admin/results", method="POST", data=invalid_results_data) #@UndefinedVariable self.assertEqual(response.status, HTTP_OK) self.assertIn("Forbidden value (status=A)", response.data) decoded_json_response = json.loads(response.data) self.assertEqual(len(decoded_json_response), 2) self.assertIn("results", decoded_json_response) self.assertIn("statistics", decoded_json_response) self.assertIsNone(decoded_json_response["statistics"]) self.assertNotEqual(tournament_21.results, updated_results) self.assertEqual(tournament_21.results, [ResultData.result21_jo, ResultData.result21_fx]) # Makes sure that the update fails with invalid data (7) invalid_results_data = copy.deepcopy(updated_results_data) invalid_results_data["Result-9-status"] = "A" invalid_results_data["Result-9-buyin"] = "" invalid_results_data["Result-9-rank"] = "" invalid_results_data["Result-9-profit"] = "0" response = app.request("/admin/results", method="POST", data=invalid_results_data) #@UndefinedVariable self.assertEqual(response.status, HTTP_OK) self.assertIn("Forbidden value (status=A)", response.data) decoded_json_response = json.loads(response.data) self.assertEqual(len(decoded_json_response), 2) self.assertIn("results", decoded_json_response) self.assertIn("statistics", decoded_json_response) self.assertIsNone(decoded_json_response["statistics"]) self.assertNotEqual(tournament_21.results, updated_results) self.assertEqual(tournament_21.results, [ResultData.result21_jo, ResultData.result21_fx]) # Makes sure that the update works with valid data response = app.request("/admin/results", method="POST", data=updated_results_data) #@UndefinedVariable self.assertEqual(response.status, HTTP_OK) decoded_json_response = json.loads(response.data) self.assertEqual(len(decoded_json_response), 2) self.assertIn("results", decoded_json_response) self.assertIn("statistics", decoded_json_response) self.assertIsNotNone(decoded_json_response["statistics"]) self.assertEqual(tournament_21.results, updated_results) self.assertNotEqual(tournament_21.results, [ResultData.result21_jo, ResultData.result21_fx])
def test_admin_results_POST_notournament(self): self.login("*****@*****.**", "secret2") response = app.request("/admin/results", method="POST") #@UndefinedVariable self.assertEqual(response.status, HTTP_NOT_FOUND)
def test_admin_results_POST_notadmin(self): self.login("*****@*****.**", "secret1") response = app.request("/admin/results", method="POST", data={"tournament_id" : "1"}) #@UndefinedVariable self.assertEqual(response.status, HTTP_FORBIDDEN)
def test_login_POST_inactiveuser(self): self.assertIsNone(config.session_manager.user) response = app.request("/login", method="POST", data={"email" : "*****@*****.**", "password" : "secret7"}) #@UndefinedVariable self.assertEqual(response.status, HTTP_OK) self.assertIsNone(config.session_manager.user)
def test_admin_results_GET_notadmin(self): self.login("*****@*****.**", "secret1") response = app.request("/admin/results?tournament_id=1") #@UndefinedVariable self.assertEqual(response.status, HTTP_FORBIDDEN)
def test_create_account_POST_invalidtoken(self): response = app.request("/create/account", method="POST", data={"token" : "invalid"}) #@UndefinedVariable self.assertEqual(response.status, HTTP_FORBIDDEN)
def test_create_account_POST_wrongtokentype(self): response = app.request("/create/account", method="POST", data={"token" : PasswordTokenData.password_token_active.token}) #@UndefinedVariable self.assertEqual(response.status, HTTP_FORBIDDEN)
def test_create_account_POST_expiredtoken(self): response = app.request("/create/account", method="POST", data={"token" : UserTokenData.user_token_expired.token}) #@UndefinedVariable self.assertEqual(response.status, HTTP_FORBIDDEN)
def test_update_user_notlogged(self): response = app.request("/update/user", method="POST") #@UndefinedVariable self.assertEqual(response.status, HTTP_SEE_OTHER)
def test_update_status_notlogged(self): response = app.request("/update/status", method="POST") #@UndefinedVariable self.assertEqual(response.status, HTTP_SEE_OTHER)
def test_admin_results_POST_notlogged(self): response = app.request("/admin/results", method="POST", data={"tournament_id" : "1"}) #@UndefinedVariable self.assertEqual(response.status, HTTP_SEE_OTHER)
def test_view_account_notlogged(self): response = app.request("/admin/account") #@UndefinedVariable self.assertEqual(response.status, HTTP_SEE_OTHER)