def test_can_switch_membership_of_an_inactive_profile(
            self, client, profile, mdm_group):
        profile = Profile(**profile)
        profile.status = ProfileStatus.INACTIVE
        mdm_group = MDMGroup(**mdm_group)

        cdatabase.db_session.add(profile)
        cdatabase.db_session.add(mdm_group)
        cdatabase.db_session.commit()

        res = client.put(
            url_for('api_app.mdmgroupresource_profile',
                    id=mdm_group.id,
                    profile_id=profile.id))

        assert self.assert_json(res.headers)
        assert self.assert_success(res)
        data = json.loads(res.data)
        assert data['message'] == "Success"

        res = client.delete(
            url_for('api_app.mdmgroupresource_profile',
                    id=mdm_group.id,
                    profile_id=profile.id))

        assert self.assert_json(res.headers)
        assert self.assert_success(res)
        data = json.loads(res.data)
        assert data['message'] == "Success"
    def test_when_device_is_added_to_group_profiles_are_installed(
            self, client, device, profile, mdm_group):
        device = Device(**device)
        profile = Profile(**profile)
        profile_service = mock.MagicMock()

        # FIXME: there has to be a better way...
        original_profile_service = capi.ProfileService

        mdm_group = MDMGroup(**mdm_group)
        profile.status = ProfileStatus.ACTIVE
        mdm_group.profiles.append(profile)
        cdatabase.db_session.add(device)
        cdatabase.db_session.add(profile)
        cdatabase.db_session.add(mdm_group)
        cdatabase.db_session.commit()

        try:
            capi.ProfileService = mock.MagicMock()
            capi.ProfileService.return_value = profile_service
            res = client.put(
                url_for('api_app.mdmgroupresource_device',
                        id=mdm_group.id,
                        device_id=device.id))
        finally:
            capi.ProfileService = original_profile_service

        assert self.assert_json(res.headers)
        assert self.assert_success(res)
        data = json.loads(res.data)
        assert data['message'] == "Success"
        profile_service.finalize_installation.assert_called_once_with(
            profile, device)
    def test_cannot_switch_profile_membership_if_its_not_inactive(
            self, client, profile, mdm_group):
        profile = Profile(**profile)
        mdm_group = MDMGroup(**mdm_group)

        cdatabase.db_session.add(profile)
        cdatabase.db_session.add(mdm_group)
        cdatabase.db_session.commit()

        for status in list(ProfileStatus):
            if status == ProfileStatus.INACTIVE:
                continue

            profile.status = status
            cdatabase.db_session.commit()
            res = client.put(
                url_for('api_app.mdmgroupresource_profile',
                        id=mdm_group.id,
                        profile_id=profile.id))

            assert self.assert_json(res.headers)
            data = json.loads(res.data)

            assert res.status_code == 400
            assert data[
                'message'] == "Cannot change groups while profile is not inactive"
    def test_when_device_is_removed_from_group_profiles_are_removed(self, client, device, profile, mdm_group):
        device = Device(**device)
        profile = Profile(**profile)
        profile_service = mock.MagicMock()

        # FIXME: there has to be a better way...
        original_profile_service = capi.ProfileService
        capi.ProfileService = mock.MagicMock()
        capi.ProfileService.return_value = profile_service

        mdm_group = MDMGroup(**mdm_group)
        profile.status = ProfileStatus.ACTIVE
        mdm_group.devices.append(device)
        mdm_group.profiles.append(profile)
        cdatabase.db_session.add(device)
        cdatabase.db_session.add(profile)
        cdatabase.db_session.add(mdm_group)
        cdatabase.db_session.commit()

        try:
            capi.ProfileService = mock.MagicMock()
            capi.ProfileService.return_value = profile_service
            res = client.delete(url_for('api_app.mdmgroupresource_device', id=mdm_group.id, device_id=device.id))
        finally:
            capi.ProfileService = original_profile_service

        assert self.assert_json(res.headers)
        assert self.assert_success(res)
        data = json.loads(res.data)
        assert data['message'] == "Success"
        profile_service.finalize_removal.assert_called_once_with(profile, device)
