forked from CenterForOpenScience/osf.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_institution_department_list.py
More file actions
163 lines (137 loc) · 5.05 KB
/
test_institution_department_list.py
File metadata and controls
163 lines (137 loc) · 5.05 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
import pytest
import datetime
from api.base.settings.defaults import API_BASE, DEFAULT_ES_NULL_VALUE
from osf_tests.factories import (
InstitutionFactory,
AuthUserFactory,
)
from osf.metrics.reports import InstitutionalUserReport
from osf.metrics.utils import YearMonth
@pytest.mark.es_metrics
@pytest.mark.django_db
class TestInstitutionDepartmentList:
@pytest.fixture()
def institution(self):
return InstitutionFactory()
@pytest.fixture()
def user(self):
return AuthUserFactory()
@pytest.fixture()
def user2(self):
return AuthUserFactory()
@pytest.fixture()
def user3(self):
return AuthUserFactory()
@pytest.fixture()
def user4(self):
return AuthUserFactory()
@pytest.fixture()
def populate_counts(self, user, user2, user3, user4, admin, institution):
# This represents a Department that had a user, but no longer has any users, so does not appear in results.
InstitutionalUserReport(
report_yearmonth=YearMonth(2017, 2),
user_id=user._id,
institution_id=institution._id,
department_name='Old Department',
public_project_count=1,
private_project_count=1,
).save()
_this_month = YearMonth.from_date(datetime.date.today())
# The user has left the department
InstitutionalUserReport(
report_yearmonth=_this_month,
user_id=user._id,
institution_id=institution._id,
department_name='New Department',
public_project_count=1,
private_project_count=1,
).save()
# A second user entered the department
InstitutionalUserReport(
report_yearmonth=_this_month,
user_id=user2._id,
institution_id=institution._id,
department_name='New Department',
public_project_count=1,
private_project_count=1,
).save()
# A new department with a single user to test sorting
InstitutionalUserReport(
report_yearmonth=_this_month,
user_id=user3._id,
institution_id=institution._id,
department_name='Smaller Department',
public_project_count=1,
private_project_count=1,
).save()
# A user with no department
InstitutionalUserReport(
report_yearmonth=_this_month,
user_id=user4._id,
institution_id=institution._id,
public_project_count=1,
private_project_count=1,
).save()
@pytest.fixture()
def admin(self, institution):
user = AuthUserFactory()
group = institution.get_group('institutional_admins')
group.user_set.add(user)
group.save()
return user
@pytest.fixture()
def url(self, institution):
return f'/{API_BASE}institutions/{institution._id}/metrics/departments/'
def test_auth(self, app, url, user, admin):
resp = app.get(url, expect_errors=True)
assert resp.status_code == 401
resp = app.get(url, auth=user.auth, expect_errors=True)
assert resp.status_code == 403
resp = app.get(url, auth=admin.auth)
assert resp.status_code == 200
assert resp.json['data'] == []
def test_get(self, app, url, admin, institution, populate_counts):
InstitutionalUserReport._get_connection().indices.refresh(InstitutionalUserReport._template_pattern)
resp = app.get(url, auth=admin.auth)
assert resp.json['data'] == [{
'id': f'{institution._id}-New-Department',
'type': 'institution-departments',
'attributes': {
'name': 'New Department',
'number_of_users': 2
},
'links': {},
}, {
'id': f'{institution._id}-{DEFAULT_ES_NULL_VALUE}',
'type': 'institution-departments',
'attributes': {
'name': DEFAULT_ES_NULL_VALUE,
'number_of_users': 1
},
'links': {},
}, {
'id': f'{institution._id}-Smaller-Department',
'type': 'institution-departments',
'attributes': {
'name': 'Smaller Department',
'number_of_users': 1
},
'links': {},
}]
# Tests CSV Export
headers = {
'accept': 'text/csv'
}
resp = app.get(url, auth=admin.auth, headers=headers)
assert resp.status_code == 200
assert resp.headers['Content-Type'] == 'text/csv; charset=utf-8'
response_body = resp.text
rows = response_body.split('\r\n')
header_row = rows[0].split(',')
new_department_row = rows[1].split(',')
na_row = rows[2].split(',')
smaller_department_row = rows[3].split(',')
assert header_row == ['name', 'number_of_users']
assert new_department_row == ['New Department', '2']
assert smaller_department_row == ['Smaller Department', '1']
assert na_row == ['N/A', '1']