# Coverage for modules/box__default/auth/tests/test_auth_models.py : 100%

## 183 statements

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

2 This file (test_auth_models.py) contains the units tests for

3 the all `auth` module's models.

4"""

5 import datetime as dt

6

7 import pytest

8 from freezegun import freeze_time

9 from modules.box__default.auth.models import AnonymousUser

10 from modules.box__default.auth.models import Role

11 from modules.box__default.auth.models import role_user_bridge

12 from modules.box__default.auth.models import User

13 from modules.box__default.auth.tests.factories import RoleFactory

14 from modules.box__default.auth.tests.factories import UserFactory

15 from sqlalchemy.exc import IntegrityError

16

17

18 class TestAuthFactory:

19 """Test all Model Factories in Auth"""

20

21 def test_user_factory(self):

22 user = UserFactory()

23 retrived = user.query.get(user.id)

24

25 assert retrived is not None

26 assert bool(retrived.username)

27 assert bool(retrived.email)

28 assert bool(retrived.password)

29 assert retrived.is_email_confirmed is True

30 assert retrived.is_admin is False

31 assert retrived.check_password("pass")

32

33 def test_role_factory(self):

34 role = RoleFactory()

35 retrived = Role.query.get(role.id)

36

37 assert retrived is not None

38 assert bool(retrived.name)

39

40

41 class TestAnonymousUser:

42 """tests app's anonymous users characteristics"""

43

44 def test_anonymous_user_is_admin(self):

45 user = AnonymousUser()

46

47 assert user.is_admin is False

48

49 @pytest.mark.parametrize(

50 "email_config",

51 [

52 ("EMAIL_CONFIRMATION_DISABLED", "remove"),

53 ("EMAIL_CONFIRMATION_DISABLED", False),

54 ("EMAIL_CONFIRMATION_DISABLED", None),

55 ("EMAIL_CONFIRMATION_DISABLED", "random string"),

56 ],

57 indirect=True,

58 )

59 def test_anonymous_user_email_confirmed_disabled(self, email_config):

60 user = AnonymousUser()

61

62 assert user.is_email_confirmed is False

63

64 @pytest.mark.parametrize(

65 "email_config",

66 [

67 ("EMAIL_CONFIRMATION_DISABLED", True),

68 ],

69 indirect=True,

70 )

71 def test_anonymous_user_has_email_confirmed_enabled(self, email_config):

72 user = AnonymousUser()

73

74 assert user.is_email_confirmed is True

75

76 def test_anonymous_username_is_set(self):

77 user = AnonymousUser()

78

79 assert bool(user.username)

80

81 def test_anonymous_email_is_set(self):

82 user = AnonymousUser()

83

84 assert bool(user.email)

85

86 def test_anonymous_user_representation(self):

87

88 user = AnonymousUser()

89

90 assert repr(user) == "<AnonymousUser guest>"

91

92

93 class TestUser:

94 """Test User model"""

95

96 def test_get_user_by_id(self):

97 user = User.create(email="foo@bar.com", password="pass")

98 retrieved = User.get_by_id(user.id)

99

100 assert retrieved == user

101

102 def test_email_is_unique(self):

103 User.create(email="foo@bar.com", password="pass")

104

105 with pytest.raises(IntegrityError):

106 User.create(email="foo@bar.com", password="another")

107

108 def test_username_is_unique(self):

109 User.create(username="foo", email="foo@bar.com", password="pass")

110

111 with pytest.raises(IntegrityError):

112 User.create(username="foo", email="bar@bar.com", password="pass")

113

114 def test_email_is_not_nullable(self):

115 with pytest.raises(IntegrityError):

116 User.create(username="foo", password="pass")

117

118 def test_password_is_not_nullable(self):

119 with pytest.raises(IntegrityError):

120 User.create(username="foo")

121

122 def test_user_is_not_admin_by_default(self):

123 user = User.create(email="foo@bar.com", password="pass")

124

125 assert user.is_admin is False

126

127 def test_date_registered_at_defaults_to_datetime(self):

128 user = User.create(email="foo@bar.com", password="pass")

129

130 assert bool(user.date_registered)

131 assert isinstance(user.date_registered, dt.datetime)

132

133 def test_email_is_not_confirmed_by_default(self):

134 user = User.create(email="foo@bar.com", password="pass")

135

136 assert user.is_email_confirmed is False

137

138 def test_check_password(self):

139 user = User.create(email="foo@bar.com", password="foobar123$")

140

141 assert user.check_password("foobaz123") is False

142 assert user.check_password("foobar123$")

143

144 def test_password_hashed_on_user_creation(self):

