upload.py 7.05 KB
Newer Older
1 2
# pylint: disable=C0111
# pylint: disable=W0621
3 4

from lettuce import world, step
5
from lettuce.django import django_url
6 7 8 9
from django.conf import settings
import requests
import string
import random
10
import os
11 12
from django.contrib.auth.models import User
from student.models import CourseEnrollment
13
from nose.tools import assert_equal, assert_not_equal  # pylint: disable=E0611
14

15
TEST_ROOT = settings.COMMON_TEST_DATA_ROOT
16
ASSET_NAMES_CSS = 'td.name-col > span.title > a.filename'
17

18

cahrens committed
19
@step(u'I go to the files and uploads page$')
20
def go_to_uploads(_step):
21
    menu_css = 'li.nav-course-courseware'
22
    uploads_css = 'li.nav-course-courseware-uploads a'
23 24
    world.css_click(menu_css)
    world.css_click(uploads_css)
25 26


27
@step(u'I upload the( test)? file "([^"]*)"$')
28 29 30 31 32
def upload_file(_step, is_test_file, file_name, button_text=None):
    if button_text:
        world.click_link(button_text)
    else:
        world.click_link('Upload New File')
33

polesye committed
34 35
    if not is_test_file:
        _write_test_file(file_name, "test file")
36 37

    # uploading the file itself
38
    path = os.path.join(TEST_ROOT, 'uploads/', file_name)
JonahStanley committed
39
    world.browser.execute_script("$('input.file-input').css('display', 'block')")
Julian Arni committed
40
    world.browser.attach_file('file', os.path.abspath(path))
41 42 43 44
    close_css = 'a.close-button'
    world.css_click(close_css)


45 46 47 48 49
@step(u'I upload the file "([^"]*)" by clicking "([^"]*)"')
def upload_file_on_button_press(_step, file_name, button_text=None):
    upload_file(_step, '', file_name, button_text)


50
@step(u'I upload the files "([^"]*)"$')
51
def upload_files(_step, files_string):
52
    # files_string should be comma separated with no spaces.
53 54 55
    files = files_string.split(",")
    upload_css = 'a.upload-button'
    world.css_click(upload_css)
56 57 58 59 60

    # uploading the files
    for filename in files:
        _write_test_file(filename, "test file")
        path = os.path.join(TEST_ROOT, 'uploads/', filename)
61
        world.browser.execute_script("$('input.file-input').css('display', 'block')")
Julian Arni committed
62
        world.browser.attach_file('file', os.path.abspath(path))
63

64
    close_css = 'a.close-button'
65
    world.css_click(close_css)
66 67


68 69 70 71 72 73 74 75 76
@step(u'I should not see the file "([^"]*)" was uploaded$')
def check_not_there(_step, file_name):
    # Either there are no files, or there are files but
    # not the one I expect not to exist.

    # Since our only test for deletion right now deletes
    # the only file that was uploaded, our success criteria
    # will be that there are no files.
    # In the future we can refactor if necessary.
77
    assert(world.is_css_not_present(ASSET_NAMES_CSS))
78 79 80 81


@step(u'I should see the file "([^"]*)" was uploaded$')
def check_upload(_step, file_name):
82
    index = get_index(file_name)
83
    assert_not_equal(index, -1)
84 85


86
@step(u'The url for the file "([^"]*)" is valid$')
87
def check_url(_step, file_name):
88
    r = get_file(file_name)
89
    assert_equal(r.status_code, 200)
90 91


JonahStanley committed
92
@step(u'I delete the file "([^"]*)"$')
93
def delete_file(_step, file_name):
JonahStanley committed
94 95
    index = get_index(file_name)
    assert index != -1
96
    delete_css = "a.remove-asset-button"
JonahStanley committed
97
    world.css_click(delete_css, index=index)
98
    world.confirm_studio_prompt()
JonahStanley committed
99 100 101


@step(u'I should see only one "([^"]*)"$')
102
def no_duplicate(_step, file_name):
103
    all_names = world.css_find(ASSET_NAMES_CSS)
104 105
    only_one = False
    for i in range(len(all_names)):
106
        if file_name == world.css_html(ASSET_NAMES_CSS, index=i):
107 108 109 110
            only_one = not only_one
    assert only_one


