Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1""" 

2This file (test_contact.py) contains the functional tests for the 

3`contact` blueprint. 

4 

5These tests use GETs and POSTs to different endpoints to check for 

6the proper behavior of the `contact` blueprint. 

7""" 

8from flask import request 

9from flask import url_for 

10 

11 

12def test_contact_page(test_client): 

13 """ 

14 GIVEN a Flask application configured for testing, 

15 WHEN the /contact page is requested (GET) 

16 THEN check that the response is valid 

17 """ 

18 response = test_client.get("/contact/") 

19 assert response.status_code == 200 

20 assert b"Name" in response.data 

21 assert b"Email" in response.data 

22 assert b"Message" in response.data 

23 assert b"Submit" in response.data 

24 

25 

26def test_contact_dashboard(test_client): 

27 """ 

28 GIVEN a Flask application configured for testing, 

29 WHEN the /contact/dashboard page is requested (GET) 

30 THEN check that the response is valid 

31 """ 

32 # Logout and try to access the contact dashboard. It should redirect 

33 response = test_client.get(url_for("auth.logout"), follow_redirects=True) 

34 print(request.path) 

35 assert response.status_code == 200 

36 assert request.path == url_for("auth.login") 

37 

38 # check request to contact correctly redirects to login page 

39 response = test_client.get("/contact/dashboard", follow_redirects=True) 

40 assert request.path == url_for("auth.login") 

41 

42 # Login and try to access the contact dashboard. It should return OK 

43 response = test_client.post( 

44 url_for("auth.login"), 

45 data=dict(email="admin1@domain.com", password="pass"), 

46 follow_redirects=True, 

47 ) 

48 

49 # check if successfully logged in 

50 assert response.status_code == 200 

51 

52 # check response is valid 

53 response = test_client.get(url_for("contact.dashboard")) 

54 assert response.status_code == 200 

55 assert b"Contact dashboard" in response.data 

56 assert b"Name" in response.data 

57 assert b"Mail" in response.data 

58 assert b"Date" in response.data 

59 assert b"Info" in response.data 

60 assert b"View message" not in response.data 

61 

62 

63def test_contact_validate_msg(test_client): 

64 """ 

65 GIVEN a Flask application configured for testing, 

66 WHEN POST request is made contact validate page 

67 THEN check that the response is valid and that 

68 the new validated message appears on contact dashboard 

69 """ 

70 # GET request should fail for validate message 

71 # Currently test is uncommented since no return statement for 

72 # validate_message for GET 

73 # UNCOMMENT BELOW CODE AFTER FIXING validate_message 

74 # response = test_client.get(url_for("contact.validate_message")) 

75 # assert response.status_code != 200 

76 

77 # add a message 

78 response = test_client.post( 

79 url_for("contact.validate_message"), 

80 data=dict(name="User1", email="user1@gmail.com", message="User1 Message"), 

81 follow_redirects=True, 

82 ) 

83 assert response.status_code == 200 

84 

85 # check if message was added successfully 

86 response = test_client.get(url_for("contact.dashboard", page=1)) 

87 assert response.status_code == 200 

88 assert b"Contact dashboard" in response.data 

89 assert b"User1" in response.data 

90 assert b"user1@gmail.com" in response.data 

91 assert b"User1 Message" in response.data 

92 assert b"View message" in response.data 

93 

94 # change contact page and make sure the message is not there 

95 response = test_client.get(url_for("contact.dashboard", page=2)) 

96 assert response.status_code == 200 

97 assert b"User1" not in response.data 

98 assert b"user1@gmail.com" not in response.data 

99 assert b"User1 Message" not in response.data 

100 assert b"View message" not in response.data