Exemplo n.º 1
0
    def setUp(self):
        super().setUp()
        self.domain_model = db_actions.crud(
            model="Domain",
            api_model=Domain,
            data={
                "name": "TestDomain",
                "description": "A test domain"
            },
            action="create"
        )
        self.site_model = db_create_entry(
            model="Site",
            data={
                "name": "TestSite",
                "domain_id": self.domain_model.id,
                "description": "A test site",
            },
        )
        self.credentials_data = {
            "site_id": self.site_model.id,
            "account_id": "accountid".rjust(32, "0"),
            "account_secret": "accountsecret".rjust(32, "0"),
            "description": "Super cool test credentials",
        }
        self.credentials_model = db_actions.crud(
            model="Credentials",
            api_model=Credentials,
            data=self.credentials_data,
            action="create"
        )

        self.headers = {API_KEY_HEADER: "test-api-key"}
Exemplo n.º 2
0
    def test_delete_user_data_usersiterole(self):
        user_id = "%s" % uuid.uuid1()
        role_model = db_actions.crud(model="Role",
                                     api_model=Role,
                                     data={
                                         "label": ("%s" % uuid.uuid1())[:30],
                                         "description":
                                         "user_site_role to create",
                                     },
                                     action="create")
        domain_model = db_actions.crud(model="Domain",
                                       api_model=Domain,
                                       data={
                                           "name": ("%s" % uuid.uuid1())[:30],
                                           "description":
                                           "user_site_role to create",
                                       },
                                       action="create")
        for index in range(1, 30):
            site_data = {
                "name": ("%s" % uuid.uuid1())[:30],
                "domain_id": domain_model.id,
                "description": "a super cool test site",
                "client_id": uuid.uuid1().int >> 97,
                "is_active": True,
            }
            site_model = db_create_entry(
                model="Site",
                data=site_data,
            )
            db_actions.crud(model="SiteRole",
                            api_model=SiteRole,
                            data={
                                "role_id": role_model.id,
                                "site_id": site_model.id,
                            },
                            action="create")
            db_actions.crud(model="UserSiteRole",
                            api_model=UserSiteRole,
                            data={
                                "role_id": role_model.id,
                                "site_id": site_model.id,
                                "user_id": user_id,
                            },
                            action="create")

        response = self.client.open(
            '/api/v1/ops/users/{user_id}/delete'.format(user_id=user_id, ),
            method='GET',
            headers=self.headers)
        response_data = json.loads(response.data)
        self.assertEqual(response_data["amount"], 29)

        with self.assertRaises(werkzeug.exceptions.NotFound):
            db_actions.crud(model="UserSiteRole",
                            api_model=UserSiteRole,
                            action="read",
                            query={
                                "user_id": user_id,
                            })
 def test_role_update(self):
     data = {
         "label": ("%s" % uuid.uuid1())[:30],
         "data_schema": {
             "type": "object"
         },
         "description": "a super cool test method",
     }
     model = db_create_entry(
         model="DeletionMethod",
         data=data,
     )
     data = {
         "label": ("%s" % uuid.uuid1())[:30],
         "description": "meh updated",
     }
     data = DeletionMethodUpdate(**data)
     response = self.client.open(
         '/api/v1/deletionmethods/{id}'.format(id=model.id),
         method='PUT',
         data=json.dumps(data),
         content_type='application/json',
         headers=self.headers)
     r_data = json.loads(response.data)
     updated_entry = db_actions.crud(model="DeletionMethod",
                                     api_model=DeletionMethod,
                                     action="read",
                                     query={"id": model.id})
     self.assertEqual(r_data["label"], updated_entry.label)
     self.assertEqual(r_data["description"], updated_entry.description)
 def test_deletion_method_list(self):
     objects = []
     for index in range(1, random.randint(5, 20)):
         deletionmethod_data = {
             "label": ("%s" % uuid.uuid1())[:30],
             "data_schema": {
                 "type": "object"
             },
             "description": "a super cool test method %s" % index,
         }
         objects.append(
             db_create_entry(
                 model="DeletionMethod",
                 data=deletionmethod_data,
             ))
     ids = ",".join(map(str, [model.id for model in objects]))
     query_string = [('deletionmethod_ids', ids)]
     response = self.client.open('/api/v1/deletionmethods',
                                 method='GET',
                                 query_string=query_string,
                                 headers=self.headers)
     r_data = json.loads(response.data)
     self.assertEqual(len(r_data), len(objects) + 1)
     self.assertEqual(int(response.headers["X-Total-Count"]),
                      len(objects) + 1)
     query_string = [('limit', 2), ('deletionmethod_ids', ids)]
     response = self.client.open('/api/v1/deletionmethods',
                                 method='GET',
                                 query_string=query_string,
                                 headers=self.headers)
     r_data = json.loads(response.data)
     self.assertEqual(len(r_data), 2)
     self.assertEqual(int(response.headers["X-Total-Count"]),
                      len(objects) + 1)
    def setUp(self):
        super().setUp()
        self.domain_data = {
            "name": ("%s" % uuid.uuid1())[:30],
            "description": "a super cool test domain",
        }
        self.domain_model = db_actions.crud(model="Domain",
                                            api_model=Domain,
                                            data=self.domain_data,
                                            action="create")
        site_data = {
            "name": ("%s" % uuid.uuid1())[:30],
            "domain_id": self.domain_model.id,
            "description": "a super cool test site",
            "client_id": 0,
            "is_active": True,
        }
        self.site_model = db_create_entry(
            model="Site",
            data=site_data,
        )
        self.roles = []

        # create a bunch of roles.
        for index in range(1, random.randint(5, 20)):
            role_data = {
                "label": ("%s" % uuid.uuid1())[:30],
                "description": "user_site_role to create",
            }
            role_model = db_actions.crud(model="Role",
                                         api_model=Role,
                                         data=role_data,
                                         action="create")
            self.roles.append(role_model)

        self.user_id = "%s" % uuid.uuid1()
        for role in self.roles:
            site_role_data = {
                "role_id": role.id,
                "site_id": self.site_model.id,
            }
            site_role_model = db_actions.crud(model="SiteRole",
                                              api_model=SiteRole,
                                              data=site_role_data,
                                              action="create")

            user_site_role_data = {
                "role_id": role.id,
                "site_id": self.site_model.id,
                "user_id": self.user_id,
            }
            user_site_role_model = db_actions.crud(model="UserSiteRole",
                                                   api_model=UserSiteRole,
                                                   data=user_site_role_data,
                                                   action="create")

        self.headers = {API_KEY_HEADER: "test-api-key"}
    def test_user_site_role_create(self):
        """Test case for user_site_role_create
        """
        role_data = {
            "label": ("%s" % uuid.uuid1())[:30],
            "description": "user_site_role to create",
        }
        role_model = db_actions.crud(model="Role",
                                     api_model=Role,
                                     data=role_data,
                                     action="create")
        domain_data = {
            "name": ("%s" % uuid.uuid1())[:30],
            "description": "a super cool test domain",
        }
        domain_model = db_actions.crud(model="Domain",
                                       api_model=Domain,
                                       data=domain_data,
                                       action="create")
        site_data = {
            "name": ("%s" % uuid.uuid1())[:30],
            "domain_id": domain_model.id,
            "description": "a super cool test site",
            "client_id": uuid.uuid1().int >> 97,
            "is_active": True,
        }
        site_model = db_create_entry(
            model="Site",
            data=site_data,
        )
        site_role_data = {
            "role_id": role_model.id,
            "site_id": site_model.id,
        }
        site_role_model = db_actions.crud(model="SiteRole",
                                          api_model=SiteRole,
                                          data=site_role_data,
                                          action="create")

        data = UserSiteRoleCreate(
            **{
                "role_id": role_model.id,
                "site_id": site_model.id,
                "user_id": "%s" % uuid.uuid1(),
            })
        response = self.client.open('/api/v1/usersiteroles',
                                    method='POST',
                                    data=json.dumps(data),
                                    content_type='application/json',
                                    headers=self.headers)
        r_data = json.loads(response.data)
        self.assertEqual(r_data["role_id"], data.role_id)
        self.assertEqual(r_data["site_id"], data.site_id)
    def setUp(self):
        super().setUp()
        self.role_data = {
            "label": ("%s" % uuid.uuid4())[:30],
            "description": "invitation_site_role to create",
        }
        self.role_model = db_actions.crud(model="Role",
                                          api_model=Role,
                                          data=self.role_data,
                                          action="create")
        self.domain_data = {
            "name": ("%s" % uuid.uuid4())[:30],
            "description": "a super cool test domain",
        }
        self.domain_model = db_actions.crud(model="Domain",
                                            api_model=Domain,
                                            data=self.domain_data,
                                            action="create")
        self.site_data = {
            "name": ("%s" % uuid.uuid4())[:30],
            "description": "a super cool test site",
            "domain_id": self.domain_model.id
        }
        self.site_model = db_create_entry(
            model="Site",
            data=self.site_data,
        )

        self.site_role_data = {
            "role_id": self.role_model.id,
            "site_id": self.site_model.id
        }
        self.site_role_model = db_actions.crud(model="SiteRole",
                                               api_model=SiteRole,
                                               data=self.site_role_data,
                                               action="create")
        self.invitation_data = {
            "first_name": "first",
            "last_name": "last",
            "email": "*****@*****.**",
            "organisation_id": 1,
            "invitor_id": "%s" % uuid.uuid1(),
            "expires_at": datetime.now()
        }
        self.invitation_model = db_actions.crud(model="Invitation",
                                                api_model=Invitation,
                                                data=self.invitation_data,
                                                action="create")

        self.headers = {API_KEY_HEADER: "test-api-key"}
    def setUp(self):
        super().setUp()
        self.role_data = {
            "label": ("%s" % uuid.uuid1())[:30],
            "description": "user_site_role to create",
        }
        self.role_model = db_actions.crud(model="Role",
                                          api_model=Role,
                                          data=self.role_data,
                                          action="create")
        self.domain_data = {
            "name": ("%s" % uuid.uuid1())[:30],
            "description": "a super cool test domain",
        }
        self.domain_model = db_actions.crud(model="Domain",
                                            api_model=Domain,
                                            data=self.domain_data,
                                            action="create")
        self.site_data = {
            "name": ("%s" % uuid.uuid1())[:30],
            "domain_id": self.domain_model.id,
            "description": "a super cool test site",
            "client_id": uuid.uuid1().int >> 97,
            "is_active": True,
        }
        self.site_model = db_create_entry(
            model="Site",
            data=self.site_data,
        )
        self.site_role_data = {
            "role_id": self.role_model.id,
            "site_id": self.site_model.id,
        }
        self.site_role_model = db_actions.crud(model="SiteRole",
                                               api_model=SiteRole,
                                               data=self.site_role_data,
                                               action="create")

        self.user_site_role_data = {
            "role_id": self.role_model.id,
            "site_id": self.site_model.id,
            "user_id": "%s" % uuid.uuid1(),
        }
        self.user_site_role_model = db_actions.crud(
            model="UserSiteRole",
            api_model=UserSiteRole,
            data=self.user_site_role_data,
            action="create")

        self.headers = {API_KEY_HEADER: "test-api-key"}
 def test_deletion_method_read(self):
     deletionmethod_data = {
         "label": ("%s" % uuid.uuid1())[:30],
         "data_schema": {
             "type": "object"
         },
         "description": "a super cool test method",
     }
     model = db_create_entry(
         model="DeletionMethod",
         data=deletionmethod_data,
     )
     response = self.client.open(
         '/api/v1/deletionmethods/{id}'.format(id=model.id),
         method='GET',
         headers=self.headers)
     r_data = json.loads(response.data)
     self.assertEqual(r_data["label"], model.label)
     self.assertEqual(r_data["data_schema"], model.data_schema)
     self.assertEqual(r_data["description"], model.description)
    def test_deletion_method_delete(self):
        deletionmethod_data = {
            "label": ("%s" % uuid.uuid1())[:30],
            "data_schema": {
                "type": "object"
            },
            "description": "a super cool test method",
        }
        model = db_create_entry(
            model="DeletionMethod",
            data=deletionmethod_data,
        )
        response = self.client.open(
            '/api/v1/deletionmethods/{id}'.format(id=model.id),
            method='DELETE',
            headers=self.headers)

        with self.assertRaises(werkzeug.exceptions.NotFound):
            db_actions.crud(model="DeletionMethod",
                            api_model=DeletionMethod,
                            action="read",
                            query={"id": model.id})
    def test_site_role_update(self):
        """Test case for site_role_update
        """
        role_data = {
            "label": ("%s" % uuid.uuid1())[:30],
            "description": "site_role to create",
        }
        role_model = db_actions.crud(
            model="Role",
            api_model=Role,
            data=role_data,
            action="create"
        )
        domain_data = {
            "name": ("%s" % uuid.uuid1())[:30],
            "description": "site_role to create",
        }
        domain_model = db_actions.crud(
            model="Domain",
            api_model=Domain,
            data=domain_data,
            action="create"
        )
        site_data = {
            "name": ("%s" % uuid.uuid1())[:30],
            "domain_id": domain_model.id,
            "description": "a super cool test site",
            "client_id": uuid.uuid1().int>>97,
            "is_active": True,
        }
        site_model = db_create_entry(
            model="Site",
            data=site_data,
        )
        site_role_data = {
            "role_id": role_model.id,
            "site_id": site_model.id,
            "grant_implicitly": True
        }
        site_role_model = db_actions.crud(
            model="SiteRole",
            api_model=SiteRole,
            data=site_role_data,
            action="create"
        )

        # Change grant on the model.
        data = {
            "grant_implicitly": False
        }
        data = SiteRoleUpdate(
            **data
        )
        response = self.client.open(
            '/api/v1/siteroles/{site_id}/{role_id}'.format(
                site_id=site_role_model.site_id,
                role_id=site_role_model.role_id,
            ),
            method='PUT',
            data=json.dumps(data),
            content_type='application/json',
            headers=self.headers)
        r_data = json.loads(response.data)
        updated_entry = db_actions.crud(
            model="SiteRole",
            api_model=SiteRole,
            action="read",
            query={
                "role_id": site_role_model.role_id,
                "site_id": site_role_model.site_id,
            }
        )
        self.assertEqual(r_data["grant_implicitly"], updated_entry.grant_implicitly)
