コード例 #1
0
	def test_authenticate_success(self):

		# initial assertions:
		assert User.objects.filter(id=1).count() == 0, 'Expect no users in local database'

		# setup:
		username = "******"
		password = "******"

		# expected responses

		# login to userservice:
		url = '{0}/api-token-auth/' . format(settings.USERSERVICE_BASE_URL)
		response_string = '{"token": "123"}'
		responses.add(responses.POST, url,
              body=response_string, status=200,
              content_type='application/json')

		# get current user:
		url = '{0}/api/v1/users/me/' . format(settings.USERSERVICE_BASE_URL)
		response_string = '{"id":1, "username":"******"}'
		responses.add(responses.GET, url,
              body=response_string, status=200,
              content_type='application/json')

		backend = UserServiceAuthBackend()
		user = backend.authenticate(username, password)

		# assert correct user is returned 
		assert user.id == 1
		assert user.username == 'joe.soap'

		# assert user is created
		assert User.objects.filter(id=1).count() == 1, 'Expect user to be created in local database'
コード例 #2
0
	def test_autentication_failure(self):

		username = "******"
		password = "******"

		# expected responses

		# login to userservice:
		url = '{0}/api-token-auth/' . format(settings.USERSERVICE_BASE_URL)
		response_string = '{"non_field_errors": ["Unable to login with provided credentials."]}'
		responses.add(responses.POST, url,
              body=response_string, status=400,
              content_type='application/json')

		backend = UserServiceAuthBackend()
		user = backend.authenticate(username, password)

		assert user is None, 'Expect authentication to fail'
		assert User.objects.filter(id=1).count() == 0, 'Expect no users in local database'