145 user = User.create(email="foo@bar.com", password="foobar123$")

146

147 assert user.password != "foobar123$"

148

149 def test_user_representation(self):

150 user = User.create(email="foo@bar.com", password="foobar123$")

151

152 assert repr(user) == f"<User-id: {user.id}, User-email: foo@bar.com>"

153

154 def test_valid_token_confirms_email(self):

155 user = User()

156 user.email = "foo@bar.com"

157 user.password = "pass"

158 user.is_email_confirmed = False

159 user.save()

160 token = user.generate_confirmation_token()

161 confirm_status = user.confirm_token(token)

162

163 assert confirm_status is True

164 assert user.is_email_confirmed

165

166 @freeze_time("Jan 14th, 2020", auto_tick_seconds=200)

167 def test_expired_token_does_not_confirm_email(self):

168 user = User.create(email="foo@bar.com", password="foobar123$")

169 token = user.generate_confirmation_token()

170 confirm_status = user.confirm_token(token, expiration=120)

171

172 assert confirm_status is False

173 assert not user.is_email_confirmed

174

175 def test_email_should_only_be_confirmed_from_same_email(self):

176 user1 = UserFactory(is_email_confirmed=False)

177 user2 = UserFactory(is_email_confirmed=False)

178 token = user1.generate_confirmation_token()

179 confirm_status = user2.confirm_token(token)

180

181 assert confirm_status is False

182 assert not user2.is_email_confirmed

183 assert not user1.is_email_confirmed

184

185

186 class TestRole:

187 """Test Role model"""

188

189 def test_get_role_by_id(self):

190 role = Role.create(name="buyer")

191 retrieved = Role.get_by_id(role.id)

192

193 assert retrieved == role

194

195 def test_role_representation(self):

196 role = Role.create(name="buyer")

197

198 assert repr(role) == f"<Role-id: {role.id}, Role-name: buyer>"

199

200

201 class TestUserRoleRelation:

202 """Test User and Role model relationship"""

203

204 def test_user_can_have_many_roles(self):

205 user = UserFactory()

206 roles = RoleFactory.create_batch(2)

207 user.roles = roles

208 user.save()

209 retrived_role1 = Role.query.get(roles[0].id)

210 retrived_role2 = Role.query.get(roles[1].id)

211

212 assert retrived_role1 is not None

213 assert retrived_role2 is not None

214 assert retrived_role1.users[0] == user

215 assert retrived_role2.users[0] == user

216

217 def test_role_can_have_many_users(self):

218 role = RoleFactory()

219 users = UserFactory.create_batch(2)

220 role.users = users

221 role.save()

222 retrived_user1 = User.query.get(users[0].id)

223 retrived_user2 = User.query.get(users[1].id)

224

225 assert retrived_user1 is not None

226 assert retrived_user2 is not None

227 assert retrived_user1.roles[0] == role

228 assert retrived_user2.roles[0] == role

229

230 def test_deleting_role_does_not_remove_user(self):

231 user = UserFactory()

232 roles = RoleFactory.create_batch(3)

233 user.roles = roles

234 user.save()

235 roles[0].delete()

236 retrived_roles = Role.query.all()

237 retrived_user = User.get_by_id(user.id)

238

239 assert retrived_roles is not None

240 assert retrived_user is not None

241 assert len(retrived_roles) == 2

242 assert len(retrived_user.roles) == 2

243

244 def test_delete_user_does_not_remove_role(self):

245 users = UserFactory.create_batch(3)

246 user_ids = [user.id for user in users]

247 role = RoleFactory()

248 role.users = users

249 role.save()

250 users[0].delete()

251 retrived_role = Role.get_by_id(role.id)

252 retrived_users = User.query.filter(User.id.in_(user_ids)).all()

253

254 assert retrived_role is not None

255 assert retrived_users is not None

256 assert len(retrived_users) == 2

257 assert len(retrived_role.users) == 2

258

259 def test_role_user_bridge_cascades_on_delete(self):

260 user1 = UserFactory()

261 user2 = UserFactory()

262 roles = RoleFactory.create_batch(3)

263 user1.roles = roles

264 user2.roles = roles

265 user1.save()

266 user2.save()

267 user1.delete()

268 roles[0].delete()

269

270 users_in_bridge = User.query.join(role_user_bridge).join(Role).all()

271 roles_in_bridge = Role.query.join(role_user_bridge).join(User).all()

272

273 assert roles_in_bridge is not None

274 assert users_in_bridge is not None

275 assert len(roles_in_bridge) == 2

276 assert len(users_in_bridge) == 1