Skip to content

Commit

Permalink
Better adding an admin to project
Browse files Browse the repository at this point in the history
  • Loading branch information
Julius O committed Sep 28, 2015
1 parent 652abbe commit c949357
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 25 deletions.
40 changes: 28 additions & 12 deletions geokey/projects/tests/test_views.py
Expand Up @@ -563,10 +563,26 @@ def setUp(self):
add_contributors=[self.contributor]
)

def test_add_not_existing_user(self):
def test_add_when_project_does_not_exist(self):
project_id = 156564541545445421
request = self.factory.post(
'/ajax/projects/%s/admins/' % (self.project.id) + '/',
{'userId': 468476351545643131}
'/ajax/projects/%s/admins/' % project_id,
{'userId': self.user_to_add.id}
)
force_authenticate(request, user=self.admin)
view = ProjectAdmins.as_view()
response = view(
request,
project_id=project_id
).render()

self.assertEqual(response.status_code, 404)

def test_add_when_user_does_not_exist(self):
user_id = 468476351545643131
request = self.factory.post(
'/ajax/projects/%s/admins/' % self.project.id,
{'userId': user_id}
)
force_authenticate(request, user=self.admin)
view = ProjectAdmins.as_view()
Expand All @@ -575,12 +591,12 @@ def test_add_not_existing_user(self):
project_id=self.project.id
).render()

self.assertEqual(response.status_code, 400)
self.assertEqual(response.status_code, 404)

def test_add_when_user_alread_admin(self):
def test_add_when_user_already_an_admin(self):
Admins.objects.create(project=self.project, user=self.user_to_add)
request = self.factory.post(
'/ajax/projects/%s/admins/' % (self.project.id) + '/',
'/ajax/projects/%s/admins/' % self.project.id,
{'userId': self.user_to_add.id}
)
force_authenticate(request, user=self.admin)
Expand All @@ -592,9 +608,9 @@ def test_add_when_user_alread_admin(self):

self.assertEqual(response.status_code, 400)

def test_add_admin_with_admin(self):
def test_add_with_admin(self):
request = self.factory.post(
'/ajax/projects/%s/admins/' % (self.project.id) + '/',
'/ajax/projects/%s/admins/' % self.project.id,
{'userId': self.user_to_add.id}
)
force_authenticate(request, user=self.admin)
Expand All @@ -610,9 +626,9 @@ def test_add_admin_with_admin(self):
Project.objects.get(pk=self.project.id).admins.all()
)

def test_add_admin_with_contributor(self):
def test_add_with_contributor(self):
request = self.factory.post(
'/ajax/projects/%s/admins/' % (self.project.id) + '/',
'/ajax/projects/%s/admins/' % self.project.id,
{'userId': self.user_to_add.id}
)
force_authenticate(request, user=self.contributor)
Expand All @@ -628,9 +644,9 @@ def test_add_admin_with_contributor(self):
Project.objects.get(pk=self.project.id).admins.all()
)

def test_add_admin_with_non_member(self):
def test_add_with_non_member(self):
request = self.factory.post(
'/ajax/projects/%s/admins/' % (self.project.id) + '/',
'/ajax/projects/%s/admins/' % self.project.id,
{'userId': self.user_to_add.id}
)
force_authenticate(request, user=self.non_member)
Expand Down
18 changes: 5 additions & 13 deletions geokey/projects/views.py
Expand Up @@ -369,29 +369,21 @@ def post(self, request, project_id):
message.
"""
project = Project.objects.as_admin(request.user, project_id)
user_id = request.data.get('userId')

try:
user = User.objects.get(pk=user_id)
except User.DoesNotExist:
return Response(
'The user you are trying to add to the user group does ' +
'not exist',
status=status.HTTP_400_BAD_REQUEST
)
user = User.objects.get(pk=request.data.get('userId'))

try:
Admins.objects.create(project=project, user=user)
except IntegrityError:
return Response(
'The user you are trying to add to the user group already ' +
'exists',
'The user is already an administrator of this project.',
status=status.HTTP_400_BAD_REQUEST
)

serializer = UserSerializer(project.admins.all(), many=True)
return Response(
{'users': serializer.data}, status=status.HTTP_201_CREATED)
{'users': serializer.data},
status=status.HTTP_201_CREATED
)


class ProjectAdminsUser(APIView):
Expand Down

0 comments on commit c949357

Please sign in to comment.