0
0
mirror of https://github.com/PostHog/posthog.git synced 2024-11-24 18:07:17 +01:00
posthog/ee/api/test/test_organization.py

82 lines
4.1 KiB
Python
Raw Normal View History

from typing import cast
Organization<>Team<>User structure (closes #1540) (#1562) * Backend changes to implement #1461 * Added the missing migration files * Fixes Typecheck errors * Refactor request.user.team_set.get() to use request.user.team * Updated user patch method to change current_team on team id instead of name * Merged migration file * Changes team property to return first item in queryset * Fixes failing tests * Changed User api to return the ids of the teams they are currently part of * Frontend for changing teams * Update and simplify migrations * Improve team and user buttons * Make team changing backend more logical * Improve current_team mechanics * Update test_team.py * Fix Team.objects.create_with_data * Update migration * Update tests * Make setup_review more convenient * Add Organization and OrganizationMembership * Replace is_admin with level * Extend API * Update team.py * Improve modeling * Improve handling of new mechanics * Add proper migration * Remove _ensure_organization_and_team * Update 0084_org_team_user.py * Improve user, org and team creation * Make MembershipLevel more flexible for the future * Add member deletion * Fix naive datetime warnings * Update setup_review.py * Update API route * Make PersonalAPIKey changes * Update models and migrations, fix typing * Fix typing * Use MAC-less UUID v1 instead of v4 for better performance * Add abstract UUIDModel * Update utils.py * Update utils.py * Fix multi/unicast bit * Update APIs, frontend and tests * Update pull_request_template.md * Fix comment * Fix migration error * Compress migrations * Updates with minimal renaming * More updates * Make further updates * Update test_team_user.py * Fix issues * Add migration * Satisfy mypy * Remove Signup redirect on logged in * Use uuid1_macless in Person * Fix typing * Update tests * Update /api/team/signup to /api/organization/signup * Fix mypy issues and update tests The remaining failures are actually missing functionality (TDD applied), so filling these in. * Update 0086_org_live * Make small improvements * Implement permissions * Remove now unneccesary membership check * Update setup_dev.py * Make small frontend improvements * Add drf-nested-routers as requirement * Remove unused import * Implemented nested routes * Remove cruft * Add relevant org/proj/user name to headings * Fix imports * Update migration * Replace unreliable drf-nested-routers with drf-extensions * Improve unset team handling * Make org and team creation proper * Update migration * Fix migration * Update TopContent * Update command palette for new sidebar structure * Remove deprecated demo data deletion * Assume that each org has a project and fix typing * Require paid plan for multiple orgs and projects * Make HogFlix demo a separate team * Update migration * Slightly improve style * Adjust page layout bottom padding * Make user dropdown nicer * Fix base app tests * Satisfy mypy * Fix test_leave_organization * Improve wording * Possibly fix import * Remove misplaced None check * Enhance org and teams APIs and add tests * Fix /api/projects for particular Team * Improve invites and demo data * Address feedback * Put everything related to billing on Organization * Fix minor issues * Simplify invitation creation * Update team model * Make orgs and projects premium only on self-hosted * Improve testing * Update migration * Remove extra License import * Fix minor issues * Fix Django tests * Fix Cypress * Fix yarn build * Fix TeamSignupViewset * Fix posthog-production incompatibility * Remove extraneous insight endpoint registration * Adjust tests for posthog-production * Simplify invitations and fix email validation * Address all feedback * Satisfy mypy * Update migration * Fix constraint removal in migration * Update tests * Fix test creation edge case * Run posthog-production CI tests against this branch and teams-live * Ensure that js_posthog_api_key is always passed * Fix preflight check pre-login * Update cypress tests * Update instanceStatus.js * Bring ee tets up to par * Bring actions-ux-201012 back * Cypress retry in cypress.json * Revert "Run posthog-production CI tests against this branch and teams-live" This reverts commit d79cb844d823d767b060122af32bd74163593d7a. Co-authored-by: anna <ms.annaphilips@gmail.com> Co-authored-by: Anna Philips <aphilips@matmacorp.com>
2020-10-22 10:58:32 +02:00
from unittest.mock import patch
from django.test.utils import tag
from rest_framework import status
from ee.api.test.base import APILicensedTest
Organization<>Team<>User structure (closes #1540) (#1562) * Backend changes to implement #1461 * Added the missing migration files * Fixes Typecheck errors * Refactor request.user.team_set.get() to use request.user.team * Updated user patch method to change current_team on team id instead of name * Merged migration file * Changes team property to return first item in queryset * Fixes failing tests * Changed User api to return the ids of the teams they are currently part of * Frontend for changing teams * Update and simplify migrations * Improve team and user buttons * Make team changing backend more logical * Improve current_team mechanics * Update test_team.py * Fix Team.objects.create_with_data * Update migration * Update tests * Make setup_review more convenient * Add Organization and OrganizationMembership * Replace is_admin with level * Extend API * Update team.py * Improve modeling * Improve handling of new mechanics * Add proper migration * Remove _ensure_organization_and_team * Update 0084_org_team_user.py * Improve user, org and team creation * Make MembershipLevel more flexible for the future * Add member deletion * Fix naive datetime warnings * Update setup_review.py * Update API route * Make PersonalAPIKey changes * Update models and migrations, fix typing * Fix typing * Use MAC-less UUID v1 instead of v4 for better performance * Add abstract UUIDModel * Update utils.py * Update utils.py * Fix multi/unicast bit * Update APIs, frontend and tests * Update pull_request_template.md * Fix comment * Fix migration error * Compress migrations * Updates with minimal renaming * More updates * Make further updates * Update test_team_user.py * Fix issues * Add migration * Satisfy mypy * Remove Signup redirect on logged in * Use uuid1_macless in Person * Fix typing * Update tests * Update /api/team/signup to /api/organization/signup * Fix mypy issues and update tests The remaining failures are actually missing functionality (TDD applied), so filling these in. * Update 0086_org_live * Make small improvements * Implement permissions * Remove now unneccesary membership check * Update setup_dev.py * Make small frontend improvements * Add drf-nested-routers as requirement * Remove unused import * Implemented nested routes * Remove cruft * Add relevant org/proj/user name to headings * Fix imports * Update migration * Replace unreliable drf-nested-routers with drf-extensions * Improve unset team handling * Make org and team creation proper * Update migration * Fix migration * Update TopContent * Update command palette for new sidebar structure * Remove deprecated demo data deletion * Assume that each org has a project and fix typing * Require paid plan for multiple orgs and projects * Make HogFlix demo a separate team * Update migration * Slightly improve style * Adjust page layout bottom padding * Make user dropdown nicer * Fix base app tests * Satisfy mypy * Fix test_leave_organization * Improve wording * Possibly fix import * Remove misplaced None check * Enhance org and teams APIs and add tests * Fix /api/projects for particular Team * Improve invites and demo data * Address feedback * Put everything related to billing on Organization * Fix minor issues * Simplify invitation creation * Update team model * Make orgs and projects premium only on self-hosted * Improve testing * Update migration * Remove extra License import * Fix minor issues * Fix Django tests * Fix Cypress * Fix yarn build * Fix TeamSignupViewset * Fix posthog-production incompatibility * Remove extraneous insight endpoint registration * Adjust tests for posthog-production * Simplify invitations and fix email validation * Address all feedback * Satisfy mypy * Update migration * Fix constraint removal in migration * Update tests * Fix test creation edge case * Run posthog-production CI tests against this branch and teams-live * Ensure that js_posthog_api_key is always passed * Fix preflight check pre-login * Update cypress tests * Update instanceStatus.js * Bring ee tets up to par * Bring actions-ux-201012 back * Cypress retry in cypress.json * Revert "Run posthog-production CI tests against this branch and teams-live" This reverts commit d79cb844d823d767b060122af32bd74163593d7a. Co-authored-by: anna <ms.annaphilips@gmail.com> Co-authored-by: Anna Philips <aphilips@matmacorp.com>
2020-10-22 10:58:32 +02:00
from posthog.models.organization import Organization, OrganizationMembership
from posthog.models.team import Team
class TestOrganizationEnterpriseAPI(APILicensedTest):
def test_create_organization(self):
response = self.client.post("/api/organizations/", {"name": "Test"})
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(Organization.objects.count(), 2)
response_data = response.json()
self.assertEqual(response_data.get("name"), "Test")
self.assertEqual(OrganizationMembership.objects.filter(organization_id=response_data.get("id")).count(), 1)
self.assertEqual(
OrganizationMembership.objects.get(organization_id=response_data.get("id"), user=self.user).level,
OrganizationMembership.Level.OWNER,
Organization<>Team<>User structure (closes #1540) (#1562) * Backend changes to implement #1461 * Added the missing migration files * Fixes Typecheck errors * Refactor request.user.team_set.get() to use request.user.team * Updated user patch method to change current_team on team id instead of name * Merged migration file * Changes team property to return first item in queryset * Fixes failing tests * Changed User api to return the ids of the teams they are currently part of * Frontend for changing teams * Update and simplify migrations * Improve team and user buttons * Make team changing backend more logical * Improve current_team mechanics * Update test_team.py * Fix Team.objects.create_with_data * Update migration * Update tests * Make setup_review more convenient * Add Organization and OrganizationMembership * Replace is_admin with level * Extend API * Update team.py * Improve modeling * Improve handling of new mechanics * Add proper migration * Remove _ensure_organization_and_team * Update 0084_org_team_user.py * Improve user, org and team creation * Make MembershipLevel more flexible for the future * Add member deletion * Fix naive datetime warnings * Update setup_review.py * Update API route * Make PersonalAPIKey changes * Update models and migrations, fix typing * Fix typing * Use MAC-less UUID v1 instead of v4 for better performance * Add abstract UUIDModel * Update utils.py * Update utils.py * Fix multi/unicast bit * Update APIs, frontend and tests * Update pull_request_template.md * Fix comment * Fix migration error * Compress migrations * Updates with minimal renaming * More updates * Make further updates * Update test_team_user.py * Fix issues * Add migration * Satisfy mypy * Remove Signup redirect on logged in * Use uuid1_macless in Person * Fix typing * Update tests * Update /api/team/signup to /api/organization/signup * Fix mypy issues and update tests The remaining failures are actually missing functionality (TDD applied), so filling these in. * Update 0086_org_live * Make small improvements * Implement permissions * Remove now unneccesary membership check * Update setup_dev.py * Make small frontend improvements * Add drf-nested-routers as requirement * Remove unused import * Implemented nested routes * Remove cruft * Add relevant org/proj/user name to headings * Fix imports * Update migration * Replace unreliable drf-nested-routers with drf-extensions * Improve unset team handling * Make org and team creation proper * Update migration * Fix migration * Update TopContent * Update command palette for new sidebar structure * Remove deprecated demo data deletion * Assume that each org has a project and fix typing * Require paid plan for multiple orgs and projects * Make HogFlix demo a separate team * Update migration * Slightly improve style * Adjust page layout bottom padding * Make user dropdown nicer * Fix base app tests * Satisfy mypy * Fix test_leave_organization * Improve wording * Possibly fix import * Remove misplaced None check * Enhance org and teams APIs and add tests * Fix /api/projects for particular Team * Improve invites and demo data * Address feedback * Put everything related to billing on Organization * Fix minor issues * Simplify invitation creation * Update team model * Make orgs and projects premium only on self-hosted * Improve testing * Update migration * Remove extra License import * Fix minor issues * Fix Django tests * Fix Cypress * Fix yarn build * Fix TeamSignupViewset * Fix posthog-production incompatibility * Remove extraneous insight endpoint registration * Adjust tests for posthog-production * Simplify invitations and fix email validation * Address all feedback * Satisfy mypy * Update migration * Fix constraint removal in migration * Update tests * Fix test creation edge case * Run posthog-production CI tests against this branch and teams-live * Ensure that js_posthog_api_key is always passed * Fix preflight check pre-login * Update cypress tests * Update instanceStatus.js * Bring ee tets up to par * Bring actions-ux-201012 back * Cypress retry in cypress.json * Revert "Run posthog-production CI tests against this branch and teams-live" This reverts commit d79cb844d823d767b060122af32bd74163593d7a. Co-authored-by: anna <ms.annaphilips@gmail.com> Co-authored-by: Anna Philips <aphilips@matmacorp.com>
2020-10-22 10:58:32 +02:00
)
def test_delete_second_managed_organization(self):
organization, _, team = Organization.objects.bootstrap(self.user, name="X")
self.assertTrue(Organization.objects.filter(id=organization.id).exists())
self.assertTrue(Team.objects.filter(id=team.id).exists())
response = self.client.delete(f"/api/organizations/{organization.id}")
self.assertEqual(response.status_code, 204)
self.assertFalse(Organization.objects.filter(id=organization.id).exists())
self.assertFalse(Team.objects.filter(id=team.id).exists())
def test_no_delete_last_organization(self):
org_id = self.organization.id
self.assertTrue(Organization.objects.filter(id=org_id).exists())
response = self.client.delete(f"/api/organizations/{org_id}")
self.assertEqual(
response.data,
{
"attr": None,
"detail": f"Cannot remove organization since that would leave member {self.CONFIG_USER_EMAIL} organization-less, which is not supported yet.",
"code": "invalid_input",
"type": "validation_error",
},
)
self.assertEqual(response.status_code, 400)
self.assertTrue(Organization.objects.filter(id=org_id).exists())
def test_no_delete_organization_not_administrating(self):
organization, organization_membership, team = Organization.objects.bootstrap(self.user)
organization_membership = cast(OrganizationMembership, organization_membership)
Organization<>Team<>User structure (closes #1540) (#1562) * Backend changes to implement #1461 * Added the missing migration files * Fixes Typecheck errors * Refactor request.user.team_set.get() to use request.user.team * Updated user patch method to change current_team on team id instead of name * Merged migration file * Changes team property to return first item in queryset * Fixes failing tests * Changed User api to return the ids of the teams they are currently part of * Frontend for changing teams * Update and simplify migrations * Improve team and user buttons * Make team changing backend more logical * Improve current_team mechanics * Update test_team.py * Fix Team.objects.create_with_data * Update migration * Update tests * Make setup_review more convenient * Add Organization and OrganizationMembership * Replace is_admin with level * Extend API * Update team.py * Improve modeling * Improve handling of new mechanics * Add proper migration * Remove _ensure_organization_and_team * Update 0084_org_team_user.py * Improve user, org and team creation * Make MembershipLevel more flexible for the future * Add member deletion * Fix naive datetime warnings * Update setup_review.py * Update API route * Make PersonalAPIKey changes * Update models and migrations, fix typing * Fix typing * Use MAC-less UUID v1 instead of v4 for better performance * Add abstract UUIDModel * Update utils.py * Update utils.py * Fix multi/unicast bit * Update APIs, frontend and tests * Update pull_request_template.md * Fix comment * Fix migration error * Compress migrations * Updates with minimal renaming * More updates * Make further updates * Update test_team_user.py * Fix issues * Add migration * Satisfy mypy * Remove Signup redirect on logged in * Use uuid1_macless in Person * Fix typing * Update tests * Update /api/team/signup to /api/organization/signup * Fix mypy issues and update tests The remaining failures are actually missing functionality (TDD applied), so filling these in. * Update 0086_org_live * Make small improvements * Implement permissions * Remove now unneccesary membership check * Update setup_dev.py * Make small frontend improvements * Add drf-nested-routers as requirement * Remove unused import * Implemented nested routes * Remove cruft * Add relevant org/proj/user name to headings * Fix imports * Update migration * Replace unreliable drf-nested-routers with drf-extensions * Improve unset team handling * Make org and team creation proper * Update migration * Fix migration * Update TopContent * Update command palette for new sidebar structure * Remove deprecated demo data deletion * Assume that each org has a project and fix typing * Require paid plan for multiple orgs and projects * Make HogFlix demo a separate team * Update migration * Slightly improve style * Adjust page layout bottom padding * Make user dropdown nicer * Fix base app tests * Satisfy mypy * Fix test_leave_organization * Improve wording * Possibly fix import * Remove misplaced None check * Enhance org and teams APIs and add tests * Fix /api/projects for particular Team * Improve invites and demo data * Address feedback * Put everything related to billing on Organization * Fix minor issues * Simplify invitation creation * Update team model * Make orgs and projects premium only on self-hosted * Improve testing * Update migration * Remove extra License import * Fix minor issues * Fix Django tests * Fix Cypress * Fix yarn build * Fix TeamSignupViewset * Fix posthog-production incompatibility * Remove extraneous insight endpoint registration * Adjust tests for posthog-production * Simplify invitations and fix email validation * Address all feedback * Satisfy mypy * Update migration * Fix constraint removal in migration * Update tests * Fix test creation edge case * Run posthog-production CI tests against this branch and teams-live * Ensure that js_posthog_api_key is always passed * Fix preflight check pre-login * Update cypress tests * Update instanceStatus.js * Bring ee tets up to par * Bring actions-ux-201012 back * Cypress retry in cypress.json * Revert "Run posthog-production CI tests against this branch and teams-live" This reverts commit d79cb844d823d767b060122af32bd74163593d7a. Co-authored-by: anna <ms.annaphilips@gmail.com> Co-authored-by: Anna Philips <aphilips@matmacorp.com>
2020-10-22 10:58:32 +02:00
organization_membership.level = OrganizationMembership.Level.MEMBER
organization_membership.save()
self.assertTrue(Organization.objects.filter(id=organization.id).exists())
self.assertTrue(Team.objects.filter(id=team.id).exists())
response = self.client.delete(f"/api/organizations/{organization.id}")
self.assertEqual(response.status_code, 403)
self.assertTrue(Organization.objects.filter(id=organization.id).exists())
self.assertTrue(Team.objects.filter(id=team.id).exists())
def test_no_delete_organization_not_belonging_to(self):
# as member only
self.organization_membership.level = OrganizationMembership.Level.ADMIN
self.organization_membership.save()
organization = Organization.objects.create(name="Some Other Org")
response_1 = self.client.delete(f"/api/organizations/{organization.id}")
self.assertEqual(
response_1.data, {"attr": None, "detail": "Not found.", "code": "not_found", "type": "invalid_request"}
)
self.assertEqual(response_1.status_code, 404)
self.assertTrue(Organization.objects.filter(id=organization.id).exists())
# as admin
self.organization_membership.level = OrganizationMembership.Level.MEMBER
self.organization_membership.save()
response_2 = self.client.delete(f"/api/organizations/{organization.id}")
self.assertEqual(
response_2.data, {"attr": None, "detail": "Not found.", "code": "not_found", "type": "invalid_request"}
)
self.assertEqual(response_2.status_code, 404)
self.assertTrue(Organization.objects.filter(id=organization.id).exists())