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_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_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"
예제 #4
0
    def test_deploy_works_only_if_inactive(self, client, profile, app):
        profile = Profile(**profile)

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

        redis_client = app.redis_store._redis_client

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

            if status == ProfileStatus.INACTIVE:
                assert self.assert_success(res)
                assert profile.status == ProfileStatus.PENDING_INSTALLATION
                assert get_queued_jobs(redis_client) == ['commandment.tasks.process_profile_deployment_change(%d)' % profile.id]
            elif status == ProfileStatus.ACTIVE:
                assert self.assert_success(res)
                assert profile.status == ProfileStatus.ACTIVE
                assert get_queued_jobs(redis_client) == []
            else:
                assert res.status_code == 400
                assert profile.status == status
                assert get_queued_jobs(redis_client) == []
    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)
예제 #6
0
def test_put_existing(client):
    """Can we PUT a resource at a specific ID?"""
    response = client.put('/artist/1',
                          data=json.dumps(REPLACED_ARTIST),
                          headers={'Content-type': 'application/json'})

    assert response.status_code == 200
    assert json.loads(response.get_data(as_text=True)) == REPLACED_ARTIST
예제 #7
0
    def test_delete(self, client, device):
        res = client.put(url_for('api_app.deviceresource'), data=device)
        data = json.loads(res.data)

        res = client.delete(url_for('api_app.deviceresource', id=data['id']))

        res = client.get(url_for('api_app.deviceresource', id=data['id']))

        assert res.status_code == 404
    def test_delete(self, client, mdm_group):
        res = client.put(url_for('api_app.mdmgroupresource'), data=mdm_group)
        data = json.loads(res.data)

        res = client.delete(url_for('api_app.mdmgroupresource', id=data['id']))

        res = client.get(url_for('api_app.mdmgroupresource', id=data['id']))

        assert res.status_code == 404
예제 #9
0
def test_put_existing(client):
    """Can we PUT a resource at a specific ID?"""
    response = client.put(
        '/artist/1',
        data=json.dumps(REPLACED_ARTIST),
        headers={'Content-type': 'application/json'}
        )

    assert response.status_code == 200
    assert json.loads(response.get_data(as_text=True)) == REPLACED_ARTIST
예제 #10
0
    def test_put(self, client, device):
        res = client.put(url_for('api_app.deviceresource'), data=device)
        assert self.assert_json(res.headers)
        assert self.assert_success(res)

        data = json.loads(res.data)

        assert 'id' in data
        assert isinstance(data['id'], int)
        assert data['id'] > 0
예제 #11
0
def test_put_new(client):
    """Can we PUT a resource at *new* ID?"""
    response = client.get('/artist/277')
    assert response.status_code == 404
    response = client.put('/artist/277',
                          data=json.dumps(NEW_ARTIST),
                          headers={'Content-type': 'application/json'})

    assert response.status_code == 201
    assert json.loads(response.get_data(as_text=True)) == NEW_ARTIST
예제 #12
0
def test_validate_put_new(client):
    """Do we get back an error message when making a PUT request for a
    totally new resource?"""
    response = client.put('/user/2',
                          data=json.dumps({
                              'name': 'Elissa Knupp',
                              'email': '*****@*****.**',
                          }),
                          headers={'Content-Type': 'application/json'})
    assert response.status_code == 400
    assert response.json['message'] == INVALID_ACTION_MESSAGE
예제 #13
0
def test_validate_put_existing(client):
    """Do we get back an error message when making a PUT request for
    an exisitng resource?"""
    response = client.put('/user/1',
                          data=json.dumps({
                              'name': 'Jeff Knupp',
                              'email': '*****@*****.**',
                          }),
                          headers={'Content-Type': 'application/json'})
    assert response.status_code == 400
    assert response.json['message'] == INVALID_ACTION_MESSAGE
