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"""Test scenario for contributions. 

2 

3## Domain 

4 

5* Users as in `conftest`, under *players* 

6* Clean slate, see `starters`. 

7* The user table 

8* The country table 

9* One contribution record 

10 

11## Acts 

12 

13Modifying contribution fields that are typed in by the user. 

14 

15`test_start` 

16: **owner** adds a new contribution and adds **editor** to the editors. 

17 

18`test_changeUserCountry` 

19: All power users change the country of **mycoord** and back. 

20 

21`test_modifyTitleAll` 

22: All users try to modify the title of the new contribution. 

23 Only some succeed, and change it back. 

24 

25`test_modifyDescription` 

26: **owner** modifies the description and costDescription. 

27 

28`test_descriptionMarkdown` 

29: **owner** sees that the description is formatted in markdown. 

30 

31`test_modifyCost` 

32: **owner** fills out the costTotal field. 

33 

34`test_modifyEmail` 

35: **owner** enters multiple contact email addresses. 

36 

37`test_modifyUrl` 

38: **owner** enters multiple contact urls, with various malformed forms. 

39 They all get normalized. 

40""" 

41 

42import pytest 

43 

44import magic # noqa 

45from control.utils import pick as G, EURO 

46from conftest import USERS, POWER_USERS 

47from example import ( 

48 BELGIUM, 

49 CHECKS, 

50 CONTACT_PERSON_EMAIL, 

51 CONTRIB, 

52 COST_TOTAL, 

53 COST_DESCRIPTION, 

54 COUNTRY, 

55 DESCRIPTION, 

56 EDITOR, 

57 EXAMPLE, 

58 LUXEMBURG, 

59 MYCOORD, 

60 OFFICE, 

61 OWNER, 

62 ROOT, 

63 SYSTEM, 

64 TITLE, 

65 TITLE1, 

66 TITLE2, 

67 URL_C, 

68 URL_A, 

69 USER, 

70) 

71from helpers import forall 

72from starters import start 

73from subtest import assertFieldValue, assertModifyField 

74 

75 

76startInfo = {} 

77 

78 

79@pytest.mark.usefixtures("db") 

80def test_start(clientOffice, clientOwner): 

81 startInfo.update( 

82 start( 

83 clientOffice=clientOffice, 

84 clientOwner=clientOwner, 

85 users=True, 

86 contrib=True, 

87 countries=True, 

88 ) 

89 ) 

90 

91 

92def test_changeUserCountry(clientsPower): 

93 valueTables = startInfo["valueTables"] 

94 

95 users = valueTables[USER] 

96 countries = valueTables[COUNTRY] 

97 

98 assert MYCOORD in users 

99 assert BELGIUM in countries 

100 assert LUXEMBURG in countries 

101 

102 mycoord = users[MYCOORD] 

103 belgium = countries[BELGIUM] 

104 luxemburg = countries[LUXEMBURG] 

105 

106 def assertIt(cl, exp): 

107 assertFieldValue((cl, USER, mycoord), COUNTRY, BELGIUM) 

108 

109 assertModifyField(cl, USER, mycoord, COUNTRY, (luxemburg, LUXEMBURG), exp) 

110 assertModifyField(cl, USER, mycoord, COUNTRY, (belgium, BELGIUM), exp) 

111 

112 expect = {user: True for user in POWER_USERS} 

113 forall(clientsPower, expect, assertIt) 

114 

115 

116def test_modifyTitleAll(clients): 

117 recordId = startInfo["recordId"] 

118 eid = G(recordId, CONTRIB) 

119 

120 def assertIt(cl, exp): 

121 assertModifyField(cl, CONTRIB, eid, TITLE, TITLE2, exp) 

122 if exp: 

123 assertModifyField(cl, CONTRIB, eid, TITLE, TITLE1, exp) 

124 

125 expect = {user: False for user in USERS} 

126 expect.update({user: True for user in {OWNER, EDITOR, OFFICE, SYSTEM, ROOT}}) 

127 forall(clients, expect, assertIt) 

128 

129 

130@pytest.mark.parametrize( 

131 ("field", "value"), 

132 ( 

133 (DESCRIPTION, EXAMPLE[DESCRIPTION][0]), 

134 (COST_DESCRIPTION, EXAMPLE[COST_DESCRIPTION][0]), 

135 ), 

136) 

137def test_modifyDescription(clientsMy, field, value): 

138 recordId = startInfo["recordId"] 

139 

140 eid = G(recordId, CONTRIB) 

141 

142 def assertIt(cl, exp): 

143 assertModifyField(cl, CONTRIB, eid, field, (value, value.strip()), exp) 

144 

145 expect = {user: True for user in {OWNER, EDITOR}} 

