Browse Source

Importer api

David Burke 2 years ago
parent
commit
3f85e2be7e

+ 1 - 1
glitchtip/importer/importer.py

@@ -57,7 +57,7 @@ class GlitchTipImporter:
         if organization_id is None:
             self.import_organization()
         else:
-            organization_id = organization_id
+            self.organization_id = organization_id
         self.import_organization_users()
         self.import_projects()
         self.import_teams()

+ 4 - 2
glitchtip/importer/serializers.py

@@ -1,5 +1,5 @@
 from rest_framework import serializers
-from organizations_ext.models import Organization
+from organizations_ext.models import Organization, OrganizationUserRole
 
 
 class ImportSerializer(serializers.Serializer):
@@ -13,5 +13,7 @@ class ImportSerializer(serializers.Serializer):
         if user := context["request"].user:
             self.fields[
                 "organizationSlug"
-            ].queryset = user.organizations_ext_organization.all()
+            ].queryset = user.organizations_ext_organization.filter(
+                organization_users__role__gte=OrganizationUserRole.ADMIN
+            )
         return super().__init__(*args, **kwargs)

+ 42 - 17
glitchtip/importer/tests.py

@@ -1,6 +1,7 @@
 import requests_mock
 from django.core.management import call_command
 from django.urls import reverse
+from model_bakery import baker
 
 from glitchtip.test_utils.test_case import GlitchTipTestCase
 from projects.models import Project
@@ -8,6 +9,14 @@ from teams.models import Team
 
 from .importer import GlitchTipImporter
 
+test_project = {"id": "1", "slug": "project", "name": "project"}
+test_key = {
+    "id": "a" * 32,
+    "public": "a" * 32,
+    "projectID": 1,
+    "label": "Default",
+}
+
 
 class ImporterTestCase(GlitchTipTestCase):
     def setUp(self):
@@ -18,39 +27,35 @@ class ImporterTestCase(GlitchTipTestCase):
             self.url.lstrip("htps:/"), self.auth_token, self.org_name
         )
 
-    @requests_mock.Mocker()
-    def test_import_command(self, m):
-        project = {"id": "1", "slug": "project", "name": "project"}
-        key = {
-            "id": "a" * 32,
-            "public": "a" * 32,
-            "projectID": 1,
-            "label": "Default",
-        }
+    def set_mocks(self, m):
         m.get(self.url + self.importer.api_root_url, json={"user": {"username": "foo"}})
         m.get(self.url + self.importer.organization_url, json={"id": 1})
         m.get(self.url + self.importer.organization_users_url, json=[])
-        m.get(self.url + self.importer.projects_url, json=[project])
-        m.get(self.url + "/api/0/projects/org/project/keys/", json=[key])
+        m.get(self.url + self.importer.projects_url, json=[test_project])
+        m.get(self.url + "/api/0/projects/org/project/keys/", json=[test_key])
         m.get(
             self.url + self.importer.teams_url,
             json=[
                 {
                     "id": "1",
                     "slug": "team",
-                    "projects": [project],
+                    "projects": [test_project],
                 }
             ],
         )
         m.get(self.url + "/api/0/teams/org/team/members/", json=[])
 
+    @requests_mock.Mocker()
+    def test_import_command(self, m):
+        self.set_mocks(m)
+
         call_command("import", self.url, self.auth_token, self.org_name)
         self.assertTrue(Team.objects.filter(slug="team").exists())
         self.assertTrue(
             Project.objects.filter(
-                slug=project["slug"],
+                slug=test_project["slug"],
                 team__slug="team",
-                projectkey__public_key=key["public"],
+                projectkey__public_key=test_key["public"],
             ).exists()
         )
 
@@ -59,14 +64,34 @@ class ImporterTestCase(GlitchTipTestCase):
         self.create_user_and_project()
         self.organization.slug = self.org_name
         self.organization.save()
+        self.set_mocks(m)
         url = reverse("import")
         data = {
             "url": self.url,
             "authToken": self.auth_token,
             "organizationSlug": self.org_name,
         }
-        res = self.client.options(url)
-        print(res.data)
         res = self.client.post(url, data)
-        print(res.data)
         self.assertEqual(res.status_code, 200)
+        self.assertTrue(Team.objects.filter(slug="team").exists())
+
+    @requests_mock.Mocker()
+    def test_invalid_org(self, m):
+        self.create_user_and_project()
+        url = reverse("import")
+        data = {
+            "url": self.url,
+            "authToken": self.auth_token,
+            "organizationSlug": "foo",
+        }
+        res = self.client.post(url, data)
+        self.assertEqual(res.status_code, 400)
+        other_user = baker.make("users.User")
+        other_org = baker.make("Organization", name="foo")
+        other_org.add_user(other_user)
+        res = self.client.post(url, data)
+        self.assertEqual(res.status_code, 400)
+        org_user = other_org.add_user(self.user)
+        m.get(self.url + self.importer.api_root_url, json={"user": {"username": "foo"}})
+        res = self.client.post(url, data)
+        self.assertEqual(res.status_code, 400)

+ 4 - 2
glitchtip/importer/views.py

@@ -1,11 +1,13 @@
 from rest_framework import views
 from rest_framework.response import Response
 
-from .serializers import ImportSerializer
 from .importer import GlitchTipImporter
+from .serializers import ImportSerializer
 
 
 class ImportAPIView(views.APIView):
+    """Import members, projects, and teams for an organization of which you are an Admin of"""
+
     serializer_class = ImportSerializer
 
     def post(self, request):
@@ -15,7 +17,7 @@ class ImportAPIView(views.APIView):
         serializer.is_valid(raise_exception=True)
         data = serializer.validated_data
         importer = GlitchTipImporter(
-            data["url"], data["authToken"], data["organizationSlug"]
+            data["url"], data["authToken"], data["organizationSlug"].slug
         )
         importer.check_auth()
         importer.run(organization_id=data["organizationSlug"].pk)