test_model_user.py
3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# -*- coding: utf8 -*-
# This file is part of PyBossa.
#
# Copyright (C) 2015 SciFabric LTD.
#
# PyBossa is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# PyBossa is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with PyBossa. If not, see <http://www.gnu.org/licenses/>.
from default import Test, db, with_context
from nose.tools import assert_raises
from sqlalchemy.exc import IntegrityError
from pybossa.model.user import User
class TestModelUser(Test):
@with_context
def test_user(self):
"""Test USER model."""
# First user
user = User(
email_addr="john.doe@example.com",
name="johndoe",
fullname="John Doe",
locale="en")
user2 = User(
email_addr="john.doe2@example.com",
name="johndoe2",
fullname="John Doe2",
locale="en",)
db.session.add(user)
db.session.commit()
tmp = db.session.query(User).get(1)
assert tmp.email_addr == user.email_addr, tmp
assert tmp.name == user.name, tmp
assert tmp.fullname == user.fullname, tmp
assert tmp.locale == user.locale, tmp
assert tmp.api_key is not None, tmp
assert tmp.created is not None, tmp
err_msg = "First user should be admin"
assert tmp.admin is True, err_msg
err_msg = "check_password method should return False"
assert tmp.check_password(password="nothing") is False, err_msg
db.session.add(user2)
db.session.commit()
tmp = db.session.query(User).get(2)
assert tmp.email_addr == user2.email_addr, tmp
assert tmp.name == user2.name, tmp
assert tmp.fullname == user2.fullname, tmp
assert tmp.locale == user2.locale, tmp
assert tmp.api_key is not None, tmp
assert tmp.created is not None, tmp
err_msg = "Second user should be not an admin"
assert tmp.admin is False, err_msg
@with_context
def test_user_errors(self):
"""Test USER model errors."""
user = User(
email_addr="john.doe@example.com",
name="johndoe",
fullname="John Doe",
locale="en")
# User.name should not be nullable
user.name = None
db.session.add(user)
assert_raises(IntegrityError, db.session.commit)
db.session.rollback()
# User.fullname should not be nullable
user.name = "johndoe"
user.fullname = None
db.session.add(user)
assert_raises(IntegrityError, db.session.commit)
db.session.rollback()
# User.email_addr should not be nullable
user.name = "johndoe"
user.fullname = "John Doe"
user.email_addr = None
db.session.add(user)
assert_raises(IntegrityError, db.session.commit)
db.session.rollback()