'Test that we can update an OAuth provider\'s icon image from the admin
form.
OAuth providers are updated using KeyedConfigurationModelAdmin, which
updates models by adding a new instance that replaces the old one,
instead of editing the old instance directly.
Updating the icon image is tricky here because
KeyedConfigurationModelAdmin copies data over from the previous
version by injecting its attributes into request.GET, but the icon
ends up in request.FILES. We need to ensure that the value is
prepopulated correctly, and that we can clear and update the image.'

def test_oauth2_provider_edit_icon_image(self):

    user = UserFactory.create(is_staff=True, is_superuser=True)
    user.save()
    self.client.login(username=user.username, password='test')
    providers = OAuth2ProviderConfig.objects.all()
    pcount = len(providers)
    provider1 = self.configure_dummy_provider(enabled=True, icon_class='', icon_image=SimpleUploadedFile('icon.svg', '<svg><rect    width="50"    height="100"/></svg>'))
    providers = OAuth2ProviderConfig.objects.all()
    self.assertEquals(len(providers), 1)
    self.assertEquals(providers[pcount].id, provider1.id)
    admin = OAuth2ProviderConfigAdmin(provider1, AdminSite())
    update_url = reverse('admin:{}_{}_add'.format(admin.model._meta.app_label, admin.model._meta.model_name))
    update_url += '?source={}'.format(provider1.pk)
    post_data = models.model_to_dict(provider1)
    del post_data['icon_image']
    del post_data['max_session_length']
    post_data['name'] = 'Another    name'
    response = self.client.post(update_url, post_data)
    self.assertEquals(response.status_code, 302)
    providers = OAuth2ProviderConfig.objects.all()
    self.assertEquals(len(providers), (pcount + 2))
    self.assertEquals(providers[pcount].id, provider1.id)
    provider2 = providers[(pcount + 1)]
    self.assertEquals(provider2.icon_image, provider1.icon_image)
    self.assertEquals(provider2.name, post_data['name'])
