Browse Source

team index => organization teams

David Cramer 10 years ago
parent
commit
46d8731c89

+ 32 - 6
src/sentry/api/endpoints/team_index.py → src/sentry/api/endpoints/organization_teams.py

@@ -5,7 +5,7 @@ from rest_framework.response import Response
 
 from sentry.api.base import Endpoint
 from sentry.api.serializers import serialize
-from sentry.models import Team, User
+from sentry.models import Organization, Team, User
 from sentry.permissions import can_create_teams
 
 
@@ -33,15 +33,40 @@ class TeamAdminSerializer(TeamSerializer):
     owner = UserField(required=False)
 
 
-class TeamIndexEndpoint(Endpoint):
-    def get(self, request):
+class OrganizationTeamsEndpoint(Endpoint):
+    def get_organization(self, request, organization_id):
+        organization_id = str(organization_id)
+        try:
+            return (
+                o for o in Organization.objects.get_for_user(
+                    user=request.user,
+                )
+                if str(o.id) == organization_id
+            ).next()
+        except StopIteration:
+            return
+
+    def get(self, request, organization_id):
+        organization = self.get_organization(request, organization_id)
+        if organization is None:
+            return Response(status=403)
+
         if request.auth:
             teams = [request.auth.project.team]
+            if teams[0].organization != organization:
+                return Response(status=403)
         else:
-            teams = Team.objects.get_for_user(request.user).values()
+            teams = Team.objects.get_for_user(
+                user=request.user,
+                organization=organization,
+            ).values()
         return Response(serialize(teams, request.user))
 
-    def post(self, request):
+    def post(self, request, organization_id):
+        organization = self.get_organization(request, organization_id)
+        if organization is None:
+            return Response(status=403)
+
         if not can_create_teams(request.user):
             return Response(status=403)
 
@@ -55,7 +80,8 @@ class TeamIndexEndpoint(Endpoint):
             team = Team.objects.create(
                 name=result['name'],
                 slug=result.get('slug'),
-                owner=result.get('owner') or request.user,
+                owner=result.get('owner') or organization.owner,
+                organization=organization,
             )
             return Response(serialize(team, request.user), status=201)
         return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

+ 6 - 4
src/sentry/api/urls.py

@@ -16,13 +16,13 @@ from .endpoints.group_events_latest import GroupEventsLatestEndpoint
 from .endpoints.group_notes import GroupNotesEndpoint
 from .endpoints.group_stats import GroupStatsEndpoint
 from .endpoints.group_tags import GroupTagsEndpoint
+from .endpoints.organization_teams import OrganizationTeamsEndpoint
 from .endpoints.project_details import ProjectDetailsEndpoint
 from .endpoints.project_index import ProjectIndexEndpoint
 from .endpoints.project_group_index import ProjectGroupIndexEndpoint
 from .endpoints.project_releases import ProjectReleasesEndpoint
 from .endpoints.project_stats import ProjectStatsEndpoint
 from .endpoints.team_details import TeamDetailsEndpoint
-from .endpoints.team_index import TeamIndexEndpoint
 from .endpoints.team_access_group_index import TeamAccessGroupIndexEndpoint
 from .endpoints.team_project_index import TeamProjectIndexEndpoint
 from .endpoints.team_member_index import TeamMemberIndexEndpoint
@@ -43,10 +43,12 @@ urlpatterns = patterns(
         UserDetailsEndpoint.as_view(),
         name='sentry-api-0-user-details'),
 
+    # Organizations
+    url(r'^organizations/(?P<organization_id>\d+)/teams/$',
+        OrganizationTeamsEndpoint.as_view(),
+        name='sentry-api-0-organization-teams'),
+
     # Teams
-    url(r'^teams/$',
-        TeamIndexEndpoint.as_view(),
-        name='sentry-api-0-team-index'),
     url(r'^teams/(?P<team_id>\d+)/$',
         TeamDetailsEndpoint.as_view(),
         name='sentry-api-0-team-details'),

+ 10 - 9
tests/sentry/api/endpoints/test_team_index.py → tests/sentry/api/endpoints/test_organization_teams.py

@@ -7,10 +7,10 @@ from sentry.models import Team
 from sentry.testutils import APITestCase
 
 
-class TeamIndexTest(APITestCase):
+class OrganizationTeamsListTest(APITestCase):
     @fixture
     def path(self):
-        return reverse('sentry-api-0-team-index')
+        return reverse('sentry-api-0-organization-teams', args=[self.organization.id])
 
     def test_simple(self):
         team = self.create_team()  # force creation
@@ -21,24 +21,24 @@ class TeamIndexTest(APITestCase):
         assert response.data[0]['id'] == str(team.id)
 
 
-class TeamCreateTest(APITestCase):
+class OrganizationTeamsCreateTest(APITestCase):
     @fixture
     def path(self):
-        return reverse('sentry-api-0-team-index')
+        return reverse('sentry-api-0-organization-teams', args=[self.organization.id])
 
-    @patch('sentry.api.endpoints.team_index.can_create_teams', Mock(return_value=False))
+    @patch('sentry.api.endpoints.organization_teams.can_create_teams', Mock(return_value=False))
     def test_missing_permission(self):
         self.login_as(user=self.user)
         resp = self.client.post(self.path)
         assert resp.status_code == 403
 
-    @patch('sentry.api.endpoints.team_index.can_create_teams', Mock(return_value=True))
+    @patch('sentry.api.endpoints.organization_teams.can_create_teams', Mock(return_value=True))
     def test_missing_params(self):
         self.login_as(user=self.user)
         resp = self.client.post(self.path)
         assert resp.status_code == 400
 
-    @patch('sentry.api.endpoints.team_index.can_create_teams', Mock(return_value=True))
+    @patch('sentry.api.endpoints.organization_teams.can_create_teams', Mock(return_value=True))
     def test_valid_params(self):
         self.login_as(user=self.user)
 
@@ -51,6 +51,7 @@ class TeamCreateTest(APITestCase):
         assert team.name == 'hello world'
         assert team.slug == 'foobar'
         assert team.owner == self.user
+        assert team.organization == self.organization
 
         member_set = list(team.member_set.all())
 
@@ -59,7 +60,7 @@ class TeamCreateTest(APITestCase):
         assert member.user == team.owner
         assert member.type == MEMBER_OWNER
 
-    @patch('sentry.api.endpoints.team_index.can_create_teams', Mock(return_value=True))
+    @patch('sentry.api.endpoints.organization_teams.can_create_teams', Mock(return_value=True))
     def test_without_slug(self):
         self.login_as(user=self.user)
 
@@ -70,7 +71,7 @@ class TeamCreateTest(APITestCase):
         team = Team.objects.get(id=resp.data['id'])
         assert team.slug == 'hello-world'
 
-    @patch('sentry.api.endpoints.team_index.can_create_teams', Mock(return_value=True))
+    @patch('sentry.api.endpoints.organization_teams.can_create_teams', Mock(return_value=True))
     def test_superuser_can_set_owner(self):
         self.login_as(user=self.user)