Commit f69778b3 by Will Daly

Changed lettuce test log_in step to log in programatically

rather than using the login dialog.
parent 93eebdcd
from lettuce import world, step from lettuce import world, step
from .factories import * from .factories import *
from lettuce.django import django_url 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.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 student.models import CourseEnrollment
from urllib import quote_plus from urllib import quote_plus
from nose.tools import assert_equals from nose.tools import assert_equals
...@@ -78,7 +83,7 @@ def the_page_title_should_contain(step, title): ...@@ -78,7 +83,7 @@ def the_page_title_should_contain(step, title):
@step('I am a logged in user$') @step('I am a logged in user$')
def i_am_logged_in_user(step): def i_am_logged_in_user(step):
create_user('robot') create_user('robot')
log_in('robot@edx.org', 'test') log_in('robot', 'test')
@step('I am not logged in$') @step('I am not logged in$')
...@@ -93,7 +98,7 @@ def i_am_staff_for_course_by_id(step, course_id): ...@@ -93,7 +98,7 @@ def i_am_staff_for_course_by_id(step, course_id):
@step('I log in$') @step('I log in$')
def i_log_in(step): def i_log_in(step):
log_in('robot@edx.org', 'test') log_in('robot', 'test')
@step(u'I am an edX user$') @step(u'I am an edX user$')
...@@ -128,30 +133,38 @@ def create_user(uname): ...@@ -128,30 +133,38 @@ def create_user(uname):
@world.absorb @world.absorb
def log_in(email, password): def log_in(username, password):
world.browser.cookies.delete() '''
world.browser.visit(django_url('/')) Log the user in programatically
world.browser.is_element_present_by_css('header.global', 10) '''
world.browser.click_link_by_href('#login-modal')
# Authenticate the user
# Wait for the login dialog to load user = authenticate(username=username, password=password)
# This is complicated by the fact that sometimes a second #login_form assert(user is not None and user.is_active)
# dialog loads, while the first one remains hidden.
# We give them both time to load, starting with the second one. # Send a fake HttpRequest to log the user in
world.browser.is_element_present_by_css('section.content-wrapper form#login_form', wait_time=4) # We need to process the request using
world.browser.is_element_present_by_css('form#login_form', wait_time=2) # Session middleware and Authentication middleware
# to ensure that session state can be stored
# For some reason, the page sometimes includes two #login_form request = HttpRequest()
# elements, the first of which is not visible. SessionMiddleware().process_request(request)
# To avoid this, we always select the last of the two #login_form dialogs AuthenticationMiddleware().process_request(request)
login_form = world.browser.find_by_css('form#login_form').last login(request, user)
login_form.find_by_name('email').fill(email) # Save the session
login_form.find_by_name('password').fill(password) request.session.save()
login_form.find_by_name('submit').click()
# 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)
# wait for the page to redraw # WebDriver has an issue where we cannot set cookies
assert world.browser.is_element_present_by_css('.content-wrapper', wait_time=10) # 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 @world.absorb
...@@ -208,6 +221,7 @@ def save_the_course_content(path='/tmp'): ...@@ -208,6 +221,7 @@ def save_the_course_content(path='/tmp'):
u = world.browser.url u = world.browser.url
section_url = u[u.find('courseware/') + 11:] section_url = u[u.find('courseware/') + 11:]
if not os.path.exists(path): if not os.path.exists(path):
os.makedirs(path) os.makedirs(path)
......
...@@ -77,7 +77,8 @@ def should_see_in_the_page(step, text): ...@@ -77,7 +77,8 @@ def should_see_in_the_page(step, text):
@step('I am logged in$') @step('I am logged in$')
def i_am_logged_in(step): def i_am_logged_in(step):
world.create_user('robot') 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$') @step('I am not logged in$')
...@@ -126,7 +127,7 @@ def i_am_registered_for_the_course(step, course): ...@@ -126,7 +127,7 @@ def i_am_registered_for_the_course(step, course):
# If the user is not already enrolled, enroll the user. # If the user is not already enrolled, enroll the user.
CourseEnrollment.objects.get_or_create(user=u, course_id=course_id(course)) 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 "([^"]*)"$') @step(u'The course "([^"]*)" has extra tab "([^"]*)"$')
......
...@@ -6,7 +6,7 @@ Feature: All the high level tabs should work ...@@ -6,7 +6,7 @@ Feature: All the high level tabs should work
Scenario: I can navigate to all high -level tabs in a course Scenario: I can navigate to all high -level tabs in a course
Given: I am registered for the course "6.002x" Given: I am registered for the course "6.002x"
And The course "6.002x" has extra tab "Custom Tab" 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 And I click on View Courseware
When I click on the "<TabName>" tab When I click on the "<TabName>" tab
Then the page title should contain "<PageTitle>" Then the page title should contain "<PageTitle>"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment