test_engage_old_users.py
5.86 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# -*- 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.jobs import get_inactive_users_jobs, get_non_contributors_users_jobs
from default import Test, with_context
from factories import TaskRunFactory, UserFactory
from pybossa.core import user_repo
import datetime
# from mock import patch, MagicMock
class TestEngageUsers(Test):
@with_context
def test_get_inactive_users_jobs_no_users(self):
"""Test JOB get without users returns empty list."""
jobs_generator = get_inactive_users_jobs()
jobs = []
for job in jobs_generator:
jobs.append(job)
msg = "There should not be any job."
assert len(jobs) == 0, msg
@with_context
def test_get_inactive_users_jobs_with_users(self):
"""Test JOB get with users returns empty list."""
TaskRunFactory.create()
jobs_generator = get_inactive_users_jobs()
jobs = []
for job in jobs_generator:
jobs.append(job)
msg = "There should not be any job."
assert len(jobs) == 0, msg
@with_context
def test_get_inactive_users_returns_jobs(self):
"""Test JOB get inactive users returns a list of jobs."""
today = datetime.datetime.today()
old_date = today - datetime.timedelta(days=120)
date_str = old_date.strftime('%Y-%m-%dT%H:%M:%S.%f')
one_year = today - datetime.timedelta(days=365)
one_year_str = one_year.strftime('%Y-%m-%dT%H:%M:%S.%f')
user = UserFactory.create()
# 3 months old contribution
tr = TaskRunFactory.create(finish_time=date_str)
# 1 year old contribution
TaskRunFactory.create(finish_time=one_year_str)
# User with a contribution from a long time ago
TaskRunFactory.create(finish_time="2010-08-08T18:23:45.714110",
user=user)
# User with a recent contribution
TaskRunFactory.create(user=user)
user = user_repo.get(tr.user_id)
jobs_generator = get_inactive_users_jobs()
jobs = []
for job in jobs_generator:
jobs.append(job)
msg = "There should be one job."
print jobs
assert len(jobs) == 1, msg
job = jobs[0]
args = job['args'][0]
assert job['queue'] == 'quaterly', job['queue']
assert len(args['recipients']) == 1
assert args['recipients'][0] == user.email_addr, args['recipients'][0]
assert "UNSUBSCRIBE" in args['body']
assert "Update" in args['html']
@with_context
def test_get_inactive_users_returns_jobs_unsubscribed(self):
"""Test JOB get inactive users returns an empty list of jobs."""
tr = TaskRunFactory.create(finish_time="2010-07-07T17:23:45.714210")
user = user_repo.get(tr.user_id)
user.subscribed = False
user_repo.update(user)
jobs_generator = get_inactive_users_jobs()
jobs = []
for job in jobs_generator:
jobs.append(job)
msg = "There should be zero jobs."
assert len(jobs) == 0, msg
class TestNonContributors(Test):
@with_context
def test_get_non_contrib_users_jobs_no_users(self):
"""Test JOB get without users returns empty list."""
jobs_generator = get_non_contributors_users_jobs()
jobs = []
for job in jobs_generator:
jobs.append(job)
msg = "There should not be any job."
assert len(jobs) == 0, msg
@with_context
def test_get_non_contrib_users_jobs_with_users(self):
"""Test JOB get with users returns empty list."""
TaskRunFactory.create()
user = user_repo.get(1)
jobs_generator = get_non_contributors_users_jobs()
jobs = []
for job in jobs_generator:
jobs.append(job)
msg = "There should not be any job."
assert len(jobs) == 1, msg
job = jobs[0]
args = job['args'][0]
assert args['recipients'][0] == user.email_addr, args['recipients'][1]
@with_context
def test_get_non_contrib_users_returns_jobs(self):
"""Test JOB get non contrib users returns a list of jobs."""
TaskRunFactory.create()
user = user_repo.get(1)
jobs_generator = get_non_contributors_users_jobs()
jobs = []
for job in jobs_generator:
jobs.append(job)
msg = "There should be one job."
print jobs
assert len(jobs) == 1, msg
job = jobs[0]
args = job['args'][0]
assert job['queue'] == 'quaterly', job['queue']
assert len(args['recipients']) == 1
assert args['recipients'][0] == user.email_addr, args['recipients'][0]
assert "UNSUBSCRIBE" in args['body']
assert "Update" in args['html']
@with_context
def test_get_non_contrib_users_returns_unsubscribed_jobs(self):
"""Test JOB get non contrib users returns a list of jobs."""
TaskRunFactory.create()
user = user_repo.get(1)
user.subscribed = False
user_repo.update(user)
jobs_generator = get_non_contributors_users_jobs()
jobs = []
for job in jobs_generator:
jobs.append(job)
msg = "There should be zero jobs."
assert len(jobs) == 0, msg