test_old_projects.py
5.69 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# -*- 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 pybossa.core import project_repo
from pybossa.jobs import warn_old_project_owners, get_non_updated_projects
from default import Test, with_context
from factories import ProjectFactory, TaskFactory
from mock import patch, MagicMock
class TestOldProjects(Test):
@with_context
def test_get_non_updated_projects_returns_none(self):
"""Test JOB get non updated returns none."""
projects = get_non_updated_projects()
err_msg = "There should not be any outdated project."
assert len(projects) == 0, err_msg
@with_context
def test_get_non_updated_projects_returns_one_project(self):
"""Test JOB get non updated returns one project."""
project = ProjectFactory.create(updated='2010-10-22T11:02:00.000000')
projects = get_non_updated_projects()
err_msg = "There should be one outdated project."
assert len(projects) == 1, err_msg
assert projects[0].name == project.name, err_msg
@with_context
@patch('pybossa.cache.projects.clean')
@patch('pybossa.core.mail')
def test_warn_project_owner(self, mail, clean_mock):
"""Test JOB email is sent to warn project owner."""
# Mock for the send method
send_mock = MagicMock()
send_mock.send.return_value = True
# Mock for the connection method
connection = MagicMock()
connection.__enter__.return_value = send_mock
# Join them
mail.connect.return_value = connection
date = '2010-10-22T11:02:00.000000'
project = ProjectFactory.create(updated=date)
project_id = project.id
warn_old_project_owners()
project = project_repo.get(project_id)
err_msg = "mail.connect() should be called"
assert mail.connect.called, err_msg
err_msg = "conn.send() should be called"
assert send_mock.send.called, err_msg
err_msg = "project.contacted field should be True"
assert project.contacted, err_msg
err_msg = "project.published field should be False"
assert project.published is False, err_msg
err_msg = "cache of project should be cleaned"
clean_mock.assert_called_with(project_id), err_msg
err_msg = "The update date should be different"
assert project.updated != date, err_msg
@with_context
@patch('pybossa.cache.projects.clean')
def test_warn_project_owner_two(self, clean_mock):
"""Test JOB email is sent to warn project owner."""
from pybossa.core import mail
with mail.record_messages() as outbox:
date = '2010-10-22T11:02:00.000000'
project = ProjectFactory.create(updated=date)
project_id = project.id
warn_old_project_owners()
project = project_repo.get(project_id)
assert len(outbox) == 1, outbox
subject = 'Your PyBossa project: %s has been inactive' % project.name
assert outbox[0].subject == subject
err_msg = "project.contacted field should be True"
assert project.contacted, err_msg
err_msg = "project.published field should be False"
assert project.published is False, err_msg
err_msg = "cache of project should be cleaned"
clean_mock.assert_called_with(project_id), err_msg
err_msg = "The update date should be different"
assert project.updated != date, err_msg
@with_context
@patch('pybossa.cache.projects.clean')
def test_warn_project_excludes_completed_projects(self, clean_mock):
"""Test JOB email excludes completed projects."""
from pybossa.core import mail
with mail.record_messages() as outbox:
date = '2010-10-22T11:02:00.000000'
project = ProjectFactory.create(updated=date, contacted=False)
TaskFactory.create(created=date, project=project, state='completed')
project_id = project.id
project = project_repo.get(project_id)
project.updated = date
project_repo.update(project)
project = ProjectFactory.create(updated=date, contacted=False)
TaskFactory.create(created=date, project=project, state='ongoing')
project_id = project.id
project = project_repo.get(project_id)
project.updated = date
project_repo.update(project)
warn_old_project_owners()
assert len(outbox) == 1, outbox
subject = 'Your PyBossa project: %s has been inactive' % project.name
assert outbox[0].subject == subject
err_msg = "project.contacted field should be True"
assert project.contacted, err_msg
err_msg = "project.published field should be False"
assert project.published is False, err_msg
err_msg = "cache of project should be cleaned"
clean_mock.assert_called_with(project_id), err_msg
err_msg = "The update date should be different"
assert project.updated != date, err_msg