예제 #14
0
def test_put_new(client):
    """Can we PUT a resource at *new* ID?"""
    response = client.get('/artist/276')
    assert response.status_code == 404
    response = client.put(
        '/artist/276',
        data=json.dumps(NEW_ARTIST),
        headers={'Content-type': 'application/json'}
        )

    assert response.status_code == 201
    assert json.loads(response.get_data(as_text=True)) == NEW_ARTIST
예제 #15
0
def test_validate_put_new(client):
    """Do we get back an error message when making a PUT request for a
    totally new resource?"""
    response = client.put(
        '/user/2',
        data=json.dumps({
            'name': 'Elissa Knupp',
            'email': '*****@*****.**',
            }),
        headers={'Content-Type': 'application/json'}
    )
    assert response.status_code == 400
    assert response.json['message'] == INVALID_ACTION_MESSAGE
예제 #16
0
def test_validate_put_existing(client):
    """Do we get back an error message when making a PUT request for
    an exisitng resource?"""
    response = client.put(
        '/user/1',
        data=json.dumps({
            'name': 'Jeff Knupp',
            'email': '*****@*****.**',
            }),
        headers={'Content-Type': 'application/json'}
    )
    assert response.status_code == 400
    assert response.json['message'] == INVALID_ACTION_MESSAGE
    def test_get(self, client, mdm_group):
        res = client.put(url_for('api_app.mdmgroupresource'), data=mdm_group)
        data = json.loads(res.data)

        res = client.get(url_for('api_app.mdmgroupresource', id=data['id']))

        data = json.loads(res.data)

        assert 'id' in data
        assert isinstance(data['id'], int)
        assert data['id'] > 0

        mdm_group['id'] = data['id']
        assert mdm_group == data
예제 #18
0
    def test_get(self, client, device):
        res = client.put(url_for('api_app.deviceresource'), data=device)
        data = json.loads(res.data)

        res = client.get(url_for('api_app.deviceresource', id=data['id']))

        data = json.loads(res.data)

        assert 'id' in data
        assert isinstance(data['id'], int)
        assert data['id'] > 0

        device['id'] = data['id']
        assert device == data
예제 #19
0
    def test_get(self, client, profile):
        res = client.put(url_for('api_app.profileresource'), data=profile)
        data = json.loads(res.data)

        res = client.get(url_for('api_app.profileresource', id=data['id']))

        data = json.loads(res.data)

        assert 'id' in data
        assert isinstance(data['id'], int)
        assert data['id'] > 0

        assert data['identifier'] == profile['identifier']
        assert data['uuid'] == profile['uuid']
        assert data['status'] == profile['status'].value
    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"
    def test_post(self, client, mdm_group):
        res = client.put(url_for('api_app.mdmgroupresource'), data=mdm_group)
        data = json.loads(res.data)

        mdm_group['group_name'] = 'something else'
        res = client.post(url_for('api_app.mdmgroupresource', id=data['id']), data=mdm_group)

        assert self.assert_success(res)

        res = client.get(url_for('api_app.mdmgroupresource', id=data['id']))

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

        mdm_group['id'] = data['id']
        assert data == mdm_group
예제 #22
0
    def test_post(self, client, profile):
        res = client.put(url_for('api_app.profileresource'), data=profile)
        data = json.loads(res.data)

        profile['profile_data'] = 'something else'
        res = client.post(url_for('api_app.profileresource', id=data['id']), data=profile)

        assert self.assert_success(res)

        res = client.get(url_for('api_app.profileresource', id=data['id']))

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

        assert data['identifier'] == profile['identifier']
        assert data['uuid'] == profile['uuid']
        assert data['status'] == profile['status'].value
예제 #23
0
    def test_post(self, client, device):
        res = client.put(url_for('api_app.deviceresource'), data=device)
        data = json.loads(res.data)

        device['udid'] = str(uuid.uuid4())
        device['serial_number'] = '12131415'
        res = client.post(url_for('api_app.deviceresource', id=data['id']), data=device)

        assert self.assert_success(res)

        res = client.get(url_for('api_app.deviceresource', id=data['id']))

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

        device['id'] = data['id']
        assert data == device
    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"
    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"