advanced_settings.py 4.97 KB
Newer Older
1
# pylint: disable=missing-docstring
2
# pylint: disable=redefined-outer-name
3

cahrens committed
4
from lettuce import world, step
5
from nose.tools import assert_false, assert_equal, assert_regexp_matches  # pylint: disable=no-name-in-module
6
from common import type_in_codemirror, press_the_notification_button, get_codemirror_value
cahrens committed
7

8 9
KEY_CSS = '.key h3.title'
DISPLAY_NAME_KEY = "Course Display Name"
10
DISPLAY_NAME_VALUE = '"Robot Super Course"'
11 12 13
ADVANCED_MODULES_KEY = "Advanced Module List"
# A few deprecated settings for testing toggling functionality.
DEPRECATED_SETTINGS = ["CSS Class for Course Reruns", "Hide Progress Tab", "XQA Key"]
cahrens committed
14

Will Daly committed
15

cahrens committed
16 17
@step('I select the Advanced Settings$')
def i_select_advanced_settings(step):
18

David Baumgold committed
19
    world.click_course_settings()
20 21 22 23 24 25 26 27

    # The click handlers are set up so that if you click <body>
    # the menu disappears.  This means that if we're even a *little*
    # bit off on the last item ('Advanced Settings'), the menu
    # will close and the test will fail.
    # For this reason, we retrieve the link and visit it directly
    # This is what the browser *should* be doing, since it's just a native
    # link with no JavaScript involved.
28
    link_css = 'li.nav-course-settings-advanced a'
29 30 31
    world.wait_for_visible(link_css)
    link = world.css_find(link_css).first['href']
    world.visit(link)
cahrens committed
32 33


34 35 36 37 38 39
@step('I am on the Advanced Course Settings page in Studio$')
def i_am_on_advanced_course_settings(step):
    step.given('I have opened a new course in Studio')
    step.given('I select the Advanced Settings')


40 41
@step(u'I edit the value of a policy key$')
def edit_the_value_of_a_policy_key(step):
42
    type_in_codemirror(get_index_of(DISPLAY_NAME_KEY), 'X')
43 44


45
@step(u'I edit the value of a policy key and save$')
46
def edit_the_value_of_a_policy_key_and_save(step):
47 48 49
    change_display_name_value(step, '"foo"')


50 51 52
@step('I create a JSON object as a value for "(.*)"$')
def create_JSON_object(step, key):
    change_value(step, key, '{"key": "value", "key_2": "value_2"}')
53 54 55 56 57


@step('I create a non-JSON value not in quotes$')
def create_value_not_in_quotes(step):
    change_display_name_value(step, 'quote me')
58

59

cahrens committed
60 61 62 63
@step('I see default advanced settings$')
def i_see_default_advanced_settings(step):
    # Test only a few of the existing properties (there are around 34 of them)
    assert_policy_entries(
64
        [ADVANCED_MODULES_KEY, DISPLAY_NAME_KEY, "Show Calculator"], ["[]", DISPLAY_NAME_VALUE, "false"])
65 66


67
@step('the settings are alphabetized$')
68
def they_are_alphabetized(step):
69
    key_elements = world.css_find(KEY_CSS)
70 71 72 73 74
    all_keys = []
    for key in key_elements:
        all_keys.append(key.value)

    assert_equal(sorted(all_keys), all_keys, "policy keys were not sorted")
75 76


77 78
@step('it is displayed as formatted$')
def it_is_formatted(step):
79
    assert_policy_entries(['Discussion Topic Mapping'], ['{\n    "key": "value",\n    "key_2": "value_2"\n}'])
80 81 82 83


@step('I get an error on save$')
def error_on_save(step):
84
    assert_regexp_matches(
85 86
        world.css_text('.error-item-message'),
        "Value stored in a .* must be .*, found .*"
87
    )
88 89 90


@step('it is displayed as a string')
91
def it_is_displayed_as_string(step):
92 93
    assert_policy_entries([DISPLAY_NAME_KEY], ['"quote me"'])

94

95 96
@step(u'the policy key value is unchanged$')
def the_policy_key_value_is_unchanged(step):
97
    assert_equal(get_display_name_value(), DISPLAY_NAME_VALUE)
98 99 100


@step(u'the policy key value is changed$')
cahrens committed
101
def the_policy_key_value_is_changed(step):
102
    assert_equal(get_display_name_value(), '"foo"')
103

104

105 106 107 108 109 110 111 112 113 114 115 116 117 118
@step(u'deprecated settings are (then|not) shown$')
def verify_deprecated_settings_shown(_step, expected):
    for setting in DEPRECATED_SETTINGS:
        if expected == "not":
            assert_equal(-1, get_index_of(setting))
        else:
            world.wait_for(lambda _: get_index_of(setting) != -1)


@step(u'I toggle the display of deprecated settings$')
def toggle_deprecated_settings(_step):
    world.css_click(".deprecated-settings-label")


119
def assert_policy_entries(expected_keys, expected_values):
120 121 122
    for key, value in zip(expected_keys, expected_values):
        index = get_index_of(key)
        assert_false(index == -1, "Could not find key: {key}".format(key=key))
123
        found_value = get_codemirror_value(index)
124 125 126 127
        assert_equal(
            value, found_value,
            "Expected {} to have value {} but found {}".format(key, value, found_value)
        )
128

cahrens committed
129

cahrens committed
130
def get_index_of(expected_key):
131 132 133
    for i, element in enumerate(world.css_find(KEY_CSS)):
        # Sometimes get stale reference if I hold on to the array of elements
        key = world.css_value(KEY_CSS, index=i)
cahrens committed
134
        if key == expected_key:
135
            return i
136

cahrens committed
137
    return -1
cahrens committed
138

139

cahrens committed
140
def get_display_name_value():
141
    index = get_index_of(DISPLAY_NAME_KEY)
142
    return get_codemirror_value(index)
143

144

145
def change_display_name_value(step, new_value):
146
    change_value(step, DISPLAY_NAME_KEY, new_value)
147

148

149
def change_value(step, key, new_value):
150 151
    index = get_index_of(key)
    type_in_codemirror(index, new_value)
152
    press_the_notification_button(step, "Save")
153
    world.wait_for_ajax_complete()