Example #5
0
    def test_delete(self, client, profile):
        profile_dict = profile
        for status in list(ProfileStatus):
            profile = Profile(**profile_dict)
            profile.status = status
            cdatabase.db_session.add(profile)
            cdatabase.db_session.commit()
            profile_id = profile.id

            res = client.delete(url_for('api_app.profileresource', id=profile_id))

            res = client.get(url_for('api_app.profileresource', id=profile_id))
            assert self.assert_json(res.headers)

            deleted = False
            if status == ProfileStatus.ACTIVE:
                assert self.assert_success(res)
                assert profile.status == ProfileStatus.PENDING_DELETION
            elif status == ProfileStatus.INACTIVE:
                assert res.status_code == 404
                deleted = True
            else:
                assert profile.status == status

            if not deleted:
                cdatabase.db_session.delete(profile)
                cdatabase.db_session.commit()
Example #6
0
    def test_undeploy_works_only_if_active(self, client, profile, app):
        profile = Profile(**profile)

        redis_client = app.redis_store._redis_client

        cdatabase.db_session.add(profile)
        cdatabase.db_session.commit()

        for status in list(ProfileStatus):
            redis_client.flushall()
            profile.status = status
            cdatabase.db_session.commit()
            res = client.delete(url_for('api_app.profileresource_deploy', id=profile.id))
            assert self.assert_json(res.headers)

            if status == ProfileStatus.ACTIVE:
                assert self.assert_success(res)
                assert profile.status == ProfileStatus.PENDING_REMOVAL
                assert get_queued_jobs(redis_client) == ['commandment.tasks.process_profile_deployment_change(%d)' % profile.id]
            elif status == ProfileStatus.INACTIVE:
                assert self.assert_success(res)
                assert profile.status == ProfileStatus.INACTIVE
                assert get_queued_jobs(redis_client) == []
            else:
                assert res.status_code == 400
                assert profile.status == status
                assert get_queued_jobs(redis_client) == []
Example #7
0
    def test_deploy_changes_iff_status_is_correct_pending(self, profile, mdm_group, device):
        profile = Profile(**profile)
        mdm_group = MDMGroup(**mdm_group)
        device = Device(**device)
        profile_service = MagicMock()

        profile.mdm_groups = [mdm_group]
        mdm_group.devices = [device]

        tasks.ProfileService = MagicMock()
        tasks.ProfileService.return_value = profile_service
        tasks.db_session = MagicMock()
        query = MagicMock()
        tasks.db_session.query.return_value = query
        query.get.return_value = profile

        for status in list(ProfileStatus):
            profile.status = status
            tasks.push_to_device = MagicMock()
            tasks.db_session.add = MagicMock()

            tasks.process_profile_deployment_change(7)

            if status == ProfileStatus.PENDING_INSTALLATION:
                assert profile.status == ProfileStatus.ACTIVE
                tasks.push_to_device.assert_called_once_with(device)
            elif status == ProfileStatus.PENDING_DELETION:
                assert profile.status == ProfileStatus.PENDING_DELETION
                tasks.push_to_device.assert_called_once_with(device)
            elif status == ProfileStatus.PENDING_REMOVAL:
                assert profile.status == ProfileStatus.INACTIVE
                tasks.push_to_device.assert_called_once_with(device)
            else:
                assert profile.status == status
                assert not tasks.push_to_device.called
