예제 #1
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
    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_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_get_by_name_returns_empty_if_missing(self, client, mdm_group):
        mdm_group = MDMGroup(**mdm_group)

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

        res = client.get(url_for('api_app.mdmgroupresource'), data={'group_name': 'carrots'})

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

        assert len(data) == 0
    def test_get_by_name_retrieves_group_if_found(self, client, mdm_group):
        mdm_group = MDMGroup(**mdm_group)

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

        res = client.get(url_for('api_app.mdmgroupresource'), data={'group_name': mdm_group.group_name})

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

        assert len(data) == 1
        assert data[0]['id'] == mdm_group.id
    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"