diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 45e8fbd6..1128ede7 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -3,9 +3,15 @@ Change Log Log of changes since the 2.0 version +[2.7.0] - 2018-02-01 + +- Disable automatic comments +- Add endpoint to allow user to post comments to changesets from inside OSMCha +- Modify user model to include message_good, message_bad and comment_feature fields + [2.6.0] - 2017-12-27 -- Post comment to OSM Changeset page when a review is added to a changeset +- Post a comment to OSM Changeset page when a review is added to a changeset (to use it, set the ENV variable ``DJANGO_ENABLE_CHANGESET_COMMENTS`` to True) - Return the correct username of users in views [2.5.0] - 2017-10-09 diff --git a/README.rst b/README.rst index c227c660..d21257d5 100644 --- a/README.rst +++ b/README.rst @@ -135,7 +135,7 @@ How to login using the OAuth api Frontend ^^^^^^^^ -`osmcha-frontend https://github.com/mapbox/osmcha-frontend`_ is one web interface +`osmcha-frontend `_ is a web interface that you can use to interact with the API. We have a django management command to get the last version of osmcha-frontend and serve it with the API. @@ -148,7 +148,7 @@ Feature creation endpoint ^^^^^^^^^^^^^^^^^^^^^^^^^ The feature creation endpoint allows only admin users to create features. You can -use the admin site to create a token to the user. +use the admin site to create a token to a user. Instances --------- @@ -166,9 +166,8 @@ Deployment Check the `Deploy `_ file for instructions on how to deploy with Heroku and Dokku. -Management Commands --------------------- +Get in contact +--------------- -1. Export a CSV of all harmful changesets - - $ python manage.py generate_harmful_csv filename.csv +If you use, deploy or are interested in help to develop OSMCha, subscribe to our +`mailing list `_. diff --git a/osmchadjango/changeset/serializers.py b/osmchadjango/changeset/serializers.py index b1e872f3..7cca624a 100644 --- a/osmchadjango/changeset/serializers.py +++ b/osmchadjango/changeset/serializers.py @@ -1,8 +1,9 @@ import json -from rest_framework.fields import ReadOnlyField, SerializerMethodField +from rest_framework.fields import ReadOnlyField, SerializerMethodField, CharField from rest_framework.serializers import ( - ModelSerializer, ListSerializer, BaseSerializer, PrimaryKeyRelatedField + ModelSerializer, ListSerializer, BaseSerializer, PrimaryKeyRelatedField, + Serializer ) from rest_framework_gis.serializers import GeoFeatureModelSerializer @@ -233,3 +234,7 @@ class ChangesetTagsSerializer(ModelSerializer): class Meta: model = Changeset fields = ('tags',) + + +class ChangesetCommentSerializer(Serializer): + comment = CharField(max_length=1000) diff --git a/osmchadjango/changeset/tasks.py b/osmchadjango/changeset/tasks.py index 9d18fda6..ac564e85 100644 --- a/osmchadjango/changeset/tasks.py +++ b/osmchadjango/changeset/tasks.py @@ -119,25 +119,6 @@ def __init__(self, user, changeset_id): self.url = 'https://api.openstreetmap.org/api/0.6/changeset/{}/comment/'.format( changeset_id ) - self.good_changeset_message = """Hello! - I reviewed your changeset on OSMCha and it looks great! - Thank you very much for your contributions to OpenStreetMap! - #REVIEWED_GOOD #OSMCHA - Published using OSMCha: https://osmcha.mapbox.com/changesets/{} - """.format(changeset_id) - self.bad_changeset_message = """Hello! - Thank you very much for your contributions to OpenStreetMap! - I reviewed your changeset on OSMCha and found some errors or elements - that could be mapped in a better way. Feel free to message me - to know more about it or visit http://learnosm.org/ to get started. - #REVIEWED_BAD #OSMCHA - Published using OSMCha: https://osmcha.mapbox.com/changesets/{} - """.format(changeset_id) - self.unchecked_changeset_message = """Hello! - My previous review of your changeset was wrong, so I'm changing its - status to unreviewed on OSMCHA. Sorry for the error. - Published using OSMCha: https://osmcha.mapbox.com/changesets/{} - """.format(changeset_id) def post_comment(self, message=None): """Post comment to changeset.""" @@ -152,31 +133,8 @@ def post_comment(self, message=None): self.changeset_id ) ) + return {'success': True} else: print("""Some error occurred and it wasn't possible to post the comment to the changeset {}.""".format(self.changeset_id)) - - def post_good_changeset_review(self, message=None): - """Post a comment to a changeset reviewed as good. If the message is not - defined, it will use the default good changeset review message. - """ - if message is None: - message = self.good_changeset_message - self.post_comment(message) - - def post_bad_changeset_review(self, message=None): - """Post a comment to a changeset reviewed as bad. If the message is not - defined, it will use the default bad changeset review message. - """ - if message is None: - message = self.bad_changeset_message - self.post_comment(message) - - def post_undo_changeset_review(self, message=None): - """Post a comment to a changeset whose review was undone. If the message - is not defined, it will use the default unchecked changeset review - message. - """ - if message is None: - message = self.unchecked_changeset_message - self.post_comment(message) + return {'success': False} diff --git a/osmchadjango/changeset/tests/test_changeset_views.py b/osmchadjango/changeset/tests/test_changeset_views.py index 3afcee26..25931632 100644 --- a/osmchadjango/changeset/tests/test_changeset_views.py +++ b/osmchadjango/changeset/tests/test_changeset_views.py @@ -1,5 +1,4 @@ import json -import mock from django.contrib.gis.geos import Polygon from django.core.urlresolvers import reverse @@ -7,7 +6,6 @@ from social_django.models import UserSocialAuth from rest_framework.test import APITestCase -import oauth2 as oauth from ...users.models import User from ...feature.tests.modelfactories import FeatureFactory @@ -658,16 +656,12 @@ def test_set_harmful_changeset_get(self): self.assertIsNone(self.changeset_2.check_user) self.assertIsNone(self.changeset_2.check_date) - @override_settings(ENABLE_POST_CHANGESET_COMMENTS=True) - @mock.patch.object(oauth.Client, 'request') - def test_set_harmful_changeset_put(self, mock_oauth_client): + def test_set_harmful_changeset_put(self): """User can set a changeset of another user as harmful with a PUT request. We can also set the tags of the changeset sending it as data. """ - mock_oauth_client.return_value = [{'status': '200'}] self.client.login(username=self.user.username, password='password') data = {'tags': [self.tag_1.id, self.tag_2.id]} - # ENABLE POST CHANGESET COMMENTS for this test response = self.client.put( reverse('changeset:set-harmful', args=[self.changeset_2.pk]), data @@ -688,13 +682,6 @@ def test_set_harmful_changeset_put(self, mock_oauth_client): self.tag_2, self.changeset_2.tags.all() ) - mock_oauth_client.assert_called_with( - 'https://api.openstreetmap.org/api/0.6/changeset/{}/comment/'.format( - self.changeset_2.pk - ), - method='POST', - body=mock.ANY - ) def test_set_harmful_changeset_with_invalid_tag_id(self): """Return a 400 error if a user try to add a invalid tag id to a changeset. @@ -745,13 +732,10 @@ def test_set_good_changeset_get(self): self.assertIsNone(self.changeset_2.check_user) self.assertIsNone(self.changeset_2.check_date) - @override_settings(ENABLE_POST_CHANGESET_COMMENTS=True) - @mock.patch.object(oauth.Client, 'request') - def test_set_good_changeset_put(self, mock_oauth_client): + def test_set_good_changeset_put(self): """User can set a changeset of another user as good with a PUT request. We can also set the tags of the changeset sending it as data. """ - mock_oauth_client.return_value = [{'status': '200'}] self.client.login(username=self.user.username, password='password') data = {'tags': [self.tag_1.id, self.tag_2.id]} response = self.client.put( @@ -773,13 +757,6 @@ def test_set_good_changeset_put(self, mock_oauth_client): self.tag_2, self.changeset_2.tags.all() ) - mock_oauth_client.assert_called_with( - 'https://api.openstreetmap.org/api/0.6/changeset/{}/comment/'.format( - self.changeset_2.pk - ), - method='POST', - body=mock.ANY - ) def test_set_good_changeset_with_invalid_tag_id(self): """Return a 400 error if a user try to add a invalid tag id to a changeset. @@ -892,10 +869,7 @@ def test_unauthenticated_response(self): self.assertEqual(self.harmful_changeset.tags.count(), 1) self.assertIn(self.tag, self.harmful_changeset.tags.all()) - @override_settings(ENABLE_POST_CHANGESET_COMMENTS=True) - @mock.patch.object(oauth.Client, 'request') - def test_uncheck_harmful_changeset(self, mock_oauth_client): - mock_oauth_client.return_value = [{'status': '200'}] + def test_uncheck_harmful_changeset(self): self.client.login(username=self.user.username, password='password') response = self.client.put( reverse('changeset:uncheck', args=[self.harmful_changeset.pk]), @@ -907,13 +881,6 @@ def test_uncheck_harmful_changeset(self, mock_oauth_client): self.assertIsNone(self.harmful_changeset.check_user) self.assertIsNone(self.harmful_changeset.check_date) self.assertEqual(self.harmful_changeset.tags.count(), 1) - mock_oauth_client.assert_called_with( - 'https://api.openstreetmap.org/api/0.6/changeset/{}/comment/'.format( - self.harmful_changeset.pk - ), - method='POST', - body=mock.ANY - ) def test_uncheck_good_changeset(self): self.client.login(username=self.user.username, password='password') diff --git a/osmchadjango/changeset/tests/test_comment_views.py b/osmchadjango/changeset/tests/test_comment_views.py new file mode 100644 index 00000000..7cc63694 --- /dev/null +++ b/osmchadjango/changeset/tests/test_comment_views.py @@ -0,0 +1,143 @@ +from django.core.urlresolvers import reverse +from django.test import override_settings + +from social_django.models import UserSocialAuth +from rest_framework.test import APITestCase +import oauth2 as oauth +import mock + +from ...users.models import User +from ..models import Changeset +from .modelfactories import ( + ChangesetFactory, GoodChangesetFactory, HarmfulChangesetFactory + ) + + +class TestCommentChangesetAPIView(APITestCase): + def setUp(self): + self.user = User.objects.create_user( + username='test_2', + password='password', + email='a@a.com' + ) + UserSocialAuth.objects.create( + user=self.user, + provider='openstreetmap', + uid='123123', + extra_data={ + 'id': '123123', + 'access_token': { + 'oauth_token': 'aaaa', + 'oauth_token_secret': 'bbbb' + } + } + ) + self.changeset = ChangesetFactory(id=31982802) + self.harmful_changeset = HarmfulChangesetFactory(id=31982803) + self.good_changeset = GoodChangesetFactory(id=31982804) + + def test_comment_changeset_unauthenticated(self): + self.client.login(username=self.user.username, password='password') + comment = {'comment': 'Hello! I found an error in your edit'} + response = self.client.post( + reverse('changeset:comment', args=[self.harmful_changeset.id]), + data=comment + ) + + self.assertEqual(response.status_code, 403) + + @override_settings(ENABLE_POST_CHANGESET_COMMENTS=True) + @mock.patch.object(oauth.Client, 'request') + def test_comment_harmful_changeset(self, mock_oauth_client): + mock_oauth_client.return_value = [{'status': '200'}] + self.client.login(username=self.user.username, password='password') + comment = {'comment': 'Hello! I found an error in your edit'} + message = """Hello! I found an error in your edit + --- + #REVIEWED_BAD #OSMCHA + Published using OSMCha: https://osmcha.mapbox.com/changesets/31982803 + """ + response = self.client.post( + reverse('changeset:comment', args=[self.harmful_changeset.id]), + data=comment) + + self.assertEqual(response.status_code, 201) + mock_oauth_client.assert_called_with( + 'https://api.openstreetmap.org/api/0.6/changeset/{}/comment/'.format( + self.harmful_changeset.id + ), + method='POST', + body='text={}'.format(message) + ) + + @override_settings(ENABLE_POST_CHANGESET_COMMENTS=True) + @mock.patch.object(oauth.Client, 'request') + def test_comment_good_changeset(self, mock_oauth_client): + mock_oauth_client.return_value = [{'status': '200'}] + self.client.login(username=self.user.username, password='password') + comment = {'comment': 'Hello! Awesome edit'} + message = """Hello! Awesome edit + --- + #REVIEWED_GOOD #OSMCHA + Published using OSMCha: https://osmcha.mapbox.com/changesets/31982804 + """ + response = self.client.post( + reverse('changeset:comment', args=[self.good_changeset.id]), + data=comment + ) + + self.assertEqual(response.status_code, 201) + mock_oauth_client.assert_called_with( + 'https://api.openstreetmap.org/api/0.6/changeset/{}/comment/'.format( + self.good_changeset.id + ), + method='POST', + body='text={}'.format(message) + ) + + @override_settings(ENABLE_POST_CHANGESET_COMMENTS=True) + @mock.patch.object(oauth.Client, 'request') + def test_comment_unreviewed_changeset(self, mock_oauth_client): + """Unreviewed changeset should not receive the #OSMCHA_(GOOD or BAD) + hashtag. + """ + mock_oauth_client.return_value = [{'status': '200'}] + self.client.login(username=self.user.username, password='password') + comment = {'comment': 'Hello! Do you know this area?'} + message = """Hello! Do you know this area? + ---\n \n Published using OSMCha: https://osmcha.mapbox.com/changesets/31982802 + """ + response = self.client.post( + reverse('changeset:comment', args=[self.changeset.id]), + data=comment + ) + + self.assertEqual(response.status_code, 201) + mock_oauth_client.assert_called_with( + 'https://api.openstreetmap.org/api/0.6/changeset/{}/comment/'.format( + self.changeset.id + ), + method='POST', + body='text={}'.format(message) + ) + + def test_comment_good_changeset_wrong_data(self): + self.client.login(username=self.user.username, password='password') + comment = {'message': 'Hello! Awesome edit'} + response = self.client.post( + reverse('changeset:comment', args=[self.good_changeset.id]), + data=comment + ) + + self.assertEqual(response.status_code, 400) + + def test_comment_changeset_doesnt_exist(self): + """Request should fail if the changeset id is not on our database.""" + self.client.login(username=self.user.username, password='password') + comment = {'message': 'Hello! Awesome edit'} + response = self.client.post( + reverse('changeset:comment', args=[2323]), + data=comment + ) + + self.assertEqual(response.status_code, 404) diff --git a/osmchadjango/changeset/tests/test_tasks.py b/osmchadjango/changeset/tests/test_tasks.py index 9c95c36f..709254d9 100644 --- a/osmchadjango/changeset/tests/test_tasks.py +++ b/osmchadjango/changeset/tests/test_tasks.py @@ -108,39 +108,3 @@ def test_post_comment(self, mock_oauth_client): method='POST', body='text=Reviewed in OSMCha and set as GOOD!' ) - - @patch.object(oauth.Client, 'request') - def test_post_good_changeset_review(self, mock_oauth_client): - mock_oauth_client.return_value = [{'status': '200'}] - changeset_comment = ChangesetCommentAPI(self.user, 123456) - changeset_comment.post_good_changeset_review() - - mock_oauth_client.assert_called_with( - 'https://api.openstreetmap.org/api/0.6/changeset/123456/comment/', - method='POST', - body='text={}'.format(changeset_comment.good_changeset_message) - ) - - @patch.object(oauth.Client, 'request') - def test_post_bad_changeset_review(self, mock_oauth_client): - mock_oauth_client.return_value = [{'status': '200'}] - changeset_comment = ChangesetCommentAPI(self.user, 123456) - changeset_comment.post_bad_changeset_review() - - mock_oauth_client.assert_called_with( - 'https://api.openstreetmap.org/api/0.6/changeset/123456/comment/', - method='POST', - body='text={}'.format(changeset_comment.bad_changeset_message) - ) - - @patch.object(oauth.Client, 'request') - def test_post_undo_changeset_review(self, mock_oauth_client): - mock_oauth_client.return_value = [{'status': '200'}] - changeset_comment = ChangesetCommentAPI(self.user, 123456) - changeset_comment.post_undo_changeset_review() - - mock_oauth_client.assert_called_with( - 'https://api.openstreetmap.org/api/0.6/changeset/123456/comment/', - method='POST', - body='text={}'.format(changeset_comment.unchecked_changeset_message) - ) diff --git a/osmchadjango/changeset/urls.py b/osmchadjango/changeset/urls.py index 18a23d7f..35cec477 100644 --- a/osmchadjango/changeset/urls.py +++ b/osmchadjango/changeset/urls.py @@ -68,6 +68,13 @@ ), name='tags' ), + url( + regex=r'^changesets/(?P\w+)/comment/$', + view=views.ChangesetCommentAPIView.as_view( + {'post': 'post_comment'} + ), + name='comment' + ), url( regex=r'^whitelist-user/$', view=views.UserWhitelistListCreateAPIView.as_view(), diff --git a/osmchadjango/changeset/views.py b/osmchadjango/changeset/views.py index e61e53b8..e43f5fdf 100644 --- a/osmchadjango/changeset/views.py +++ b/osmchadjango/changeset/views.py @@ -30,6 +30,7 @@ ChangesetTagsSerializer, SuspicionReasonsChangesetSerializer, SuspicionReasonsFeatureSerializer, SuspicionReasonsSerializer, TagSerializer, UserStatsSerializer, UserWhitelistSerializer, + ChangesetCommentSerializer ) from .tasks import ChangesetCommentAPI from .throttling import NonStaffUserThrottle @@ -291,12 +292,6 @@ def update_changeset(self, changeset, request, harmful): changeset.save( update_fields=['checked', 'harmful', 'check_user', 'check_date'] ) - if settings.ENABLE_POST_CHANGESET_COMMENTS: - changeset_comment = ChangesetCommentAPI(request.user, changeset.id) - if harmful: - changeset_comment.post_bad_changeset_review() - else: - changeset_comment.post_good_changeset_review() return Response( {'detail': 'Changeset marked as {}.'.format('harmful' if harmful else 'good')}, status=status.HTTP_200_OK @@ -384,9 +379,6 @@ def uncheck_changeset(request, pk): instance.save( update_fields=['checked', 'harmful', 'check_user', 'check_date'] ) - if settings.ENABLE_POST_CHANGESET_COMMENTS: - changeset_comment = ChangesetCommentAPI(request.user, instance.id) - changeset_comment.post_undo_changeset_review() return Response( {'detail': 'Changeset marked as unchecked.'}, status=status.HTTP_200_OK @@ -530,3 +522,56 @@ class UserStatsAPIView(ListAPIView): def get_queryset(self): return Changeset.objects.filter(uid=self.kwargs['uid']) + + +class ChangesetCommentAPIView(ModelViewSet): + """Post a comment to a changeset in the OpenStreetMap website.""" + queryset = Changeset.objects.all() + permission_classes = (IsAuthenticated,) + serializer_class = ChangesetCommentSerializer + + @detail_route(methods=['post']) + def post_comment(self, request, pk): + self.changeset = self.get_object() + serializer = self.get_serializer(data=request.data) + if serializer.is_valid(): + if settings.ENABLE_POST_CHANGESET_COMMENTS: + changeset_comment = ChangesetCommentAPI( + request.user, + self.changeset.id + ) + comment_response = changeset_comment.post_comment( + self.add_footer(serializer.data['comment']) + ) + if comment_response.get('success'): + return Response( + {'detail': 'Changeset comment posted succesfully.'}, + status=status.HTTP_201_CREATED + ) + else: + return Response( + {'detail': 'Changeset comment failed.'}, + status=status.HTTP_400_BAD_REQUEST + ) + else: + return Response( + {'detail': 'Changeset comment is not enabled.'}, + status=status.HTTP_403_FORBIDDEN + ) + else: + return Response( + serializer.errors, + status=status.HTTP_400_BAD_REQUEST + ) + + def add_footer(self, message): + status = "" + if self.changeset.checked and self.changeset.harmful is not None: + status = "#REVIEWED_{} #OSMCHA".format( + 'BAD' if self.changeset.harmful else 'GOOD' + ) + return """{} + --- + {} + Published using OSMCha: https://osmcha.mapbox.com/changesets/{} + """.format(message, status, self.changeset.id) diff --git a/osmchadjango/supervise/views.py b/osmchadjango/supervise/views.py index a65195e4..873c2df5 100644 --- a/osmchadjango/supervise/views.py +++ b/osmchadjango/supervise/views.py @@ -217,7 +217,7 @@ def list(self, request, *args, **kwargs): class AOIStatsAPIView(ListAPIView): """Return the statistics of the changesets that matches an Area of Interest. - Return the data in the same format as the Changeset Stats view. + The data will be in the same format as the Changeset Stats view. """ queryset = AreaOfInterest.objects.all() serializer_class = ChangesetStatsSerializer diff --git a/osmchadjango/users/migrations/0007_auto_20170421_1939.py b/osmchadjango/users/migrations/0007_auto_20170421_1939.py new file mode 100644 index 00000000..fe34f4ad --- /dev/null +++ b/osmchadjango/users/migrations/0007_auto_20170421_1939.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.7 on 2017-04-21 19:39 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('users', '0006_auto_20170330_1401'), + ] + + operations = [ + migrations.AlterField( + model_name='user', + name='name', + field=models.CharField(blank=True, db_index=True, max_length=255, verbose_name='Name of User'), + ), + ] diff --git a/osmchadjango/users/migrations/0008_auto_20180118_1229.py b/osmchadjango/users/migrations/0008_auto_20180118_1229.py new file mode 100644 index 00000000..0c2fc6e8 --- /dev/null +++ b/osmchadjango/users/migrations/0008_auto_20180118_1229.py @@ -0,0 +1,30 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.8 on 2018-01-18 12:29 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('users', '0007_auto_20170421_1939'), + ] + + operations = [ + migrations.AddField( + model_name='user', + name='comment_feature', + field=models.BooleanField(default=False), + ), + migrations.AddField( + model_name='user', + name='message_bad', + field=models.TextField(blank=True, verbose_name='Default message to bad changesets'), + ), + migrations.AddField( + model_name='user', + name='message_good', + field=models.TextField(blank=True, verbose_name='Default message to good changesets'), + ), + ] diff --git a/osmchadjango/users/models.py b/osmchadjango/users/models.py index 3cf74034..4fdc7d71 100644 --- a/osmchadjango/users/models.py +++ b/osmchadjango/users/models.py @@ -8,9 +8,22 @@ class User(AbstractUser): - # First Name and Last Name do not cover name patterns - # around the globe. - name = models.CharField("Name of User", blank=True, max_length=255, db_index=True) + # First Name and Last Name do not cover name patterns around the globe. + name = models.CharField( + "Name of User", blank=True, max_length=255, db_index=True + ) + message_good = models.TextField( + "Default message to good changesets", + blank=True + ) + message_bad = models.TextField( + "Default message to bad changesets", + blank=True + ) + comment_feature = models.BooleanField( + "Enable suggestion to post comment", + default=False + ) def __str__(self): return self.username diff --git a/osmchadjango/users/serializers.py b/osmchadjango/users/serializers.py index 1311fe46..da8e20d1 100644 --- a/osmchadjango/users/serializers.py +++ b/osmchadjango/users/serializers.py @@ -40,7 +40,8 @@ class Meta: model = get_user_model() fields = ( 'id', 'uid', 'username', 'is_staff', 'is_active', 'email', - 'avatar', 'whitelists' + 'avatar', 'whitelists', 'message_good', 'message_bad', + 'comment_feature' ) read_only_fields = ('username', 'is_staff', 'is_active', 'id', 'uid') diff --git a/osmchadjango/users/tests/test_models.py b/osmchadjango/users/tests/test_models.py new file mode 100644 index 00000000..79732a19 --- /dev/null +++ b/osmchadjango/users/tests/test_models.py @@ -0,0 +1,38 @@ +from django.core.exceptions import ValidationError +from django.db.utils import IntegrityError +from django.test import TestCase + +from ..models import User + + +class TestUserModel(TestCase): + def setUp(self): + self.user = User.objects.create_user( + username='test', + password='password', + email='a@a.com' + ) + self.user_complete = User.objects.create_user( + username='test_complete_user', + password='password', + email='a@a.com', + message_good='Hello! Thank you for this amazing changeset!', + message_bad='Hello! I found some errors in your changeset...', + comment_feature=True + ) + + def test_basic_user_model(self): + self.assertIsInstance(self.user, User) + self.assertEqual(User.objects.count(), 2) + self.assertEqual(self.user.username, 'test') + self.assertEqual(self.user.email, 'a@a.com') + self.assertFalse(self.user.comment_feature) + self.assertEqual( + self.user_complete.message_good, + 'Hello! Thank you for this amazing changeset!' + ) + self.assertEqual( + self.user_complete.message_bad, + 'Hello! I found some errors in your changeset...' + ) + self.assertTrue(self.user_complete.comment_feature) diff --git a/osmchadjango/users/tests/test_views.py b/osmchadjango/users/tests/test_views.py index ab262ec2..365699aa 100644 --- a/osmchadjango/users/tests/test_views.py +++ b/osmchadjango/users/tests/test_views.py @@ -37,6 +37,7 @@ def test_get_view(self): self.assertEqual(response.data.get('is_active'), True) self.assertEqual(response.data.get('avatar'), 'http://theurl.org/pic.jpg') self.assertEqual(response.data.get('whitelists'), []) + self.assertFalse(response.data.get('comment_feature')) self.assertFalse('password' in response.data.keys()) def test_update_view(self): @@ -44,14 +45,26 @@ def test_update_view(self): data = { "username": "test_user", "email": "admin@a.com", - "is_staff": "true" + "is_staff": "true", + "message_good": "Hello! Awesome edit!", + "message_bad": "Hello! I found an error in your edit...", + "comment_feature": True } response = self.client.put(self.url, data) self.assertEqual(response.status_code, 200) response = self.client.get(self.url) self.assertEqual(response.data.get('email'), 'admin@a.com') self.assertEqual(response.data.get('username'), 'test') - self.assertEqual(response.data.get('is_staff'), False) + self.assertFalse(response.data.get('is_staff')) + self.assertEqual( + response.data.get('message_good'), + 'Hello! Awesome edit!' + ) + self.assertEqual( + response.data.get('message_bad'), + 'Hello! I found an error in your edit...' + ) + self.assertTrue(response.data.get('comment_feature')) def test_username_serialization(self): self.user.name = 'test user' diff --git a/osmchadjango/users/views.py b/osmchadjango/users/views.py index 3d4db80f..357cf338 100644 --- a/osmchadjango/users/views.py +++ b/osmchadjango/users/views.py @@ -22,11 +22,9 @@ class CurrentUserDetailAPIView(RetrieveUpdateAPIView): get: Get details of the current logged user. patch: - Update details of the current logged user. It's allowed only to update the - email address. + Update details of the current logged user. put: - Update details of the current logged user. It's allowed only to update the - email address. + Update details of the current logged user. """ permission_classes = (IsAuthenticated,) serializer_class = UserSerializer