def test_create_local_identity_removes_user_from_old_accounts(self): # Obter dados do usuário with identity_client.tests.use_cassette('fetch_identity_data/success'): response = APIClient.fetch_identity_data(uuid=test_user_uuid) status_code, content, error = response # Criar usuário local identity = MyfcidAPIBackend().create_local_identity(content) # 5 contas devem ter sido criadas serviceAccountModel = get_account_module() accounts = serviceAccountModel.for_identity(identity) self.assertEqual(accounts.count(), 5) # Obter os dados do usuário, desta vez sem accounts with identity_client.tests.use_cassette('fetch_identity_data/success'): response = APIClient.fetch_identity_data(uuid=test_user_uuid) status_code, content, error = response # Criar usuário local identity = MyfcidAPIBackend().create_local_identity(content) # O usuário deve ter sido dissociado da account serviceAccountModel = get_account_module() accounts = serviceAccountModel.for_identity(identity) self.assertEqual(accounts.count(), 5)
def test_fetch_user_data_with_success(self): with identity_client.tests.use_cassette('myfcid_api_backend/success'): _, user_data, _ = MyfcidAPIBackend().fetch_user_data( test_user_email, test_user_password) self.assertEqual( user_data, { u'authentication_key': u'$2a$12$nA3ad2y5aSBlg80K9ekbNuvnRO1OI1WUKZyoJqWEhk.PQpD8.6jkS', u'email': u'*****@*****.**', u'first_name': u'Identity', u'id_token': u'729dd3a15cf03a80024d0986deee9ae91fdd5d834fabf6f9', u'is_active': True, u'last_name': u'Client', u'notifications': { u'count': 0, u'list': u'/notifications/api/' }, u'profile_url': u'/accounts/api/identities/c3769912-baa9-4a0c-9856-395a706c7d57/profile/', u'send_myfreecomm_news': True, u'send_partner_news': True, u'update_info_url': u'/accounts/api/identities/c3769912-baa9-4a0c-9856-395a706c7d57/', u'uuid': u'c3769912-baa9-4a0c-9856-395a706c7d57' })
def test_failed_auth(self): # Autenticar um usuário with identity_client.tests.use_cassette( 'myfcid_api_backend/wrong_password'): identity = MyfcidAPIBackend().authenticate(test_user_email, 'senha errada') # Garantir que o usuario não foi autenticado self.assertEqual(identity, None)
def test_fetch_user_data_failure(self): with identity_client.tests.use_cassette( 'myfcid_api_backend/wrong_password'): _, user_data, error = MyfcidAPIBackend().fetch_user_data( test_user_email, 'senha errada') self.assertEqual(user_data, None) self.assertEqual( error, { 'status': 401, 'message': u'{"detail": "You need to login or otherwise authenticate the request."}' })
def test_authentication_should_not_remove_user_accounts(self): # Obter dados do usuário with identity_client.tests.use_cassette('fetch_identity_data/success'): response = APIClient.fetch_identity_data(uuid=test_user_uuid) status_code, content, error = response # Criar usuário local identity = MyfcidAPIBackend().create_local_identity(content) # 1 conta deve ter sido criada serviceAccountModel = get_account_module() accounts = serviceAccountModel.for_identity(identity) self.assertEqual(accounts.count(), 5) # Autenticar o usuário with identity_client.tests.use_cassette('myfcid_api_backend/success'): identity = MyfcidAPIBackend().authenticate(test_user_email, test_user_password) # A conta deve continuar existindo serviceAccountModel = get_account_module() accounts = serviceAccountModel.for_identity(identity) self.assertEqual(accounts.count(), 5)
def test_auth_updates_user(self): # Create a user user = Identity.objects.create(uuid=test_user_uuid, email='*****@*****.**') # Autenticar um usuário with identity_client.tests.use_cassette('myfcid_api_backend/success'): identity = MyfcidAPIBackend().authenticate(test_user_email, test_user_password) # Checar se os dados do usuário foram atualizados self.assertEqual(identity.first_name, 'Identity') self.assertEqual(identity.last_name, 'Client') self.assertEqual(identity.email, test_user_email) self.assertEqual(identity.uuid, test_user_uuid)
def test_auth_user_accounts_creation_fails_if_settings_are_missing(self): # Obter dados do usuário with identity_client.tests.use_cassette('fetch_identity_data/success'): response = APIClient.fetch_identity_data(uuid=test_user_uuid) status_code, content, error = response # Criar usuário local with patch.object(settings, 'SERVICE_ACCOUNT_MODULE', None): identity = MyfcidAPIBackend().create_local_identity(content) self.assertTrue(identity is not None) # Nenhuma conta foi criada serviceAccountModel = get_account_module() accounts = serviceAccountModel.for_identity(identity) self.assertEqual(accounts.count(), 0)
def register(request, template_name='registration_form.html', redirect_field_name=REDIRECT_FIELD_NAME, registration_form=RegistrationForm, **kwargs): form = registration_form(data=request.POST) if form.is_valid(): # Registro status, content, form = APIClient.invoke_registration_api(form) if status in (200, 201): user = MyfcidAPIBackend().create_local_identity(content) login_user(request, user) return redirect_logged_user(request, redirect_field_name) return handle_redirect_to(request, template_name, redirect_field_name, form, **kwargs)
def test_successful_auth(self): # Autenticar um usuário with identity_client.tests.use_cassette('myfcid_api_backend/success'): identity = MyfcidAPIBackend().authenticate(test_user_email, test_user_password) # Checar se o usuário foi autenticado corretamente self.assertNotEqual(identity, None) self.assertEqual(identity.first_name, 'Identity') self.assertEqual(identity.last_name, 'Client') self.assertEqual(identity.email, test_user_email) self.assertEqual( identity.user_data, { u'authentication_key': u'$2a$12$nA3ad2y5aSBlg80K9ekbNuvnRO1OI1WUKZyoJqWEhk.PQpD8.6jkS', u'email': u'*****@*****.**', u'first_name': u'Identity', u'id_token': u'729dd3a15cf03a80024d0986deee9ae91fdd5d834fabf6f9', u'is_active': True, u'last_name': u'Client', u'notifications': { u'count': 0, u'list': u'/notifications/api/' }, u'profile_url': u'/accounts/api/identities/c3769912-baa9-4a0c-9856-395a706c7d57/profile/', u'send_myfreecomm_news': True, u'send_partner_news': True, u'update_info_url': u'/accounts/api/identities/c3769912-baa9-4a0c-9856-395a706c7d57/', u'uuid': u'c3769912-baa9-4a0c-9856-395a706c7d57' }) # Checar se o backend foi setado corretamente self.assertEqual( identity.backend, '%s.%s' % (MyfcidAPIBackend.__module__, 'MyfcidAPIBackend'))
def _create_user(self): # Autenticar um usuário with identity_client.tests.use_cassette('myfcid_api_backend/success'): return MyfcidAPIBackend().authenticate(test_user_email, test_user_password)
def as_local_identity(data): if ('uuid' in data) and ('email' in data): return MyfcidAPIBackend().create_local_identity(data) return data