def show_posts(user_id): '''Displays all existing likes made by a user. Args: user_id: the users id ''' user = User(id=user_id) user.load_posts(include=['preview', 'tags'], page_size=0) tags = combine_tags(user.posts) return render_template('gallery.html', posts=user.posts, tags=tags)
def show_comments(user_id): '''Displays all existing comments made by a user. Args: user_id: the users id ''' user = User(id=user_id) user.load_comments(include=['post.preview', 'user'], fields={'users': ['username']}, page_size=0) return render_template('comments.html', comments=user.comments)
class TestShowComments(object): comments_uri = mappers.resource_uri(User(id=1), 'comments') comments = utils.load_test_data( 'show_comments.json')['comments_with_previews'] def test_no_user(self, client): with requests_mock.Mocker() as mock: mock.get(self.comments_uri, status_code=404, json=utils.error_response('User', 1)) response = client.get(url_for('userinfo.show_comments', user_id=1)) assert response.status_code == 200 def test_user_no_comments(self, client): with requests_mock.Mocker() as mock: mock.get(self.comments_uri, json={'data': []}) response = client.get(url_for('userinfo.show_comments', user_id=1)) assert response.status_code == 200 def test_user_with_comments(self, client): with requests_mock.Mocker() as mock: mock.get(self.comments_uri, json=self.comments) response = client.get(url_for('userinfo.show_comments', user_id=1)) assert response.status_code == 200
class TestShowUsers(object): users_uri = mappers.collection_uri(User()) test_data = utils.load_test_data('show_users.json') def test_no_users(self, client): with requests_mock.Mocker() as mock: mock.get(self.users_uri, json={'data': []}) response = client.get(url_for('userinfo.show_users')) assert response.status_code == 200
def test_signup_new_user(client, mocker, user_exists, authenticated): user = User(**test_user()).dump() mocker.patch('benwaonline.auth.views.verify_token', return_value=auth_payload()) with requests_mock.Mocker() as mock: mock.get('/api/users', json=user_exists) mock.post('/api/users', json=user, status_code=201) resp = signup(client, 'access token') assert resp.status_code == 302 assert current_user.is_authenticated == authenticated
class TestShowUser(object): user_uri = mappers.instance_uri(User(id=1)) posts_uri = mappers.resource_uri(User(id=1), 'posts') likes_uri = mappers.resource_uri(User(id=1), 'likes') test_data = utils.load_test_data('show_user.json') def test_no_posts(self, client): user = User(id=1) result = render_template('user.html', user=user) assert 0 == result.count('gallery/show/') @pytest.mark.usefixtures('cache') def test_no_user(self, client): with requests_mock.Mocker() as mock: mock.get('/api/users/1', status_code=404, json=utils.error_response('User', 1)) response = client.get(url_for('userinfo.show_user', user_id=1)) assert response.status_code == 200 @pytest.mark.usefixtures('cache') def test_no_user_raises_exception(self): with requests_mock.Mocker() as mock: mock.get('/api/users/1', status_code=404, json=utils.error_response('User', 1)) with pytest.raises(BenwaOnlineError): show_user(user_id=1) @pytest.mark.usefixtures('cache') def test_show_user(self, client): user = self.test_data['user'] posts = self.test_data['user_posts'] with requests_mock.Mocker() as mock: mock.get(self.user_uri, json=user) mock.get(self.posts_uri, json=posts) mock.get(self.likes_uri, json=posts) response = client.get(url_for('userinfo.show_user', user_id=1)) assert response.status_code == 200
def test_authorize_callback(client, mocker, auth_resp, user_data, next_url): users_uri = mappers.collection_uri(User()) user = UserSchema(many=True).dump(user_data).data mocker.patch('benwaonline.auth.views.verify_token', return_value=auth_payload()) mocker.patch('benwaonline.auth.views.handle_authorize_response', return_value=auth_resp) with requests_mock.Mocker() as mock: mock.get(users_uri, json=user) response = authenticate(client, mocker) assert response.status_code == 302 assert next_url in response.headers['location']
def test_no_posts(self, client): user = User(id=1) result = render_template('user.html', user=user) assert 0 == result.count('gallery/show/')
def test_entity_criteria(): user = User(username='******') c1 = EntityCriteria('any', user) assert c1.to_filter() == {'name': 'username', 'op': 'any', 'val': 'Benwa'}