From f69778b36731cb8e45af3460c8956e9eeeec2a4f Mon Sep 17 00:00:00 2001 From: Will Daly Date: Wed, 20 Mar 2013 15:11:07 -0400 Subject: [PATCH] Changed lettuce test log_in step to log in programatically rather than using the login dialog. --- common/djangoapps/terrain/steps.py | 58 ++++++++++++------- lms/djangoapps/courseware/features/common.py | 5 +- .../features/high-level-tabs.feature | 2 +- 3 files changed, 40 insertions(+), 25 deletions(-) diff --git a/common/djangoapps/terrain/steps.py b/common/djangoapps/terrain/steps.py index 371496f823..8b991561c3 100644 --- a/common/djangoapps/terrain/steps.py +++ b/common/djangoapps/terrain/steps.py @@ -1,7 +1,12 @@ from lettuce import world, step from .factories import * from lettuce.django import django_url +from django.conf import settings +from django.http import HttpRequest from django.contrib.auth.models import User +from django.contrib.auth import authenticate, login +from django.contrib.auth.middleware import AuthenticationMiddleware +from django.contrib.sessions.middleware import SessionMiddleware from student.models import CourseEnrollment from urllib import quote_plus from nose.tools import assert_equals @@ -78,7 +83,7 @@ def the_page_title_should_contain(step, title): @step('I am a logged in user$') def i_am_logged_in_user(step): create_user('robot') - log_in('robot@edx.org', 'test') + log_in('robot', 'test') @step('I am not logged in$') @@ -93,7 +98,7 @@ def i_am_staff_for_course_by_id(step, course_id): @step('I log in$') def i_log_in(step): - log_in('robot@edx.org', 'test') + log_in('robot', 'test') @step(u'I am an edX user$') @@ -128,30 +133,38 @@ def create_user(uname): @world.absorb -def log_in(email, password): - world.browser.cookies.delete() - world.browser.visit(django_url('/')) - world.browser.is_element_present_by_css('header.global', 10) - world.browser.click_link_by_href('#login-modal') +def log_in(username, password): + ''' + Log the user in programatically + ''' - # Wait for the login dialog to load - # This is complicated by the fact that sometimes a second #login_form - # dialog loads, while the first one remains hidden. - # We give them both time to load, starting with the second one. - world.browser.is_element_present_by_css('section.content-wrapper form#login_form', wait_time=4) - world.browser.is_element_present_by_css('form#login_form', wait_time=2) + # Authenticate the user + user = authenticate(username=username, password=password) + assert(user is not None and user.is_active) - # For some reason, the page sometimes includes two #login_form - # elements, the first of which is not visible. - # To avoid this, we always select the last of the two #login_form dialogs - login_form = world.browser.find_by_css('form#login_form').last + # Send a fake HttpRequest to log the user in + # We need to process the request using + # Session middleware and Authentication middleware + # to ensure that session state can be stored + request = HttpRequest() + SessionMiddleware().process_request(request) + AuthenticationMiddleware().process_request(request) + login(request, user) - login_form.find_by_name('email').fill(email) - login_form.find_by_name('password').fill(password) - login_form.find_by_name('submit').click() + # Save the session + request.session.save() - # wait for the page to redraw - assert world.browser.is_element_present_by_css('.content-wrapper', wait_time=10) + # Retrieve the sessionid and add it to the browser's cookies + cookie_dict = {settings.SESSION_COOKIE_NAME: request.session.session_key} + try: + world.browser.cookies.add(cookie_dict) + + # WebDriver has an issue where we cannot set cookies + # before we make a GET request, so if we get an error, + # we load the '/' page and try again + except: + world.browser.visit(django_url('/')) + world.browser.cookies.add(cookie_dict) @world.absorb @@ -208,6 +221,7 @@ def save_the_course_content(path='/tmp'): u = world.browser.url section_url = u[u.find('courseware/') + 11:] + if not os.path.exists(path): os.makedirs(path) diff --git a/lms/djangoapps/courseware/features/common.py b/lms/djangoapps/courseware/features/common.py index 8fb2843656..553797553a 100644 --- a/lms/djangoapps/courseware/features/common.py +++ b/lms/djangoapps/courseware/features/common.py @@ -77,7 +77,8 @@ def should_see_in_the_page(step, text): @step('I am logged in$') def i_am_logged_in(step): world.create_user('robot') - world.log_in('robot@edx.org', 'test') + world.log_in('robot', 'test') + world.browser.visit(django_url('/')) @step('I am not logged in$') @@ -126,7 +127,7 @@ def i_am_registered_for_the_course(step, course): # If the user is not already enrolled, enroll the user. CourseEnrollment.objects.get_or_create(user=u, course_id=course_id(course)) - world.log_in('robot@edx.org', 'test') + world.log_in('robot', 'test') @step(u'The course "([^"]*)" has extra tab "([^"]*)"$') diff --git a/lms/djangoapps/courseware/features/high-level-tabs.feature b/lms/djangoapps/courseware/features/high-level-tabs.feature index 931281a455..473f3f1572 100644 --- a/lms/djangoapps/courseware/features/high-level-tabs.feature +++ b/lms/djangoapps/courseware/features/high-level-tabs.feature @@ -6,7 +6,7 @@ Feature: All the high level tabs should work Scenario: I can navigate to all high -level tabs in a course Given: I am registered for the course "6.002x" And The course "6.002x" has extra tab "Custom Tab" - And I log in + And I am logged in And I click on View Courseware When I click on the "" tab Then the page title should contain ""