Testing whether a Url is giving 500 error or not in Django [duplicate]

早过忘川 提交于 2019-12-11 06:38:01

问题


I want to test Urls whether am getting 500 error or not. In normal case where login is not required I get status_code 200 but where login is required, it gives me 302 error. So, how can one test loginrequired and paramterized url in best way.

Thank You

So I am adding this because someone link that question into duplicate but it is not my answer and why it is not my answer because I can login with this method but I want to test that url whose views has loginrequired I can log in but not in that view

c.post('login/', {
'username': 'nitin',
'password': 'qwerty123321'})

if you don't know the answer with url then checkout question on 'views' How to test views with pytest whose views has LoginRequired and some specific user dependencies in that question I am getting user logged in with mixer.blend() and that's fine but in 'views' the user has some more functions related to that just check once. And please help me with that Thank you.

And atleast tell me how should I do this I am very messed with that. Thank you again

urls.py

path('', event_views.dashboard_view, name='event-dashboard'),

views.py

@login_required
def dashboard_view(request):
    # ccd = Org.objects.first()
    # print(ccd.__dict__)
    # print(request.user.)
    org = request.user.profile.org
    week_responses = day_wise_responses(7, org)
    user_org = request.user.profile.org.name
    sms_sent = org.sms_counter
    email_sent = org.email_counter
    today = datetime.today().date()
    responses_one_week = number_of_responses(7, org)
    responses_two_week = number_of_responses(14, org)
    average_rating = org_average_rating(org)
    responses_last_week = responses_two_week - responses_one_week
    if responses_last_week:
        responses_percent_change = (abs(responses_one_week - responses_last_week)/responses_last_week)*100
    else:
        responses_percent_change = responses_one_week*100
    # last n responses
    last_5_responses = last_n_responses(5, org)
    # print(last_5_responses)
    context = {'week_responses': week_responses, 'user_org': user_org, 'today': today,
               'responses_one_week': responses_one_week, 'responses_percent_change': responses_percent_change,
               'last_5_responses': last_5_responses, 'sms_sent': sms_sent, 'email_sent': email_sent,
               'average_rating': average_rating}
    return render(request, 'events/dashboard.html', context)

test_urls.py

@pytest.mark.django_db
class TestUrls(test.TestCase):
def test_event_dashboard(self):
c = Client()
c.post('login/', {
'username': 'nitin',
'password': 'qwerty123321'})
 response = c.get(reverse('event-dashboard'))
 self.assertEqual(response.status_code, 200)



回答1:


The 302 is because your user is being redirected to the login screen.

If you want to test views that require authentication, you'll need to authenticate the user first.

Luckily, this is very easy to do. See the docs.

# Create a new user
User.objects.create_user(
    username='fred',
    password='secret'
)

# Start up a test client
c = Client()

# Authenticate the user on the client
c.login(username='fred', password='secret')

# Do your thing
response = c.get(reverse('event-dashboard'))
self.assertEqual(response.status_code, 200)


来源:https://stackoverflow.com/questions/55583069/testing-whether-a-url-is-giving-500-error-or-not-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!