111
@step(u'I can download the correct "([^"]*)" file$')
112
def check_download(_step, file_name):
113 114 115 116 117 118
    path = os.path.join(TEST_ROOT, 'uploads/', file_name)
    with open(os.path.abspath(path), 'r') as cur_file:
        cur_text = cur_file.read()
        r = get_file(file_name)
        downloaded_text = r.text
        assert cur_text == downloaded_text
119
    # resetting the file back to its original state
120 121 122 123 124
    _write_test_file(file_name, "This is an arbitrary file for testing uploads")


def _write_test_file(file_name, text):
    path = os.path.join(TEST_ROOT, 'uploads/', file_name)
125
    # resetting the file back to its original state
126
    with open(os.path.abspath(path), 'w') as cur_file:
127
        cur_file.write(text)
128 129 130


@step(u'I modify "([^"]*)"$')
131
def modify_upload(_step, file_name):
132
    new_text = ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(10))
133 134 135
    _write_test_file(file_name, new_text)


136 137 138 139 140 141 142 143 144 145 146 147
@step(u'I upload an asset$')
def upload_an_asset(step):
    step.given('I upload the file "asset.html"')


@step(u'I (lock|unlock) the asset$')
def lock_unlock_file(_step, _lock_state):
    index = get_index('asset.html')
    assert index != -1, 'Expected to find an asset but could not.'

    # Warning: this is a misnomer, it really only toggles the
    # lock state. TODO: fix it.
148 149
    lock_css = "input.lock-checkbox"
    world.css_find(lock_css)[index].click()
150 151


152 153 154 155 156
@step(u'the user "([^"]*)" is enrolled in the course$')
def user_foo_is_enrolled_in_the_course(step, name):
    world.create_user(name, 'test')
    user = User.objects.get(username=name)

157
    course_id = world.scenario_dict['COURSE'].id
158 159 160 161 162 163 164
    CourseEnrollment.enroll(user, course_id)


@step(u'Then the asset is (locked|unlocked)$')
def verify_lock_unlock_file(_step, lock_state):
    index = get_index('asset.html')
    assert index != -1, 'Expected to find an asset but could not.'
165 166 167
    lock_css = "input.lock-checkbox"
    checked = world.css_find(lock_css)[index]._element.get_attribute('checked')
    assert_equal(lock_state == "locked", bool(checked))
168 169


170 171
@step(u'I am at the files and upload page of a Studio course')
def at_upload_page(step):
172 173
    step.given('I have opened a new course in studio')
    step.given('I go to the files and uploads page')
174 175 176 177 178 179 180


@step(u'I have created a course with a (locked|unlocked) asset$')
def open_course_with_locked(step, lock_state):
    step.given('I am at the files and upload page of a Studio course')
    step.given('I upload the file "asset.html"')

181
    if lock_state == "locked":
182
        step.given('I lock the asset')
183 184 185
        step.given('I reload the page')


186 187 188
@step(u'Then the asset is (viewable|protected)$')
def view_asset(_step, status):
    url = django_url('/c4x/MITx/999/asset/asset.html')
189
    if status == 'viewable':
190
        expected_text = 'test file'
191
    else:
192
        expected_text = 'Unauthorized'
193

194 195 196
    # Note that world.visit would trigger a 403 error instead of displaying "Unauthorized"
    # Instead, we can drop back into the selenium driver get command.
    world.browser.driver.get(url)
Jay Zoldak committed
197
    assert_equal(world.css_text('body'), expected_text)
198 199


cahrens committed
200
@step('I see a confirmation that the file was deleted$')
Peter Fogg committed
201
def i_see_a_delete_confirmation(_step):
202
    alert_css = '#notification-confirmation'
203 204 205
    assert world.is_css_present(alert_css)


206
def get_index(file_name):
207
    all_names = world.css_find(ASSET_NAMES_CSS)
208
    for i in range(len(all_names)):
209
        if file_name == world.css_html(ASSET_NAMES_CSS, index=i):
210 211
            return i
    return -1
212 213 214 215 216


def get_file(file_name):
    index = get_index(file_name)
    assert index != -1
217
    url_css = 'a.filename'
218

219 220 221 222
    def get_url():
        return world.css_find(url_css)[index]._element.get_attribute('href')
    url = world.retry_on_exception(get_url)
    return requests.get(url)