def test_update_service_organisation_deletes_cache(mocker, fake_uuid):
    mock_redis_delete = mocker.patch('app.extensions.RedisClient.delete')
    mock_post = mocker.patch(
        'app.notify_client.organisations_api_client.OrganisationsClient.post')

    organisations_client.update_service_organisation(service_id=fake_uuid,
                                                     org_id=fake_uuid)

    assert sorted(mock_redis_delete.call_args_list) == [
        call('live-service-and-organisation-counts'),
        call('organisations'),
        call('service-{}'.format(fake_uuid)),
    ]
    mock_post.assert_called_with(
        url='/organisations/{}/service'.format(fake_uuid), data=ANY)
def test_update_service_organisation(mocker, fake_uuid):
    org_id, service_id = fake_uuid, fake_uuid

    mock_redis_delete = mocker.patch("app.extensions.RedisClient.delete")
    mock_post = mocker.patch(
        "app.notify_client.organisations_api_client.OrganisationsClient.post")

    organisations_client.update_service_organisation(
        service_id,
        org_id,
    )

    mock_post.assert_called_with(
        url="/organisations/{}/service".format(org_id),
        data={"service_id": service_id})
    assert mock_redis_delete.call_args_list == [
        call("organisations"),
        call("live-service-and-organisation-counts"),
        call("service-{}".format(service_id)),
    ]
def link_service_to_organisation(service_id):

    all_organisations = organisations_client.get_organisations()

    form = LinkOrganisationsForm(
        choices=convert_dictionary_to_wtforms_choices_format(
            all_organisations, 'id', 'name'),
        organisations=current_service.organisation_id)

    if form.validate_on_submit():
        if form.organisations.data != current_service.organisation_id:
            organisations_client.update_service_organisation(
                service_id, form.organisations.data)
        return redirect(url_for('.service_settings', service_id=service_id))

    return render_template(
        'views/service-settings/link-service-to-organisation.html',
        has_organisations=all_organisations,
        form=form,
        search_form=SearchByNameForm(),
    )