Example #8
0
    def test_remove_queues_a_job_only_if_status_is_active(self, profile, app):
        profile = Profile(**profile)
        profile_server = service.ProfileService()

        redis_client = app.redis_store._redis_client

        for status in list(ProfileStatus):
            redis_client.flushall()
            profile.id = 1
            profile.status = status
            try:
                profile_server.remove(profile)
            except werkzeug.exceptions.BadRequest as e:
                bad_request = True
            else:
                bad_request = False

            if status == ProfileStatus.ACTIVE:
                assert profile.status == ProfileStatus.PENDING_REMOVAL
                assert get_queued_jobs(redis_client) == ['commandment.tasks.process_profile_deployment_change(%d)' % profile.id]
                assert not bad_request
            elif status == ProfileStatus.INACTIVE:
                assert profile.status == ProfileStatus.INACTIVE
                assert get_queued_jobs(redis_client) == []
                assert not bad_request
            else:
                assert profile.status == status
                assert get_queued_jobs(redis_client) == []
                assert bad_request
    def test_cannot_switch_profile_membership_if_its_not_inactive(self, client, profile, mdm_group):
        profile = Profile(**profile)
        mdm_group = MDMGroup(**mdm_group)

        cdatabase.db_session.add(profile)
        cdatabase.db_session.add(mdm_group)
        cdatabase.db_session.commit()

        for status in list(ProfileStatus):
            if status == ProfileStatus.INACTIVE:
                continue

            profile.status = status
            cdatabase.db_session.commit()
            res = client.put(url_for('api_app.mdmgroupresource_profile', id=mdm_group.id, profile_id=profile.id))

            assert self.assert_json(res.headers)
            data = json.loads(res.data)

            assert res.status_code == 400
            assert data['message'] == "Cannot change groups while profile is not inactive"
Example #10
0
    def test_finalize_installation(self, profile, device):
        profile = Profile(**profile)
        device = Device(**device)
        profile_service = service.ProfileService()
        profile.id = 7

        service.db_session = mock.MagicMock()
        query = mock.MagicMock()
        service.db_session.query.return_value = query
        query.get.return_value = profile

        installqc = object()
        service.InstallProfile = mock.MagicMock()
        service.InstallProfile.new_queued_command.return_value = installqc

        service.db_session.add = mock.MagicMock()

        profile_service.finalize_installation(profile, device)

        assert service.InstallProfile.new_queued_command.call_args[0] == (device, {'id': 7})
        service.db_session.add.assert_called_once_with(installqc)
    def test_can_switch_membership_of_an_inactive_profile(self, client, profile, mdm_group):
        profile = Profile(**profile)
        profile.status = ProfileStatus.INACTIVE
        mdm_group = MDMGroup(**mdm_group)

        cdatabase.db_session.add(profile)
        cdatabase.db_session.add(mdm_group)
        cdatabase.db_session.commit()

        res = client.put(url_for('api_app.mdmgroupresource_profile', id=mdm_group.id, profile_id=profile.id))

        assert self.assert_json(res.headers)
        assert self.assert_success(res)
        data = json.loads(res.data)
        assert data['message'] == "Success"

        res = client.delete(url_for('api_app.mdmgroupresource_profile', id=mdm_group.id, profile_id=profile.id))

        assert self.assert_json(res.headers)
        assert self.assert_success(res)
        data = json.loads(res.data)
        assert data['message'] == "Success"
    def test_switching_profile_membership_is_idempotent(
            self, client, profile, mdm_group):
        profile = Profile(**profile)
        mdm_group = MDMGroup(**mdm_group)

        cdatabase.db_session.add(profile)
        cdatabase.db_session.add(mdm_group)
        profile.mdm_groups.append(mdm_group)
        cdatabase.db_session.commit()

        res = client.put(
            url_for('api_app.mdmgroupresource_profile',
                    id=mdm_group.id,
                    profile_id=profile.id))

        assert self.assert_json(res.headers)
        assert self.assert_success(res)
        data = json.loads(res.data)

        assert data['message'] == "Already in group"
Example #13
0
    def test_finalize_removal(self, profile, device):
        profile = Profile(**profile)
        device = Device(**device)
        profile_service = service.ProfileService()

        service.db_session = mock.MagicMock()
        query = mock.MagicMock()
        service.db_session.query.return_value = query
        query.get.return_value = profile

        removeqc = object()
        service.RemoveProfile = mock.MagicMock()
        service.RemoveProfile.new_queued_command.return_value = removeqc

        service.db_session.add = mock.MagicMock()

        profile_service.finalize_removal(profile, device)

        assert service.RemoveProfile.new_queued_command.call_args[0] == (device, {'Identifier': profile.identifier, 'UUID': profile.uuid})
        service.db_session.add.assert_called_once_with(removeqc)