本文整理汇总了Python中student.tests.factories.UserFactory.save方法的典型用法代码示例。如果您正苦于以下问题:Python UserFactory.save方法的具体用法?Python UserFactory.save怎么用?Python UserFactory.save使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类student.tests.factories.UserFactory
的用法示例。
在下文中一共展示了UserFactory.save方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_create_permissions
# 需要导入模块: from student.tests.factories import UserFactory [as 别名]
# 或者: from student.tests.factories.UserFactory import save [as 别名]
def test_create_permissions(self):
""" Users should only be allowed to create data for themselves. """
url = reverse('api_experiments:v0:data-list')
# Authentication is required
response = self.client.post(url, {})
self.assertEqual(response.status_code, 401)
user = UserFactory()
data = {
'experiment_id': 1,
'key': 'foo',
'value': 'bar',
}
self.client.login(username=user.username, password=UserFactory._DEFAULT_PASSWORD)
# Users can create data for themselves
response = self.client.post(url, data)
self.assertEqual(response.status_code, 201)
ExperimentData.objects.get(user=user)
# A non-staff user cannot create data for another user
other_user = UserFactory()
data['user'] = other_user.username
response = self.client.post(url, data)
self.assertEqual(response.status_code, 403)
self.assertFalse(ExperimentData.objects.filter(user=other_user).exists())
# A staff user can create data for other users
user.is_staff = True
user.save()
response = self.client.post(url, data)
self.assertEqual(response.status_code, 201)
ExperimentData.objects.get(user=other_user)
示例2: test_authenticated_user
# 需要导入模块: from student.tests.factories import UserFactory [as 别名]
# 或者: from student.tests.factories.UserFactory import save [as 别名]
def test_authenticated_user(self):
req = self.make_successful_sneakpeek_login_request()
user = UserFactory()
user.save()
req.user = user
self.assertIsNone(self.middleware.process_request(req))
self.assertNoSneakPeek(req, self.open_course, check_auth=False)
示例3: IssueProgramCertificatesViewTests
# 需要导入模块: from student.tests.factories import UserFactory [as 别名]
# 或者: from student.tests.factories.UserFactory import save [as 别名]
class IssueProgramCertificatesViewTests(TestCase, ProgramsApiConfigMixin):
password = 'password'
def setUp(self):
super(IssueProgramCertificatesViewTests, self).setUp()
self.create_programs_config()
self.path = reverse('support:programs-certify')
self.user = UserFactory(password=self.password, is_staff=True)
self.data = {'username': self.user.username}
self.headers = {}
self.client.login(username=self.user.username, password=self.password)
def _verify_response(self, status_code):
"""Verify that the endpoint returns the provided status code and enqueues the task if appropriate."""
with mock.patch('lms.djangoapps.support.views.programs.award_program_certificates.delay') as mock_task:
response = self.client.post(self.path, self.data, **self.headers)
self.assertEqual(response.status_code, status_code)
self.assertEqual(status_code == 200, mock_task.called)
def test_authentication_required(self):
"""Verify that the endpoint requires authentication."""
self.client.logout()
self._verify_response(403)
def test_session_auth(self):
"""Verify that the endpoint supports session auth."""
self._verify_response(200)
def test_oauth(self):
"""Verify that the endpoint supports OAuth 2.0."""
access_token = AccessTokenFactory(user=self.user, client=ClientFactory()).token # pylint: disable=no-member
self.headers['HTTP_AUTHORIZATION'] = 'Bearer ' + access_token
self.client.logout()
self._verify_response(200)
def test_staff_permissions_required(self):
"""Verify that staff permissions are required to access the endpoint."""
self.user.is_staff = False
self.user.save() # pylint: disable=no-member
self._verify_response(403)
def test_certification_disabled(self):
"""Verify that the endpoint returns a 400 when program certification is disabled."""
self.create_programs_config(enable_certification=False)
self._verify_response(400)
def test_username_required(self):
"""Verify that the endpoint returns a 400 when a username isn't provided."""
self.data.pop('username')
self._verify_response(400)
示例4: EcommerceApiClientTest
# 需要导入模块: from student.tests.factories import UserFactory [as 别名]
# 或者: from student.tests.factories.UserFactory import save [as 别名]
class EcommerceApiClientTest(TestCase):
""" Tests to ensure the client is initialized properly. """
TEST_USER_EMAIL = '[email protected]'
TEST_CLIENT_ID = 'test-client-id'
def setUp(self):
super(EcommerceApiClientTest, self).setUp()
self.user = UserFactory()
self.user.email = self.TEST_USER_EMAIL
self.user.save() # pylint: disable=no-member
@httpretty.activate
def test_tracking_context(self):
"""
Ensure the tracking context is set up in the api client correctly and
automatically.
"""
# fake an ecommerce api request.
httpretty.register_uri(
httpretty.POST,
'{}/baskets/1/'.format(TEST_API_URL),
status=200, body='{}',
adding_headers={'Content-Type': 'application/json'}
)
mock_tracker = mock.Mock()
mock_tracker.resolve_context = mock.Mock(return_value={'client_id': self.TEST_CLIENT_ID})
with mock.patch('commerce.tracker.get_tracker', return_value=mock_tracker):
ecommerce_api_client(self.user).baskets(1).post()
# make sure the request's JWT token payload included correct tracking context values.
actual_header = httpretty.last_request().headers['Authorization']
expected_payload = {
'username': self.user.username,
'email': self.user.email,
'tracking_context': {
'lms_user_id': self.user.id, # pylint: disable=no-member
'lms_client_id': self.TEST_CLIENT_ID,
},
}
expected_header = 'JWT {}'.format(jwt.encode(expected_payload, TEST_API_SIGNING_KEY))
self.assertEqual(actual_header, expected_header)
@httpretty.activate
def test_client_unicode(self):
"""
The client should handle json responses properly when they contain
unicode character data.
Regression test for ECOM-1606.
"""
expected_content = '{"result": "Préparatoire"}'
httpretty.register_uri(
httpretty.GET,
'{}/baskets/1/order/'.format(TEST_API_URL),
status=200, body=expected_content,
adding_headers={'Content-Type': 'application/json'},
)
actual_object = ecommerce_api_client(self.user).baskets(1).order.get()
self.assertEqual(actual_object, {u"result": u"Préparatoire"})
示例5: user_is_embargoed
# 需要导入模块: from student.tests.factories import UserFactory [as 别名]
# 或者: from student.tests.factories.UserFactory import save [as 别名]
def user_is_embargoed(self, user, is_embargoed):
"""
Set a users emabargo state.
"""
user_profile = UserFactory(username=user.username, email=user.email).profile
user_profile.allow_certificate = not is_embargoed
user_profile.save()
示例6: test_transfer_students
# 需要导入模块: from student.tests.factories import UserFactory [as 别名]
# 或者: from student.tests.factories.UserFactory import save [as 别名]
def test_transfer_students(self):
student = UserFactory()
student.set_password(self.PASSWORD) # pylint: disable=E1101
student.save() # pylint: disable=E1101
# Original Course
original_course_location = locator.CourseLocator('Org0', 'Course0', 'Run0')
course = self._create_course(original_course_location)
# Enroll the student in 'verified'
CourseEnrollment.enroll(student, course.id, mode="verified")
# New Course 1
course_location_one = locator.CourseLocator('Org1', 'Course1', 'Run1')
new_course_one = self._create_course(course_location_one)
# New Course 2
course_location_two = locator.CourseLocator('Org2', 'Course2', 'Run2')
new_course_two = self._create_course(course_location_two)
original_key = unicode(course.id)
new_key_one = unicode(new_course_one.id)
new_key_two = unicode(new_course_two.id)
# Run the actual management command
transfer_students.Command().handle(
source_course=original_key, dest_course_list=new_key_one + "," + new_key_two
)
# Confirm the enrollment mode is verified on the new courses, and enrollment is enabled as appropriate.
self.assertEquals(('verified', False), CourseEnrollment.enrollment_mode_for_user(student, course.id))
self.assertEquals(('verified', True), CourseEnrollment.enrollment_mode_for_user(student, new_course_one.id))
self.assertEquals(('verified', True), CourseEnrollment.enrollment_mode_for_user(student, new_course_two.id))
示例7: test_is_email_retired_is_retired
# 需要导入模块: from student.tests.factories import UserFactory [as 别名]
# 或者: from student.tests.factories.UserFactory import save [as 别名]
def test_is_email_retired_is_retired():
"""
Check functionality of is_email_retired when email is retired
"""
user = UserFactory()
original_email = user.email
retired_email = get_retired_email_by_email(user.email)
# Fake email retirement.
user.email = retired_email
user.save()
assert is_email_retired(original_email)
示例8: test_is_username_retired_is_retired
# 需要导入模块: from student.tests.factories import UserFactory [as 别名]
# 或者: from student.tests.factories.UserFactory import save [as 别名]
def test_is_username_retired_is_retired():
"""
Check functionality of is_username_retired when username is retired
"""
user = UserFactory()
original_username = user.username
retired_username = get_retired_username_by_username(user.username)
# Fake username retirement.
user.username = retired_username
user.save()
assert is_username_retired(original_username)
示例9: test_session_auth
# 需要导入模块: from student.tests.factories import UserFactory [as 别名]
# 或者: from student.tests.factories.UserFactory import save [as 别名]
def test_session_auth(self):
""" Verify the endpoint supports session authentication, and only allows authorization for staff users. """
user = UserFactory(password=self.password, is_staff=False)
self.client.login(username=user.username, password=self.password)
# Non-staff users should not have access to the API
response = self.client.get(self.path)
self.assertEqual(response.status_code, 403)
# Staff users should have access to the API
user.is_staff = True
user.save()
response = self.client.get(self.path)
self.assertEqual(response.status_code, 200)
示例10: test_retired_username
# 需要导入模块: from student.tests.factories import UserFactory [as 别名]
# 或者: from student.tests.factories.UserFactory import save [as 别名]
def test_retired_username(self):
"""
Ensure that a retired username cannot be registered again.
"""
user = UserFactory()
orig_username = user.username
# Fake retirement of the username.
user.username = get_retired_username_by_username(orig_username)
user.save()
# Attempt to create another account with the same username that's been retired.
self.url_params['username'] = orig_username
response = self.client.post(self.url, self.url_params)
self._validate_exiting_username_response(orig_username, response, self.INVALID_ERR_MSG[0], self.INVALID_ERR_MSG[1])
示例11: test_oauth
# 需要导入模块: from student.tests.factories import UserFactory [as 别名]
# 或者: from student.tests.factories.UserFactory import save [as 别名]
def test_oauth(self):
""" Verify the endpoint supports OAuth, and only allows authorization for staff users. """
user = UserFactory(is_staff=False)
oauth_client = ClientFactory.create()
access_token = AccessTokenFactory.create(user=user, client=oauth_client).token
headers = {"HTTP_AUTHORIZATION": "Bearer " + access_token}
# Non-staff users should not have access to the API
response = self.client.get(self.path, **headers)
self.assertEqual(response.status_code, 403)
# Staff users should have access to the API
user.is_staff = True
user.save() # pylint: disable=no-member
response = self.client.get(self.path, **headers)
self.assertEqual(response.status_code, 200)
示例12: test_get_potentially_retired_user_hashed_match
# 需要导入模块: from student.tests.factories import UserFactory [as 别名]
# 或者: from student.tests.factories.UserFactory import save [as 别名]
def test_get_potentially_retired_user_hashed_match():
"""
Check that we can pass in a hashed username and get the
user-to-be-retired back.
"""
user = UserFactory()
orig_username = user.username
hashed_username = get_retired_username_by_username(orig_username)
# Fake username retirement.
user.username = hashed_username
user.save()
# Check to find the user by original username should fail,
# 2nd check by hashed username should succeed.
assert get_potentially_retired_user_by_username_and_hash(orig_username, hashed_username) == user
示例13: test_provider_login_can_handle_unicode_email_inactive_account
# 需要导入模块: from student.tests.factories import UserFactory [as 别名]
# 或者: from student.tests.factories.UserFactory import save [as 别名]
def test_provider_login_can_handle_unicode_email_inactive_account(self):
user = UserFactory(email=u"user.ąęł@gmail.com", username=u"ąęół")
url = reverse("openid-provider-login")
# login to the client so that we can persist session information
user.profile.name = u"Jan ĄĘ"
user.profile.save()
self.client.login(username=user.username, password="test")
# login once to get the right session information
self.attempt_login(200)
# We trigger situation where user is not active at final phase of
# OpenId login.
user.is_active = False
user.save()
post_args = {"email": user.email, "password": "test"}
# call url again, this time with username and password
self.client.post(url, post_args)
示例14: test_payment_accepted_order
# 需要导入模块: from student.tests.factories import UserFactory [as 别名]
# 或者: from student.tests.factories.UserFactory import save [as 别名]
def test_payment_accepted_order(self):
"""
Tests payment_accepted cases with an order
"""
student1 = UserFactory()
student1.save()
order1 = Order.get_cart_for_user(student1)
params = {
'card_accountNumber': '1234',
'card_cardType': '001',
'billTo_firstName': student1.first_name,
'billTo_lastName': u"\u2603",
'orderNumber': str(order1.id),
'orderCurrency': 'usd',
'decision': 'ACCEPT',
'ccAuthReply_amount': '0.00'
}
# tests for an order number that doesn't match up
params_bad_ordernum = params.copy()
params_bad_ordernum['orderNumber'] = str(order1.id + 10)
with self.assertRaises(CCProcessorDataException):
payment_accepted(params_bad_ordernum)
# tests for a reply amount of the wrong type
params_wrong_type_amt = params.copy()
params_wrong_type_amt['ccAuthReply_amount'] = 'ab'
with self.assertRaises(CCProcessorDataException):
payment_accepted(params_wrong_type_amt)
# tests for a reply amount of the wrong type
params_wrong_amt = params.copy()
params_wrong_amt['ccAuthReply_amount'] = '1.00'
with self.assertRaises(CCProcessorWrongAmountException):
payment_accepted(params_wrong_amt)
# tests for a not accepted order
params_not_accepted = params.copy()
params_not_accepted['decision'] = "REJECT"
self.assertFalse(payment_accepted(params_not_accepted)['accepted'])
# finally, tests an accepted order
self.assertTrue(payment_accepted(params)['accepted'])
示例15: test_render_purchase_form_html
# 需要导入模块: from student.tests.factories import UserFactory [as 别名]
# 或者: from student.tests.factories.UserFactory import save [as 别名]
def test_render_purchase_form_html(self, render):
"""
Tests the rendering of the purchase form
"""
student1 = UserFactory()
student1.save()
order1 = Order.get_cart_for_user(student1)
item1 = OrderItem(order=order1, user=student1, unit_cost=1.0, line_cost=1.0)
item1.save()
render_purchase_form_html(order1)
((template, context), render_kwargs) = render.call_args
self.assertEqual(template, 'shoppingcart/cybersource_form.html')
self.assertDictContainsSubset({'amount': '1.00',
'currency': 'usd',
'orderPage_transactionType': 'sale',
'orderNumber': str(order1.id)},
context['params'])