-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(stats): implement user can view read stats
- implement read count is logged when authenticated users read an article - implement authenticated users can view read count - implement article owners can view people who have read their articles - read count and readers returned when getting an article [Delivers #165305284]
- Loading branch information
Showing
6 changed files
with
188 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Generated by Django 2.2 on 2019-05-15 12:10 | ||
|
||
from django.conf import settings | ||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
('articles', '0029_auto_20190513_1142'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='ReadStatsModel', | ||
fields=[ | ||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('article', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='read_article', to='articles.ArticleModel')), | ||
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='read_user', to=settings.AUTH_USER_MODEL)), | ||
], | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
""" | ||
Reader statistics tests | ||
""" | ||
import json | ||
from .base_test import BaseTest | ||
from .data import Data | ||
|
||
|
||
class HighlightArticleTestcase(BaseTest): | ||
""" | ||
This class defines the test suite for like and dislike | ||
cases. | ||
""" | ||
|
||
def setUp(self): | ||
""" Define the test client and required test variables. """ | ||
|
||
BaseTest.setUp(self) | ||
data = self.base_data.user_data2 | ||
signup = self.signup_user() | ||
signup2 = self.signup_user(data) | ||
|
||
uid = signup.data.get('data')['id'] | ||
token = signup.data.get('data')['token'] | ||
uid_2 = signup2.data.get('data')['id'] | ||
token_2 = signup2.data.get('data')['token'] | ||
|
||
self.activate_user(uid=uid, token=token) | ||
self.activate_user(uid=uid_2, token=token_2) | ||
self.base_data = Data() | ||
|
||
login = self.login_user() | ||
self.token = login.data['token'] | ||
self.control_token = self.login_user_and_get_token(data) | ||
|
||
def test_can_add_article_read_count_if_user_authenticated(self): | ||
""" | ||
Test that a reading count is added after getting | ||
an article by an authenticated user | ||
""" | ||
article = self.create_article() | ||
slug = article.data['data']['slug'] | ||
|
||
response = self.client.get('/api/articles/{}/'.format(slug), | ||
HTTP_AUTHORIZATION='Bearer ' + | ||
self.token) | ||
res = json.loads(response.content.decode('utf-8')) | ||
|
||
self.assertEqual(res['data']['read_count'], 1) | ||
|
||
def test_cannot_add_article_read_count_if_user_not_authenticated(self): | ||
""" | ||
Test that a reading count is not added after getting | ||
an article by an unauthenticated user | ||
""" | ||
article = self.create_article() | ||
slug = article.data['data']['slug'] | ||
|
||
response = self.client.get('/api/articles/{}/'.format(slug)) | ||
res = json.loads(response.content.decode('utf-8')) | ||
|
||
self.assertEqual(res['data']['read_count'], None) | ||
|
||
def test_can_get_article_readers_if_owner(self): | ||
""" | ||
Test that the author can view the people who have | ||
read the article | ||
""" | ||
article = self.create_article() | ||
slug = article.data['data']['slug'] | ||
|
||
response = self.client.get('/api/articles/{}/'.format(slug), | ||
HTTP_AUTHORIZATION='Bearer ' + | ||
self.token) | ||
res = json.loads(response.content.decode('utf-8')) | ||
|
||
self.assertEqual(res['data']['article_readers'][0], 'Alpha') | ||
|
||
def test_canot_get_article_readers_if_not_owner(self): | ||
""" | ||
Test that one cannot view the people who have | ||
read an article if they are not the author | ||
""" | ||
article = self.create_article() | ||
slug = article.data['data']['slug'] | ||
|
||
response = self.client.get('/api/articles/{}/'.format(slug), | ||
HTTP_AUTHORIZATION='Bearer ' + | ||
self.control_token) | ||
res = json.loads(response.content.decode('utf-8')) | ||
|
||
self.assertEqual(res['data']['article_readers'], None) |