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

106 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-04-28 22:05 +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-auto/package") 

48 assert response.status_code == 200 

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

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

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-auto/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( 

84 "/first-snap/python/linux-auto/build-and-test" 

85 ) 

86 assert response.status_code == 200 

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

88 self.assert_context("os", "linux-auto") 

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

90 

91 def test_get_build_and_test_snap_name(self): 

92 self.client.set_cookie( 

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

94 ) 

95 response = self.client.get( 

96 "/first-snap/python/linux-auto/build-and-test" 

97 ) 

98 assert response.status_code == 200 

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

100 self.assert_context("os", "linux-auto") 

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

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

103 

104 def test_get_build_and_test_404_language(self): 

105 response = self.client.get( 

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

107 ) 

108 

109 assert response.status_code == 404 

110 

111 def test_get_build_and_test_404_os(self): 

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

113 

114 assert response.status_code == 404 

115 

116 def test_get_upload(self): 

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

118 

119 assert response.status_code == 200 

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

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

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

123 self.assert_context("user", None) 

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

125 

126 def test_get_upload_snap_name(self): 

127 self.client.set_cookie( 

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

129 ) 

130 response = self.client.get( 

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

132 ) 

133 assert response.status_code == 200 

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

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

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

137 self.assert_context("has_user_chosen_name", True) 

138 self.assert_context("user", None) 

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

140 

141 def test_get_upload_logged_in(self): 

142 user_expected = { 

143 "image": None, 

144 "username": "Toto", 

145 "display_name": "El Toto", 

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

147 } 

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

149 s["publisher"] = { 

150 "image": None, 

151 "nickname": "Toto", 

152 "fullname": "El Toto", 

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

154 } 

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

156 

157 assert response.status_code == 200 

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

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

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

161 self.assert_context("user", user_expected) 

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

163 

164 def test_get_upload_404(self): 

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

166 

167 assert response.status_code == 404