From 7506562f5069256fb6cc816bbbaef051ceb91420 Mon Sep 17 00:00:00 2001 From: David Baumgold Date: Tue, 2 Jul 2013 10:57:10 -0400 Subject: [PATCH] Another lettuce test, and fixes to make it work --- .../contentstore/features/textbooks.feature | 27 +++++++++ .../contentstore/features/textbooks.py | 55 ++++++++++++++++--- cms/djangoapps/contentstore/views/course.py | 4 +- cms/static/js/models/textbook.js | 2 +- 4 files changed, 78 insertions(+), 10 deletions(-) diff --git a/cms/djangoapps/contentstore/features/textbooks.feature b/cms/djangoapps/contentstore/features/textbooks.feature index 07b7868c41..0758a0b57b 100644 --- a/cms/djangoapps/contentstore/features/textbooks.feature +++ b/cms/djangoapps/contentstore/features/textbooks.feature @@ -18,3 +18,30 @@ Feature: Textbooks Then I should see a textbook named "Economics" with a chapter path containing "/c4x/MITx/999/asset/textbook.pdf" And I reload the page Then I should see a textbook named "Economics" with a chapter path containing "/c4x/MITx/999/asset/textbook.pdf" + + Scenario: Create a textbook with multiple chapters + Given I have opened a new course in Studio + And I go to the textbooks page + When I click on the New Textbook button + And I name my textbook "History" + And I name the first chapter "Britain" + And I type in "britain.pdf" for the first chapter asset + And I click Add a Chapter + And I name the second chapter "America" + And I type in "america.pdf" for the second chapter asset + And I save the textbook + Then I should see a textbook named "History" with 2 chapters + And I click the textbook chapters + Then I should see a textbook named "History" with 2 chapters + And the first chapter should be named "Britain" + And the first chapter should have an asset called "britain.pdf" + And the second chapter should be named "America" + And the second chapter should have an asset called "america.pdf" + And I reload the page + Then I should see a textbook named "History" with 2 chapters + And I click the textbook chapters + Then I should see a textbook named "History" with 2 chapters + And the first chapter should be named "Britain" + And the first chapter should have an asset called "britain.pdf" + And the second chapter should be named "America" + And the second chapter should have an asset called "america.pdf" diff --git a/cms/djangoapps/contentstore/features/textbooks.py b/cms/djangoapps/contentstore/features/textbooks.py index a03bf77e16..13a952287b 100644 --- a/cms/djangoapps/contentstore/features/textbooks.py +++ b/cms/djangoapps/contentstore/features/textbooks.py @@ -42,14 +42,27 @@ def name_textbook(_step, name): input_css = ".textbook input[name=textbook-name]" world.css_fill(input_css, name) -@step(u'I name the first chapter "([^"]*)"') -def name_chapter(_step, name): - input_css = ".textbook input.chapter-name" +@step(u'I name the (first|second|third) chapter "([^"]*)"') +def name_chapter(_step, ordinal, name): + index = ["first", "second", "third"].index(ordinal) + input_css = ".textbook .chapter{i} input.chapter-name".format(i=index+1) world.css_fill(input_css, name) -@step(u'I click the Upload Asset link for the first chapter') -def click_upload_asset(_step): - button_css = ".chapter .action-upload" +@step(u'I type in "([^"]*)" for the (first|second|third) chapter asset') +def asset_chapter(_step, name, ordinal): + index = ["first", "second", "third"].index(ordinal) + input_css = ".textbook .chapter{i} input.chapter-asset-path".format(i=index+1) + world.css_fill(input_css, name) + +@step(u'I click the Upload Asset link for the (first|second|third) chapter') +def click_upload_asset(_step, ordinal): + index = ["first", "second", "third"].index(ordinal) + button_css = ".textbook .chapter{i} .action-upload".format(i=index+1) + world.css_click(button_css) + +@step(u'I click Add a Chapter') +def click_add_chapter(_step): + button_css = ".textbook .action-add-chapter" world.css_click(button_css) @step(u'I save the textbook') @@ -58,9 +71,37 @@ def save_textbook(_step): world.css_click(submit_css) @step(u'I should see a textbook named "([^"]*)" with a chapter path containing "([^"]*)"') -def check_textbook(step, textbook_name, chapter_name): +def check_textbook(_step, textbook_name, chapter_name): title = world.css_find(".textbook h3.textbook-title") chapter = world.css_find(".textbook .wrap-textbook p") assert title.text == textbook_name, "{} != {}".format(title.text, textbook_name) assert chapter.text == chapter_name, "{} != {}".format(chapter.text, chapter_name) +@step(u'I should see a textbook named "([^"]*)" with (\d+) chapters') +def check_textbook(_step, textbook_name, num_chapters_str): + num_chapters = int(num_chapters_str) + title = world.css_find(".textbook .view-textbook h3.textbook-title") + toggle = world.css_find(".textbook .view-textbook .chapter-toggle") + assert title.text == textbook_name, "{} != {}".format(title.text, textbook_name) + assert toggle.text == "{num} PDF Chapters".format(num=num_chapters), \ + "Expected {num} chapters, found {real}".format(num=num_chapters, real=toggle.text) + +@step(u'I click the textbook chapters') +def click_chapters(_step): + world.css_click(".textbook a.chapter-toggle") + +@step(u'the (first|second|third) chapter should be named "([^"]*)"') +def check_chapter_name(_step, ordinal, name): + index = ["first", "second", "third"].index(ordinal) + chapter = world.css_find(".textbook .view-textbook ol.chapters li")[index] + element = chapter.find_by_css(".chapter-name") + assert element.text == name, "Expected chapter named {expected}, found chapter named {actual}".format( + expected=name, actual=element.text) + +@step(u'the (first|second|third) chapter should have an asset called "([^"]*)"') +def check_chapter_name(_step, ordinal, name): + index = ["first", "second", "third"].index(ordinal) + chapter = world.css_find(".textbook .view-textbook ol.chapters li")[index] + element = chapter.find_by_css(".chapter-asset-path") + assert element.text == name, "Expected chapter with asset {expected}, found chapter with asset {actual}".format( + expected=name, actual=element.text) diff --git a/cms/djangoapps/contentstore/views/course.py b/cms/djangoapps/contentstore/views/course.py index 99f77b6c77..1062798783 100644 --- a/cms/djangoapps/contentstore/views/course.py +++ b/cms/djangoapps/contentstore/views/course.py @@ -543,7 +543,7 @@ def create_textbook(request, org, course, name): @login_required @ensure_csrf_cookie -@require_http_methods(("GET", "POST", "DELETE")) +@require_http_methods(("GET", "POST", "PUT", "DELETE")) def textbook_by_id(request, org, course, name, tid): """ JSON API endpoint for manipulating a textbook via its internal ID. @@ -563,7 +563,7 @@ def textbook_by_id(request, org, course, name, tid): if not textbook: return JsonResponse(status=404) return JsonResponse(textbook) - elif request.method == 'POST': + elif request.method in ('POST', 'PUT'): try: new_textbook = validate_textbook_json(request.body) except TextbookValidationError as e: diff --git a/cms/static/js/models/textbook.js b/cms/static/js/models/textbook.js index 6f4010c050..54bbbc2d74 100644 --- a/cms/static/js/models/textbook.js +++ b/cms/static/js/models/textbook.js @@ -85,7 +85,7 @@ CMS.Models.Textbook = Backbone.AssociatedModel.extend({ invalidChapters.push(chapter); } }); - if(invalidChapters) { + if(!_.isEmpty(invalidChapters)) { return { message: "All chapters must have a name and asset", attributes: {chapters: invalidChapters}