'Given an existing authenticated user, and an updated identity (e.g.
the ident changed from the SSO provider), we should be re-linking
the identity automatically (without prompt) assuming the user is
a member of the org.'

def test_flow_managed_duplicate_users_with_membership(self):

    organization = self.create_organization(name='foo', owner=self.user)
    auth_provider = AuthProvider.objects.create(organization=organization, provider='dummy')
    user = self.create_user('bar@example.com', is_active=False, is_managed=True)
    auth_identity = AuthIdentity.objects.create(auth_provider=auth_provider, user=user, ident='bar@example.com')
    self.create_member(organization=organization, user=user)
    self.login_as(user)
    path = reverse('sentry-auth-organization', args=[organization.slug])
    resp = self.client.post(path)
    assert (resp.status_code == 200)
    assert (self.provider.TEMPLATE in resp.content.decode('utf-8'))
    path = reverse('sentry-auth-sso')
    resp = self.client.post(path, {'email': 'adfadsf@example.com'})
    assert (resp.status_code == 302)
    assert (resp['Location'] == ('http://testserver' + reverse('sentry-login')))
    auth_identity = AuthIdentity.objects.get(id=auth_identity.id)
    assert (auth_identity.ident == 'adfadsf@example.com')
    new_user = auth_identity.user
    assert (new_user == user)
    member = OrganizationMember.objects.get(organization=organization, user=new_user)
    assert getattr(member.flags, 'sso:linked')
    assert (not getattr(member.flags, 'sso:invalid'))