Exemplo n.º 12
0
    def setUp(self):
        super().setUp()
        # Parent Domain
        self.domain_parent_data = {
            "name": ("%s" % uuid.uuid1())[:30],
            "description": "The Root Domain",
        }
        self.domain_parent_model = db_actions.crud(
            model="Domain",
            api_model=Domain,
            data=self.domain_parent_data,
            action="create")
        # Child Domain
        self.domain_child_data = {
            "name": ("%s" % uuid.uuid1())[:30],
            "description": "The Child Domain",
            "parent_id": self.domain_parent_model.id
        }
        self.domain_child_model = db_actions.crud(model="Domain",
                                                  api_model=Domain,
                                                  data=self.domain_child_data,
                                                  action="create")
        # Site Child
        self.site_data = {
            "name": ("%s" % uuid.uuid1())[:30],
            "domain_id": self.domain_child_model.id,
            "description": "A Site",
            "client_id": 1,
            "is_active": True,
        }
        self.site = db_create_entry(
            model="Site",
            data=self.site_data,
        )
        # Create some roles.
        self.roles = []
        for role in ROLES:
            role_model = db_actions.crud(model="Role",
                                         api_model=Role,
                                         data=role,
                                         action="create")
            self.roles.append(role_model)
        # Some users as well.
        self.user_id_1 = "%s" % uuid.uuid1()
        self.user_id_2 = "%s" % uuid.uuid1()

        for role in self.roles:
            domain_role_data = {
                "domain_id": self.domain_parent_model.id,
                "role_id": role.id,
                "grant_implicitly": "view" in role.description
            }
            db_actions.crud(model="DomainRole",
                            api_model=DomainRole,
                            data=domain_role_data,
                            action="create")
            if not domain_role_data["grant_implicitly"]:
                user_domain_role_data = {
                    "user_id": self.user_id_1,
                    "domain_id": self.domain_parent_model.id,
                    "role_id": role.id
                }
                db_actions.crud(model="UserDomainRole",
                                api_model=UserDomainRole,
                                data=user_domain_role_data,
                                action="create")
            domain_role_data = {
                "domain_id": self.domain_child_model.id,
                "role_id": role.id,
                "grant_implicitly": "view" in role.description
            }
            db_actions.crud(model="DomainRole",
                            api_model=DomainRole,
                            data=domain_role_data,
                            action="create")
            if "create" in role.description:
                user_domain_role_data = {
                    "user_id": self.user_id_2,
                    "domain_id": self.domain_child_model.id,
                    "role_id": role.id
                }
                db_actions.crud(model="UserDomainRole",
                                api_model=UserDomainRole,
                                data=user_domain_role_data,
                                action="create")
            site_role_data = {
                "site_id": self.site.id,
                "role_id": role.id,
                "grant_implicitly": "view" in role.description
            }
            db_actions.crud(model="SiteRole",
                            api_model=SiteRole,
                            data=site_role_data,
                            action="create")

            if "update" in role.description:
                user_site_role_data = {
                    "user_id": self.user_id_2,
                    "site_id": self.site.id,
                    "role_id": role.id
                }
                db_actions.crud(model="UserSiteRole",
                                api_model=UserSiteRole,
                                data=user_site_role_data,
                                action="create")

        self.headers = {API_KEY_HEADER: "test-api-key"}
    def setUp(self):
        super().setUp()
        role_data = {
            "label": ("%s" % uuid.uuid4())[:30],
            "description": "invitation_site_role to create"
        }
        self.role_model_1 = db_actions.crud(model="Role",
                                            api_model=Role,
                                            data=role_data,
                                            action="create")
        role_data = {
            "label": ("%s" % uuid.uuid4())[:30],
            "description": "invitation_site_role to create"
        }
        self.role_model_2 = db_actions.crud(model="Role",
                                            api_model=Role,
                                            data=role_data,
                                            action="create")
        domain_data = {
            "name": ("%s" % uuid.uuid4())[:30],
            "description": "a super cool test domain",
        }
        self.domain_model = db_actions.crud(model="Domain",
                                            api_model=Domain,
                                            data=domain_data,
                                            action="create")
        domain_data = {
            "name": ("%s" % uuid.uuid4())[:30],
            "description": "a super cool test domain 2"
        }
        self.domain_model_2 = db_actions.crud(model="Domain",
                                              api_model=Domain,
                                              data=domain_data,
                                              action="create")
        domain_role_data = {
            "role_id": self.role_model_1.id,
            "domain_id": self.domain_model.id
        }
        self.domain_role_model = db_actions.crud(model="DomainRole",
                                                 api_model=DomainRole,
                                                 data=domain_role_data,
                                                 action="create")
        site_data = {
            "name": ("%s" % uuid.uuid4())[:30],
            "description": "a super cool test site",
            "domain_id": self.domain_model_2.id
        }
        self.site_model = db_create_entry(
            model="Site",
            data=site_data,
        )
        site_role_data = {
            "role_id": self.role_model_1.id,
            "site_id": self.site_model.id
        }
        self.site_role_model = db_actions.crud(model="SiteRole",
                                               api_model=SiteRole,
                                               data=site_role_data,
                                               action="create")
        site_role_data = {
            "role_id": self.role_model_2.id,
            "site_id": self.site_model.id
        }
        self.site_role_model = db_actions.crud(model="SiteRole",
                                               api_model=SiteRole,
                                               data=site_role_data,
                                               action="create")
        invitation_data = {
            "first_name": "first",
            "last_name": "last",
            "email": "*****@*****.**",
            "organisation_id": 1,
            "invitor_id": "%s" % uuid.uuid1(),
            "expires_at": datetime.now() + timedelta(days=1)
        }
        self.invitation_model = db_actions.crud(model="Invitation",
                                                api_model=Invitation,
                                                data=invitation_data,
                                                action="create")
        invitation_domain_role_data = {
            "invitation_id": self.invitation_model.id,
            "domain_id": self.domain_model.id,
            "role_id": self.role_model_1.id
        }
        self.invitation_domain_role_model = db_actions.crud(
            model="InvitationDomainRole",
            api_model=InvitationDomainRole,
            data=invitation_domain_role_data,
            action="create")
        invitation_site_role_data = {
            "invitation_id": self.invitation_model.id,
            "site_id": self.site_model.id,
            "role_id": self.role_model_1.id
        }
        self.invitation_site_role_model = db_actions.crud(
            model="InvitationSiteRole",
            api_model=InvitationSiteRole,
            data=invitation_site_role_data,
            action="create")
        invitation_site_role_data = {
            "invitation_id": self.invitation_model.id,
            "site_id": self.site_model.id,
            "role_id": self.role_model_2.id
        }
        self.invitation_site_role_model = db_actions.crud(
            model="InvitationSiteRole",
            api_model=InvitationSiteRole,
            data=invitation_site_role_data,
            action="create")
        expired_invite_data = {
            "first_name": "first",
            "last_name": "last",
            "email": "*****@*****.**",
            "organisation_id": 1,
            "invitor_id": "%s" % uuid.uuid1(),
            "expires_at": datetime.now() - timedelta(days=1)
        }
        self.expired_invite = db_actions.crud(model="Invitation",
                                              api_model=Invitation,
                                              data=expired_invite_data,
                                              action="create")
        invitation_redirect_url_data = {
            "url": "http://example.com/redirect?foo=bar",
            "description": "A test redirect URL"
        }
        self.invitation_redirect_url = db_actions.crud(
            model="InvitationRedirectUrl",
            api_model=InvitationRedirectUrl,
            data=invitation_redirect_url_data,
            action="create")

        self.headers = {API_KEY_HEADER: "test-api-key"}
