From 9eb1cce352ba6aea30451ccf3d00ba66318757ed Mon Sep 17 00:00:00 2001 From: ihoover Date: Tue, 16 Jul 2013 14:05:41 -0400 Subject: [PATCH] maximum number of random users --- common/djangoapps/student/views.py | 25 +++++++++++++++++++++++++ lms/djangoapps/branding/views.py | 28 +++++++++++++++++++++++----- lms/envs/common.py | 5 +++-- 3 files changed, 51 insertions(+), 7 deletions(-) diff --git a/common/djangoapps/student/views.py b/common/djangoapps/student/views.py index 398a3f6efc..129d9d4a19 100644 --- a/common/djangoapps/student/views.py +++ b/common/djangoapps/student/views.py @@ -918,6 +918,21 @@ def get_random_post_override(): 'honor_code': u'true', 'terms_of_service': u'true', } +def get_planned_post_override(username, password): + """ + Return a dictionary suitable for passing to post_vars of _do_create_account or post_override + of create_account, with specified username and password. + """ + def id_generator(size=6, chars=string.ascii_uppercase + string.ascii_lowercase + string.digits): + return ''.join(random.choice(chars) for x in range(size)) + + return {'username': username, + 'email': id_generator(size=10, chars=string.ascii_lowercase) + "_dummy_test@mitx.mit.edu", + 'password': password, + 'name': (id_generator(size=5, chars=string.ascii_lowercase) + " " + + id_generator(size=7, chars=string.ascii_lowercase)), + 'honor_code': u'true', + 'terms_of_service': u'true', } def create_random_account(create_account_function): def inner_create_random_account(request): @@ -929,6 +944,16 @@ def create_random_account(create_account_function): if settings.GENERATE_RANDOM_USER_CREDENTIALS: create_account = create_random_account(create_account) +def create_random_account_with_name_and_password(create_account_function): + def inner_create_random_account(request, username, password): + return create_account_function(request, post_override=get_planned_post_override(username, password)) + + return inner_create_random_account + +# for load testing we want to create lots of accounts +# with controlled username and password +if settings.AUTOMATIC_AUTH_FOR_LOAD_TESTING: + create_account = create_random_account_with_name_and_password(create_account) @ensure_csrf_cookie def activate_account(request, key): diff --git a/lms/djangoapps/branding/views.py b/lms/djangoapps/branding/views.py index c070c2ade9..f30eec1554 100644 --- a/lms/djangoapps/branding/views.py +++ b/lms/djangoapps/branding/views.py @@ -58,12 +58,30 @@ def auto_auth(request): true. """ - # log the user in - student.views.create_account(request) + from django.contrib.auth.models import User + from django.contrib.auth import login, authenticate + from random import randint - # activate account - request.user.is_active = True - request.user.save() + # generate random user ceredentials from a small name space + name_base = 'USER_' + pass_base = 'PASS_' + + number = randint(1, settings.MAX_AUTO_AUTH_USERS) + + username = name_base + str(number) + password = pass_base + str(number) + + # if they already are a user, log in + try: + user = User.objects.get(username=username) + user = authenticate(username=username, password=password) + login(request, user) + + except: + # create and activate account info + student.views.create_account(request, username, password) + request.user.is_active = True + request.user.save() # redirect to home-page return redirect('root') diff --git a/lms/envs/common.py b/lms/envs/common.py index ab1b73a88f..57604ba1ff 100644 --- a/lms/envs/common.py +++ b/lms/envs/common.py @@ -38,12 +38,13 @@ COURSEWARE_ENABLED = True ENABLE_JASMINE = False AUTOMATIC_AUTH_FOR_LOAD_TESTING = True +MAX_AUTO_AUTH_USERS = 2 GENERATE_RANDOM_USER_CREDENTIALS = False PERFSTATS = False -# automatic_auth should turn on random_cred of it needs to -GENERATE_RANDOM_USER_CREDENTIALS = GENERATE_RANDOM_USER_CREDENTIALS or AUTOMATIC_AUTH_FOR_LOAD_TESTING +# # automatic_auth should turn on random_cred of it needs to +# GENERATE_RANDOM_USER_CREDENTIALS = GENERATE_RANDOM_USER_CREDENTIALS or AUTOMATIC_AUTH_FOR_LOAD_TESTING DISCUSSION_SETTINGS = { 'MAX_COMMENT_DEPTH': 2,