/
test_user.py
241 lines (201 loc) · 10.1 KB
/
test_user.py
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import listenbrainz.db.stats as db_stats
import listenbrainz.db.user as db_user
import ujson
from unittest import mock
from flask import url_for, current_app
from influxdb import InfluxDBClient
from listenbrainz.db.testing import DatabaseTestCase
from listenbrainz.listenstore.tests.util import create_test_data_for_influxlistenstore
from listenbrainz.webserver.influx_connection import init_influx_connection
from listenbrainz.webserver.login import User
from listenbrainz.webserver.testing import ServerTestCase
import listenbrainz.db.user as db_user
import logging
class UserViewsTestCase(ServerTestCase, DatabaseTestCase):
def setUp(self):
ServerTestCase.setUp(self)
DatabaseTestCase.setUp(self)
self.log = logging.getLogger(__name__)
self.influx = InfluxDBClient(
host=current_app.config['INFLUX_HOST'],
port=current_app.config['INFLUX_PORT'],
database=current_app.config['INFLUX_DB_NAME'],
)
self.influx.query('''create database %s''' % current_app.config['INFLUX_DB_NAME'])
self.logstore = init_influx_connection(self.log, {
'REDIS_HOST': current_app.config['REDIS_HOST'],
'REDIS_PORT': current_app.config['REDIS_PORT'],
'REDIS_NAMESPACE': current_app.config['REDIS_NAMESPACE'],
'INFLUX_HOST': current_app.config['INFLUX_HOST'],
'INFLUX_PORT': current_app.config['INFLUX_PORT'],
'INFLUX_DB_NAME': current_app.config['INFLUX_DB_NAME'],
})
user = db_user.get_or_create(1, 'iliekcomputers')
db_user.agree_to_gdpr(user['musicbrainz_id'])
self.user = User.from_dbrow(user)
weirduser = db_user.get_or_create(2, 'weird\\user name')
self.weirduser = User.from_dbrow(weirduser)
def tearDown(self):
self.influx.query('''drop database %s''' % current_app.config['INFLUX_DB_NAME'])
self.logstore = None
ServerTestCase.tearDown(self)
DatabaseTestCase.tearDown(self)
def test_user_page(self):
response = self.client.get(url_for('user.profile', user_name=self.user.musicbrainz_id))
self.assert200(response)
self.assertContext('active_section', 'listens')
# check that artist count is not shown if stats haven't been calculated yet
response = self.client.get(url_for('user.profile', user_name=self.user.musicbrainz_id))
self.assert200(response)
self.assertTemplateUsed('user/profile.html')
props = ujson.loads(self.get_context_variable('props'))
self.assertIsNone(props['artist_count'])
# check that artist count is shown if stats have been calculated
db_stats.insert_user_stats(
user_id=self.user.id,
artists={},
recordings={},
releases={},
artist_count=2,
)
response = self.client.get(url_for('user.profile', user_name=self.user.musicbrainz_id))
self.assert200(response)
self.assertTemplateUsed('user/profile.html')
props = ujson.loads(self.get_context_variable('props'))
self.assertEqual(props['artist_count'], '2')
self.assertDictEqual(props['spotify'], {})
@mock.patch('listenbrainz.webserver.views.user.spotify')
def test_spotify_token_access(self, mock_domain_spotify):
response = self.client.get(url_for('user.profile', user_name=self.user.musicbrainz_id))
self.assert200(response)
self.assertTemplateUsed('user/profile.html')
props = ujson.loads(self.get_context_variable('props'))
self.assertDictEqual(props['spotify'], {})
self.temporary_login(self.user.login_id)
mock_domain_spotify.get_user_dict.return_value = {}
response = self.client.get(url_for('user.profile', user_name=self.user.musicbrainz_id))
self.assert200(response)
props = ujson.loads(self.get_context_variable('props'))
self.assertDictEqual(props['spotify'], {})
mock_domain_spotify.get_user_dict.return_value = {
'access_token': 'token',
'permission': 'permission',
}
response = self.client.get(url_for('user.profile', user_name=self.user.musicbrainz_id))
self.assert200(response)
props = ujson.loads(self.get_context_variable('props'))
mock_domain_spotify.get_user_dict.assert_called_with(self.user.id)
self.assertDictEqual(props['spotify'], {
'access_token': 'token',
'permission': 'permission',
})
response = self.client.get(url_for('user.profile', user_name=self.weirduser.musicbrainz_id))
self.assert200(response)
props = ujson.loads(self.get_context_variable('props'))
mock_domain_spotify.get_user_dict.assert_called_with(self.user.id)
self.assertDictEqual(props['spotify'], {
'access_token': 'token',
'permission': 'permission',
})
def test_scraper_username(self):
""" Tests that the username is correctly rendered in the last.fm importer """
response = self.client.get(
url_for('user.lastfmscraper', user_name=self.user.musicbrainz_id),
query_string={
'user_token': self.user.auth_token,
'lastfm_username': 'dummy',
}
)
self.assert200(response)
self.assertIn('var user_name = "iliekcomputers";', response.data.decode('utf-8'))
response = self.client.get(
url_for('user.lastfmscraper', user_name=self.weirduser.musicbrainz_id),
query_string={
'user_token': self.weirduser.auth_token,
'lastfm_username': 'dummy',
}
)
self.assert200(response)
self.assertIn('var user_name = "weird%5Cuser%20name";', response.data.decode('utf-8'))
def test_top_artists(self):
""" Tests the artist stats view """
# when no stats in db, it should redirect to the profile page
r = self.client.get(url_for('user.artists', user_name=self.user.musicbrainz_id))
self.assertRedirects(r, url_for('user.profile', user_name=self.user.musicbrainz_id))
r = self.client.get(url_for('user.artists', user_name=self.user.musicbrainz_id), follow_redirects=True)
self.assert200(r)
self.assertIn('No data calculated', r.data.decode('utf-8'))
# add some artist stats to the db
with open(self.path_to_data_file('user_top_artists.json')) as f:
artists = ujson.load(f)
# insert empty documents to check for KeyError / ISE
db_stats.insert_user_stats(
user_id=self.user.id,
artists={},
recordings={},
releases={},
artist_count=2,
)
r = self.client.get(url_for('user.artists', user_name=self.user.musicbrainz_id))
self.assert200(r)
self.assertContext('active_section', 'artists')
db_stats.insert_user_stats(
user_id=self.user.id,
artists=artists,
recordings={},
releases={},
artist_count=2,
)
r = self.client.get(url_for('user.artists', user_name=self.user.musicbrainz_id))
self.assert200(r)
self.assertContext('active_section', 'artists')
def _create_test_data(self, user_name):
test_data = create_test_data_for_influxlistenstore(user_name)
self.logstore.insert(test_data)
def test_username_case(self):
"""Tests that the username in URL is case insensitive"""
self._create_test_data('iliekcomputers')
response1 = self.client.get(url_for('user.profile', user_name='iliekcomputers'))
self.assertContext('user', self.user)
response2 = self.client.get(url_for('user.profile', user_name='IlieKcomPUteRs'))
self.assertContext('user', self.user)
self.assert200(response1)
self.assert200(response2)
@mock.patch('listenbrainz.webserver.views.user.time')
@mock.patch('listenbrainz.webserver.influx_connection._influx.fetch_listens')
def test_ts_filters(self, influx, m_time):
"""Check that max_ts and min_ts are passed to the influx """
# If no parameter is given, use current time as the to_ts
m_time.time.return_value = 1520946608
self.client.get(url_for('user.profile', user_name='iliekcomputers'))
req_call = mock.call('iliekcomputers', limit=25, to_ts=1520946608)
influx.assert_has_calls([req_call])
influx.reset_mock()
# max_ts query param -> to_ts influx param
self.client.get(url_for('user.profile', user_name='iliekcomputers'), query_string={'max_ts': 1520946000})
req_call = mock.call('iliekcomputers', limit=25, to_ts=1520946000)
influx.assert_has_calls([req_call])
influx.reset_mock()
# min_ts query param -> from_ts influx param
self.client.get(url_for('user.profile', user_name='iliekcomputers'), query_string={'min_ts': 1520941000})
req_call = mock.call('iliekcomputers', limit=25, from_ts=1520941000)
influx.assert_has_calls([req_call])
influx.reset_mock()
# If max_ts and min_ts set, only max_ts is used
self.client.get(url_for('user.profile', user_name='iliekcomputers'),
query_string={'min_ts': 1520941000, 'max_ts': 1520946000})
req_call = mock.call('iliekcomputers', limit=25, to_ts=1520946000)
influx.assert_has_calls([req_call])
@mock.patch('listenbrainz.webserver.influx_connection._influx.fetch_listens')
def test_ts_filters_errors(self, influx):
"""If max_ts and min_ts are not integers, show an error page"""
self._create_test_data('iliekcomputers')
response = self.client.get(url_for('user.profile', user_name='iliekcomputers'),
query_string={'max_ts': 'a'})
self.assert400(response)
self.assertIn(b'Incorrect timestamp argument max_ts: a', response.data)
response = self.client.get(url_for('user.profile', user_name='iliekcomputers'),
query_string={'min_ts': 'b'})
self.assert400(response)
self.assertIn(b'Incorrect timestamp argument min_ts: b', response.data)
influx.assert_not_called()