Coverage for tests/first_snap/tests_views.py: 100%

106 statements  

« prev     ^ index     » next       coverage.py v7.10.2, created at 2025-08-05 22:06 +0000

1import os 

2 

3from unittest.mock import mock_open, patch 

4from flask_testing import TestCase 

5 

6from webapp import helpers 

7from webapp.app import create_app 

8 

9 

10class FirstSnap(TestCase): 

11 def create_app(self): 

12 app = create_app(testing=True) 

13 app.secret_key = "secret_key" 

14 

15 return app 

16 

17 @patch("builtins.open", new_callable=mock_open, read_data="test: test") 

18 def test_get_yaml(self, mock_open_file): 

19 yaml_read = helpers.get_yaml("filename.yaml", typ="rt") 

20 self.assertEqual(yaml_read, {"test": "test"}) 

21 mock_open_file.assert_called_with( 

22 os.path.join(self.app.root_path, "filename.yaml"), "r" 

23 ) 

24 

25 @patch( 

26 "builtins.open", new_callable=mock_open, read_data="test: test: test" 

27 ) 

28 def test_get_yaml_error(self, mock_open_file): 

29 yaml_read = helpers.get_yaml("filename.yaml", typ="rt") 

30 self.assertEqual(yaml_read, None) 

31 mock_open_file.assert_called_with( 

32 os.path.join(self.app.root_path, "filename.yaml"), "r" 

33 ) 

34 

35 def test_get_language(self): 

36 response = self.client.get("/first-snap/python") 

37 assert response.status_code == 200 

38 self.assert_context("language", "python") 

39 self.assert_template_used("first-snap/install-snapcraft.html") 

40 

41 def test_get_language_404(self): 

42 response = self.client.get("/first-snap/toto-lang") 

43 

44 assert response.status_code == 404 

45 

46 def test_get_package(self): 

47 response = self.client.get("/first-snap/python/linux/package") 

48 assert response.status_code == 200 

49 self.assert_context("language", "python") 

50 self.assert_context("os", "linux") 

51 self.assert_context("has_user_chosen_name", False) 

52 self.assert_template_used("first-snap/package.html") 

53 

54 def test_get_package_snap_name(self): 

55 self.client.set_cookie( 

56 "localhost", "fsf_snap_name_python", "test-snap-name-python" 

57 ) 

58 response = self.client.get( 

59 "/first-snap/python/linux/package", 

60 ) 

61 assert response.status_code == 200 

62 self.assert_context("language", "python") 

63 self.assert_context("snap_name", "test-snap-name-python") 

64 self.assert_context("has_user_chosen_name", True) 

65 self.assert_template_used("first-snap/package.html") 

66 

67 def test_get_package_404_language(self): 

68 response = self.client.get("/first-snap/toto-lang/linux/package") 

69 

70 assert response.status_code == 404 

71 

72 @patch("builtins.open", new_callable=mock_open, read_data="name: test") 

73 def test_get_snapcraft_yaml(self, mock_open_file): 

74 response = self.client.get("/first-snap/python/snapcraft.yaml") 

75 self.assert200(response) 

76 self.assertEqual(response.get_data(as_text=True), "name: test") 

77 

78 def test_get_snapcraft_yaml_404(self): 

79 response = self.client.get("/first-snap/toto-lang/snapcraft.yaml") 

80 self.assert404(response) 

81 

82 def test_get_build_and_test(self): 

83 response = self.client.get("/first-snap/python/linux/build-and-test") 

84 assert response.status_code == 200 

85 self.assert_context("language", "python") 

86 self.assert_context("os", "linux") 

87 self.assert_template_used("first-snap/build-and-test.html") 

88 

89 def test_get_build_and_test_snap_name(self): 

90 self.client.set_cookie( 

91 "snapcraft.io", "fsf_snap_name_python", "test-snap-name-python" 

92 ) 

93 response = self.client.get("/first-snap/python/linux/build-and-test") 

94 assert response.status_code == 200 

95 self.assert_context("language", "python") 

96 self.assert_context("os", "linux") 

97 self.assert_context("snap_name", "test-offlineimap-{name}") 

98 self.assert_template_used("first-snap/build-and-test.html") 

99 

100 def test_get_build_and_test_404_language(self): 

101 response = self.client.get( 

102 "/first-snap/toto-lang/linux/build-and-test" 

103 ) 

104 

105 assert response.status_code == 404 

106 

107 def test_get_build_and_test_404_os(self): 

108 response = self.client.get("/first-snap/python/totOs/build-and-test") 

109 

110 assert response.status_code == 404 

111 

112 def test_get_upload(self): 

113 response = self.client.get("/first-snap/python/linux/upload") 

114 

115 assert response.status_code == 200 

116 self.assert_template_used("first-snap/upload.html") 

117 self.assert_context("language", "python") 

118 self.assert_context("os", "linux") 

119 self.assert_context("user", None) 

120 self.assert_context("snap_name", "test-offlineimap-{name}") 

121 

122 def test_get_upload_snap_name(self): 

123 self.client.set_cookie( 

124 "localhost", "fsf_snap_name_python", "test-snap-name-python" 

125 ) 

126 response = self.client.get( 

127 "/first-snap/python/linux/upload", 

128 ) 

129 assert response.status_code == 200 

130 self.assert_context("language", "python") 

131 self.assert_context("os", "linux") 

132 self.assert_context("snap_name", "test-snap-name-python") 

133 self.assert_context("has_user_chosen_name", True) 

134 self.assert_context("user", None) 

135 self.assert_template_used("first-snap/upload.html") 

136 

137 def test_get_upload_logged_in(self): 

138 user_expected = { 

139 "image": None, 

140 "username": "Toto", 

141 "display_name": "El Toto", 

142 "email": "testing@testing.com", 

143 } 

144 with self.client.session_transaction() as s: 

145 s["publisher"] = { 

146 "image": None, 

147 "nickname": "Toto", 

148 "fullname": "El Toto", 

149 "email": "testing@testing.com", 

150 } 

151 response = self.client.get("/first-snap/python/linux/upload") 

152 

153 assert response.status_code == 200 

154 self.assert_template_used("first-snap/upload.html") 

155 self.assert_context("language", "python") 

156 self.assert_context("os", "linux") 

157 self.assert_context("user", user_expected) 

158 self.assert_context("snap_name", "test-offlineimap-Toto") 

159 

160 def test_get_upload_404(self): 

161 response = self.client.get("/first-snap/toto-lang/linux/upload") 

162 

163 assert response.status_code == 404