Exemplo n.º 14
0
    def test_sql_atomic_nature(self):
        user_id = "%s" % uuid.uuid1()
        role_model = db_actions.crud(model="Role",
                                     api_model=Role,
                                     data={
                                         "label": ("%s" % uuid.uuid1())[:30],
                                         "description":
                                         "user_site_role to create",
                                     },
                                     action="create")
        domain_model = db_actions.crud(model="Domain",
                                       api_model=Domain,
                                       data={
                                           "name": ("%s" % uuid.uuid1())[:30],
                                           "description":
                                           "user_site_role to create",
                                       },
                                       action="create")
        for index in range(1, 30):
            site_data = {
                "name": ("%s" % uuid.uuid1())[:30],
                "domain_id": domain_model.id,
                "description": "a super cool test site",
                "client_id": uuid.uuid1().int >> 97,
                "is_active": True,
            }
            site_model = db_create_entry(
                model="Site",
                data=site_data,
            )
            db_actions.crud(model="SiteRole",
                            api_model=SiteRole,
                            data={
                                "role_id": role_model.id,
                                "site_id": site_model.id,
                            },
                            action="create")
            db_actions.crud(model="UserSiteRole",
                            api_model=UserSiteRole,
                            data={
                                "role_id": role_model.id,
                                "site_id": site_model.id,
                                "user_id": user_id,
                            },
                            action="create")
        for index in range(1, 35):
            domain_model = db_actions.crud(model="Domain",
                                           api_model=Domain,
                                           data={
                                               "name":
                                               ("%s" % uuid.uuid1())[:30],
                                               "description":
                                               "user_site_role to create",
                                           },
                                           action="create")
            db_actions.crud(model="DomainRole",
                            api_model=DomainRole,
                            data={
                                "role_id": role_model.id,
                                "domain_id": domain_model.id
                            },
                            action="create")
            db_actions.crud(model="UserDomainRole",
                            api_model=UserDomainRole,
                            data={
                                "role_id": role_model.id,
                                "domain_id": domain_model.id,
                                "user_id": user_id
                            },
                            action="create")

        # SQL being mocked needs to break, to ensure nothing is committed
        # unless the entire query is successful
        mocked_sql = """
        -- Given a user id (:user_id),
        -- delete UserDomainRoles and UserSiteRoles tied to user id

        WITH deleted_site_roles AS (
            DELETE FROM user_site_role
                WHERE user_id = :user_id
            RETURNING user_id
        ),
        deleted_domain_roles AS (
            DELETE FROM fooooooo -- This is meant to break
                WHERE none_valid = :user_id
            RETURNING user_id
        ),
        deleted_rows AS (
           SELECT * FROM deleted_site_roles
           UNION ALL  -- ALL is required so that duplicates are not dropped
           SELECT * FROM deleted_domain_roles
        )

        SELECT COUNT(*) AS amount
          FROM deleted_rows;
        """
        with mock.patch(
                "swagger_server.controllers.operational_controller.SQL_DELETE_USER_DATA",
                new_callable=lambda: mocked_sql):
            response = self.client.open(
                '/api/v1/ops/users/{user_id}/delete'.format(user_id=user_id, ),
                method='GET',
                headers=self.headers)

        response_data = json.loads(response.data)
        self.assertIn('relation "fooooooo" does not e', response_data["error"])
        site_roles = db_actions.crud(model="UserSiteRole",
                                     api_model=UserSiteRole,
                                     action="list",
                                     query={
                                         "order_by": ["user_id"],
                                         "ids": {
                                             "user_id": user_id
                                         }
                                     })
        self.assertEqual(len(site_roles[0]), 29)
        domain_roles = db_actions.crud(model="UserDomainRole",
                                       api_model=UserDomainRole,
                                       action="list",
                                       query={
                                           "order_by": ["user_id"],
                                           "ids": {
                                               "user_id": user_id
                                           }
                                       })
        self.assertEqual(len(domain_roles[0]), 34)
    def test_user_site_role_list(self):
        """Test case for user_site_role_list
        """
        objects = []
        role_data = {
            "label": ("%s" % uuid.uuid1())[:30],
            "description": "user_site_role to create",
        }
        role_model = db_actions.crud(model="Role",
                                     api_model=Role,
                                     data=role_data,
                                     action="create")
        for index in range(1, random.randint(5, 20)):
            domain_data = {
                "name": ("%s" % uuid.uuid1())[:30],
                "description": "user_site_role to create",
            }
            domain_model = db_actions.crud(model="Domain",
                                           api_model=Domain,
                                           data=domain_data,
                                           action="create")
            site_data = {
                "name": ("%s" % uuid.uuid1())[:30],
                "domain_id": domain_model.id,
                "description": "a super cool test site",
                "client_id": uuid.uuid1().int >> 97,
                "is_active": True,
            }
            site_model = db_create_entry(
                model="Site",
                data=site_data,
            )
            site_role_data = {
                "role_id": role_model.id,
                "site_id": site_model.id,
            }
            site_role_model = db_actions.crud(model="SiteRole",
                                              api_model=SiteRole,
                                              data=site_role_data,
                                              action="create")

            user_site_role_data = {
                "role_id": role_model.id,
                "site_id": site_model.id,
                "user_id": "%s" % uuid.uuid1(),
            }
            objects.append(
                db_actions.crud(model="UserSiteRole",
                                api_model=UserSiteRole,
                                data=user_site_role_data,
                                action="create"))
        query_string = [  #('offset', 0),
            ('role_id', role_model.id),
        ]
        response = self.client.open('/api/v1/usersiteroles',
                                    method='GET',
                                    query_string=query_string,
                                    headers=self.headers)
        r_data = json.loads(response.data)
        self.assertEqual(len(r_data), len(objects))
        self.assertEqual(int(response.headers["X-Total-Count"]), len(objects))
        query_string = [  #('offset', 0),
            ('limit', 2),
            ('role_id', role_model.id),
        ]
        response = self.client.open('/api/v1/usersiteroles',
                                    method='GET',
                                    query_string=query_string,
                                    headers=self.headers)
        r_data = json.loads(response.data)
        self.assertEqual(len(r_data), 2)
        self.assertEqual(int(response.headers["X-Total-Count"]), len(objects))
    def test_user_site_role_delete(self):
        """Test case for user_site_role_delete
        """
        role_data = {
            "label": ("%s" % uuid.uuid1())[:30],
            "description": "user_site_role to create",
        }
        role_model = db_actions.crud(model="Role",
                                     api_model=Role,
                                     data=role_data,
                                     action="create")
        domain_data = {
            "name": ("%s" % uuid.uuid1())[:30],
            "description": "user_site_role to create",
        }
        domain_model = db_actions.crud(model="Domain",
                                       api_model=Domain,
                                       data=domain_data,
                                       action="create")
        site_data = {
            "name": ("%s" % uuid.uuid1())[:30],
            "domain_id": domain_model.id,
            "description": "a super cool test site",
            "client_id": uuid.uuid1().int >> 97,
            "is_active": True,
        }
        site_model = db_create_entry(
            model="Site",
            data=site_data,
        )
        site_role_data = {
            "role_id": role_model.id,
            "site_id": site_model.id,
        }
        site_role_model = db_actions.crud(model="SiteRole",
                                          api_model=SiteRole,
                                          data=site_role_data,
                                          action="create")

        user_site_role_data = {
            "role_id": role_model.id,
            "site_id": site_model.id,
            "user_id": "%s" % uuid.uuid1(),
        }
        model = db_actions.crud(model="UserSiteRole",
                                api_model=UserSiteRole,
                                data=user_site_role_data,
                                action="create")
        response = self.client.open(
            '/api/v1/usersiteroles/{user_id}/{site_id}/{role_id}'.format(
                user_id=model.user_id,
                site_id=model.site_id,
                role_id=model.role_id,
            ),
            method='DELETE',
            headers=self.headers)

        # Little crude. Raise an error if the object actually still exists else
        # pass after the 404 error.
        with self.assertRaises(werkzeug.exceptions.NotFound):
            db_actions.crud(model="UserSiteRole",
                            api_model=UserSiteRole,
                            action="read",
                            query={
                                "role_id": model.role_id,
                                "site_id": model.site_id,
                            })
    def setUp(self):
        super().setUp()
        # Create top level parent domain.
        self.domain_data = {
            "name": ("%s" % uuid.uuid1())[:30],
            "description": "a super cool test domain",
        }
        self.domain_model = db_actions.crud(model="Domain",
                                            api_model=Domain,
                                            data=self.domain_data,
                                            action="create")
        role_data = {
            "label": ("%s" % uuid.uuid1())[:30],
            "description": "user_site_role to create",
        }
        role_model = db_actions.crud(model="Role",
                                     api_model=Role,
                                     data=role_data,
                                     action="create")
        domain_role_data = {
            "role_id": role_model.id,
            "domain_id": self.domain_model.id
        }
        db_actions.crud(model="DomainRole",
                        api_model=DomainRole,
                        data=domain_role_data,
                        action="create")

        # Set a single role on the top level domain.
        self.data = OrderedDict()
        self.data["d:%s" % self.domain_model.id] = [role_model.id]

        domain_id = self.domain_model.id
        for index in range(1, random.randint(5, 20)):
            # Create a domain tree with roles per domain.
            domain_data = {
                "name": ("%s" % uuid.uuid1())[:30],
                "description": "%s" % uuid.uuid1(),
                "parent_id": domain_id
            }
            domain_model = db_actions.crud(model="Domain",
                                           api_model=Domain,
                                           data=domain_data,
                                           action="create")

            # Set id for next iteration.
            domain_id = domain_model.id
            roles = []

            self.data["d:%s" % domain_model.id] = []
            for index in range(1, random.randint(5, 20)):
                role_data = {
                    "label": ("%s" % uuid.uuid1())[:30],
                    "description": "user_site_role to create",
                }
                role_model = db_actions.crud(model="Role",
                                             api_model=Role,
                                             data=role_data,
                                             action="create")
                roles.append(role_model)
            for role in roles:
                domain_role_data = {
                    "role_id": role.id,
                    "domain_id": domain_model.id
                }
                db_actions.crud(model="DomainRole",
                                api_model=DomainRole,
                                data=domain_role_data,
                                action="create")
                self.data["d:%s" % domain_model.id].append(role.id)

        # Assign the site to the last domain in the tree.
        site_data = {
            "name": ("%s" % uuid.uuid1())[:30],
            "domain_id": domain_id,
            "description": "a super cool test site",
            "client_id": 0,
            "is_active": True,
        }
        self.site_model = db_create_entry(
            model="Site",
            data=site_data,
        )

        # create a bunch of roles for a site..
        self.data["s:%s" % self.site_model.id] = []
        for index in range(1, random.randint(5, 20)):
            role_data = {
                "label": ("%s" % uuid.uuid1())[:30],
                "description": "user_site_role to create",
            }
            role_model = db_actions.crud(model="Role",
                                         api_model=Role,
                                         data=role_data,
                                         action="create")
            site_role_data = {
                "role_id": role_model.id,
                "site_id": self.site_model.id,
            }
            site_role_model = db_actions.crud(model="SiteRole",
                                              api_model=SiteRole,
                                              data=site_role_data,
                                              action="create")
            self.data["s:%s" % self.site_model.id].append(role_model.id)

        self.headers = {API_KEY_HEADER: "test-api-key"}
 def setUp(self):
     """
     Top Level Domain
      |
      +-- Child Domain 1
      |    |
      |    +-- Site 1
      |    +-- Site 2
      |
      +-- Child Domain 2
           |
           +-- Site 3
     :return:
     """
     super().setUp()
     # Parent Domain
     self.top_level_domain_data = {
         "name": "The Top Level Domain",
         "description": "The Top Level Domain",
     }
     self.top_level_domain = db_actions.crud(
         model="Domain",
         api_model=Domain,
         data=self.top_level_domain_data,
         action="create"
     )
     # Child Domain 1
     self.child_domain_data = {
         "name": "Child Domain 1",
         "description": "The 1st Child Domain",
         "parent_id": self.top_level_domain.id
     }
     self.child_domain_1 = db_actions.crud(
         model="Domain",
         api_model=Domain,
         data=self.child_domain_data,
         action="create"
     )
     # Child Domain 2
     self.child_domain_data = {
         "name": "Child Domain 2",
         "description": "The 2nd Child Domain",
         "parent_id": self.top_level_domain.id
     }
     self.child_domain_2 = db_actions.crud(
         model="Domain",
         api_model=Domain,
         data=self.child_domain_data,
         action="create"
     )
     # Site 1
     self.site_data = {
         "name": "Site 1",
         "domain_id": self.child_domain_1.id,
         "description": "Site 1",
         "is_active": True,
     }
     self.site_1 = db_create_entry(
         model="Site",
         data=self.site_data,
     )
     # Site 2
     self.site_data = {
         "name": "Site 2",
         "domain_id": self.child_domain_1.id,
         "description": "Site 2",
         "is_active": True,
     }
     self.site_2 = db_create_entry(
         model="Site",
         data=self.site_data,
     )
     # Site 3
     self.site_data = {
         "name": "Site 3",
         "domain_id": self.child_domain_2.id,
         "description": "Site 3",
         "is_active": True,
     }
     self.site_3 = db_create_entry(
         model="Site",
         data=self.site_data,
     )
     self.headers = {API_KEY_HEADER: "test-api-key"}