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""" 

2File conftest.py contains pytest fixtures that are used in numerous 

3test functions. Refer to https://docs.pytest.org/en/stable/fixture.html 

4for more details on pytest 

5""" 

6import datetime 

7import json 

8import os 

9 

10import pytest 

11from app import create_app 

12from flask import url_for 

13from init import db as _db 

14from modules.box__default.auth.models import User 

15from modules.box__default.settings.models import Settings 

16 

17# run in shopyo/shopyo 

18# python -m pytest . or python -m pytest -v 

19 

20if os.path.exists("testing.db"): 20 ↛ 24line 20 didn't jump to line 24, because the condition on line 20 was never false

21 os.remove("testing.db") 

22 

23 

24@pytest.fixture(scope="session") 

25def unconfirmed_user(): 

26 """ 

27 A pytest fixture that returns a non admin user 

28 """ 

29 user = User() 

30 user.email = "unconfirmed@domain.com" 

31 user.password = "pass" 

32 user.is_email_confirmed = False 

33 return user 

34 

35 

36@pytest.fixture(scope="session") 

37def non_admin_user(): 

38 """ 

39 A pytest fixture that returns a non admin user 

40 """ 

41 user = User() 

42 user.email = "admin1@domain.com" 

43 user.password = "pass" 

44 user.is_email_confirmed = True 

45 user.email_confirm_date = datetime.datetime.now() 

46 return user 

47 

48 

49@pytest.fixture(scope="session") 

50def admin_user(): 

51 """ 

52 A pytest fixture that returns an admin user 

53 """ 

54 user = User() 

55 user.email = "admin2@domain.com" 

56 user.password = "pass" 

57 user.is_admin = True 

58 user.is_email_confirmed = True 

59 user.email_confirm_date = datetime.datetime.now() 

60 return user 

61 

62 

63@pytest.fixture(scope="session") 

64def flask_app(): 

65 flask_app = create_app("testing") 

66 return flask_app 

67 

68 

69@pytest.fixture(scope="session") 

70def test_client(flask_app): 

71 """ 

72 setups up and returns the flask testing app 

73 """ 

74 # Create a test client using the Flask application configured for testing 

75 with flask_app.test_client() as testing_client: 

76 # Establish an application context 

77 with flask_app.app_context(): 

78 yield testing_client # this is where the testing happens! 

79 

80 

81@pytest.fixture(scope="session") 

82def db(test_client, non_admin_user, admin_user, unconfirmed_user): 

83 """ 

84 creates and returns the initial testing database 

85 """ 

86 # Create the database and the database table 

87 _db.app = test_client 

88 _db.create_all() 

89 

90 # Insert admin, non admin, and unconfirmed 

91 _db.session.add(non_admin_user) 

92 _db.session.add(admin_user) 

93 _db.session.add(unconfirmed_user) 

94 

95 # add the default settings 

96 with open("config.json") as config: 

97 config = json.load(config) 

98 for name, value in config["settings"].items(): 

99 s = Settings(setting=name, value=value) 

100 _db.session.add(s) 

101 

102 # Commit the changes for the users 

103 _db.session.commit() 

104 

105 yield _db # this is where the testing happens! 

106 

107 _db.drop_all() 

108 

109 

110@pytest.fixture(scope="function", autouse=True) 

111def db_session(db): 

112 """ 

113 Creates a new database session for a test. Note you must use this fixture 

114 if your test connects to db. Autouse is set to true which implies 

115 that the fixture will be setup before each test 

116 

117 Here we not only support commit calls but also rollback calls in tests. 

118 """ 

119 connection = db.engine.connect() 

120 transaction = connection.begin() 

121 options = dict(bind=connection, binds={}) 

122 session = db.create_scoped_session(options=options) 

123 db.session = session 

124 

125 yield session 

126 

127 transaction.rollback() 

128 connection.close() 

129 session.remove() 

130 

131 

132@pytest.fixture 

133def login_unconfirmed_user(auth, unconfirmed_user): 

134 """Login with unconfirmed and logout during teadown""" 

135 auth.login(unconfirmed_user) 

136 yield 

137 auth.logout() 

138 

139 

140@pytest.fixture 

141def login_admin_user(auth, admin_user): 

142 """Login with admin and logout during teadown""" 

143 auth.login(admin_user) 

144 yield 

145 auth.logout() 

146 

147 

148@pytest.fixture 

149def login_non_admin_user(auth, non_admin_user): 

150 """Login with non-admin and logout during teadown""" 

151 auth.login(non_admin_user) 

152 yield 

153 auth.logout() 

154 

155 

156@pytest.fixture 

157def auth(test_client): 

158 return AuthActions(test_client) 

159 

160 

161class AuthActions: 

162 def __init__(self, client): 

163 self._client = client 

164 

165 def login(self, user, password="pass"): 

166 return self._client.post( 

167 url_for("auth.login"), 

168 data=dict(email=user.email, password=password), 

169 follow_redirects=True, 

170 ) 

171 

172 def logout(self): 

173 return self._client.get(url_for("auth.logout"), follow_redirects=True) 

174 

175 

176# Want TO USE THE BELOW 2 FIXTURES TO DYNAMICALLY 

177# GET THE ROUTES FOR A GIVEN MODULE BUT UNABLE TO 

178# PARAMETERIZE THE LIST OF ROUTES RETURNED FROM THE FIXTURE 

179# CURRENTLY THIS NOT POSSIBLE WITH FIXTURES IN PYTEST @rehmanis 

180 

181# @pytest.fixture(scope="module") 

182# def get_module_routes(request, get_routes): 

183# module_prefix = getattr(request.module, "module_prefix", "/") 

184# return get_routes[module_prefix] 

185 

186 

187# @pytest.fixture(scope="session") 

188# def get_routes(flask_app): 

189 

190# routes_dict = {} 

191# relative_path = "/" 

192# prefix = "/" 

193 

194# for route in flask_app.url_map.iter_rules(): 

195# split_route = list(filter(None, str(route).split("/", 2))) 

196 

197# if len(split_route) == 0: 

198# prefix = "/" 

199# relative_path = "" 

200# elif len(split_route) == 1: 

201# prefix = "/" + split_route[0] 

202# relative_path = "/" 

203# else: 

204# prefix = "/" + split_route[0] 

205# relative_path = split_route[1] 

206 

207# if prefix in routes_dict: 

208# routes_dict[prefix].append(relative_path) 

209# else: 

210# routes_dict[prefix] = [relative_path] 

211 

212# return routes_dict