为什么管理员得到"您没有权限编辑任何内容"在StaticLiveServerCase下运行时,它在开发服务器下运行正常

时间:2014-05-18 17:57:08

标签: django-admin django-testing

使用django 1.7我创建了一个非超级用户的管理员用户,因此只能编辑应用模型(教师和工作名单)。在开发服务器下运行时,它运行正常;我可以作为安迪管理员登录,我只看到编辑名单和教师的链接。我为此写的测试会出现问题;在StaticLiveServerCase下运行,管理员得到了#34;你没有权限编辑任何东西"错误。

以下是测试:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

from .base import FunctionalTest

class TeacherCreationTest(FunctionalTest):
    fixtures = ['andy_the_admin.json']

    def test_can_create_new_teacher_via_admin(self):
        # Andy, the administrator, opens his web browser, and 
        # goes to the admin page
        self.browser.get(self.live_server_url + '/admin/')

        # He types in his username and password and hits return
        username_field = self.browser.find_element_by_name('username')
        username_field.send_keys('andy')
        password_field = self.browser.find_element_by_name('password')
        password_field.send_keys('adminpass')
        password_field.send_keys(Keys.RETURN)

        # His username and password are accepted, and he is taken to
        # the Site Administration page
        body = self.browser.find_element_by_tag_name('body')
        self.assertIn('Site administration', body.text)

        # He now sees a hyperlink that says "Teachers"
        teacher_links = self.browser.find_elements_by_link_text('Teachers')
        self.assertTrue(len(teacher_links) > 0)

FunctionalTest类是StaticLiveServerCase的子类。它包含setUp()和tearDown()函数,它们实例化浏览器并在每次测试后退出。它失败了,消息,self.assertTrue(len(teacher_links)> 0) AssertionError:False不正确

fixture,andy_the_admin.json是通过运行dumpdata auth.User>创建的。灯具/ andy_the_user.json。我已经尝试删除数据库(sqlite3)并在创建夹具之前重新重新构建它。我也尝试使用dumpdata auth.User名单创建夹具,并在数据库中包含一些教师记录,我尝试了dumpdata -e contenttypes。他们都没有参加考试。

这是固定装置:

[
{
  "fields": {
    "username": "robert",
    "first_name": "",
    "last_name": "",
    "is_active": true,
    "is_superuser": true,
    "is_staff": true,
    "last_login": "2014-05-17T17:17:34.215Z",
    "groups": [],
    "user_permissions": [],
    "password": "pbkdf2_sha256$12000$CZPPVO7vdgAd$QCHmwSOCMicbSCZeE7SAIX4fHcbE/PcU0s6V14oymeU=",
    "email": "",
    "date_joined": "2014-05-17T17:16:42.382Z"
  },
  "model": "auth.user",
  "pk": 1
},
{
  "fields": {
    "username": "andy",
    "first_name": "",
    "last_name": "",
    "is_active": true,
    "is_superuser": false,
    "is_staff": true,
    "last_login": "2014-05-17T17:20:18.152Z",
    "groups": [],
    "user_permissions": [
      19,
      20,
      21
    ],
    "password": "pbkdf2_sha256$12000$JEECtYmNDGIE$FObcdtu7z+6kRwLBPiRcBcwbGHUN0Xz796Vr7CWYndw=",
    "email": "",
    "date_joined": "2014-05-17T17:18:10Z"
  },`enter code here`
  "model": "auth.user",
  "pk": 2
}
]

如果我编辑灯具以使andy.is_superuser为True,那么测试工作正常;安迪可以编辑教师和名册,但他也可以编辑我不想要的用户和组。在开发服务器下,andy的登录工作正常,这使我很难想到我的项目设置可能出错的任何事情。如果我使用超级用户并且测试运行正确的事实使得很难想到测试可能出错的任何事情。在开发服务器下运行时,几天的谷歌搜索只出现了错误。

0 个答案:

没有答案