146 forall(clientsMy, expect, assertIt) 

147 

148 

149@pytest.mark.parametrize( 

150 ("field", "checks"), 

151 ((DESCRIPTION, CHECKS[DESCRIPTION]), (COST_DESCRIPTION, CHECKS[COST_DESCRIPTION]),), 

152) 

153def test_descriptionMarkdown(clientsMy, field, checks): 

154 recordId = startInfo["recordId"] 

155 

156 eid = G(recordId, CONTRIB) 

157 

158 def assertIt(cl, exp): 

159 response = cl.get(f"/api/{CONTRIB}/item/{eid}/field/{field}?action=view") 

160 text = response.get_data(as_text=True) 

161 for check in checks: 

162 assert check in text 

163 

164 expect = {user: True for user in {OWNER, EDITOR}} 

165 forall(clientsMy, expect, assertIt) 

166 

167 

168@pytest.mark.parametrize( 

169 ("value", "expectVal"), 

170 ( 

171 ("103", f"{EURO} 103.0"), 

172 ("103.0", f"{EURO} 103.0"), 

173 ("103.00", f"{EURO} 103.0"), 

174 ("103.45", f"{EURO} 103.45"), 

175 ("103.456", f"{EURO} 103.456"), 

176 ), 

177) 

178def test_modifyCost(clientsMy, value, expectVal): 

179 recordId = startInfo["recordId"] 

180 

181 eid = G(recordId, CONTRIB) 

182 

183 def assertIt(cl, exp): 

184 assertModifyField(cl, CONTRIB, eid, COST_TOTAL, (value, expectVal), exp) 

185 

186 expect = {user: True for user in {OWNER, EDITOR}} 

187 forall(clientsMy, expect, assertIt) 

188 

189 

190@pytest.mark.parametrize( 

191 ("value", "expectVal"), 

192 ( 

193 ([OWNER], OWNER), 

194 ([], ""), 

195 (["owner@a"], "owner@a"), 

196 (["owner@a.b"], "owner@a.b"), 

197 (["owner@a.b", "owner@d.e"], "owner@a.b,owner@d.e"), 

198 ), 

199) 

200def test_modifyEmail(clientsMy, value, expectVal): 

201 recordId = startInfo["recordId"] 

202 

203 eid = G(recordId, CONTRIB) 

204 

205 def assertIt(cl, exp): 

206 assertModifyField( 

207 cl, CONTRIB, eid, CONTACT_PERSON_EMAIL, (value, expectVal), exp 

208 ) 

209 

210 expect = {user: True for user in {OWNER, EDITOR}} 

211 forall(clientsMy, expect, assertIt) 

212 

213 

214@pytest.mark.parametrize( 

215 ("field", "value", "expectVal"), 

216 ( 

217 (URL_C, [OWNER], "https://owner.org"), 

218 (URL_C, ["https://owner"], "https://owner.org"), 

219 (URL_C, ["Https://owner"], "https://owner.org"), 

220 (URL_C, ["Http://owner"], "http://owner.org"), 

221 (URL_C, ["Htp://owner"], "https://owner.org"), 

222 (URL_C, ["Ftp://owner"], "https://owner.org"), 

223 (URL_C, ["https:/owner"], "https://owner.org"), 

224 (URL_C, ["https:owner"], "https://owner.org"), 

225 (URL_C, ["https/owner"], "https://owner.org"), 

226 (URL_C, ["https/:owner"], "https://owner.org"), 

227 (URL_C, ["https/:/owner"], "https://owner.org"), 

228 (URL_C, ["https:///owner"], "https://owner.org"), 

229 (URL_C, ["https:////owner"], "https://owner.org"), 

230 (URL_C, ["owner.org"], "https://owner.org"), 

231 (URL_C, ["owner.eu"], "https://owner.eu"), 

232 (URL_C, ["https://owner.eu"], "https://owner.eu"), 

233 ( 

234 URL_C, 

235 ["https://owner.org", "http://owner.eu"], 

236 "https://owner.org,http://owner.eu", 

237 ), 

238 (URL_A, ["https://owner.eu"], "https://owner.eu"), 

239 ( 

240 URL_A, 

241 ["https://owner.org", "http://owner.eu"], 

242 "https://owner.org,http://owner.eu", 

243 ), 

244 ), 

245) 

246def test_modifyUrl(clientsMy, field, value, expectVal): 

247 recordId = startInfo["recordId"] 

248 

249 eid = G(recordId, CONTRIB) 

250 

251 def assertIt(cl, exp): 

252 assertModifyField(cl, CONTRIB, eid, field, (value, expectVal), exp) 

253 

254 expect = {user: True for user in {OWNER, EDITOR}} 

255 forall(clientsMy, expect, assertIt)