Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pytest Flask, error 308 Permanent Redirect when login

I want to try my app, here is the test code :

import sys
import pytest
from flask_simplelogin import SimpleLogin

sys.path.insert(1, '')
from app import app as myapp


#---------------------------------------------------
#SETUP
#---------------------------------------------------

myapp.config['SECRET_KEY'] = 'something-secret'
myapp.config['SIMPLELOGIN_USERNAME'] = 'admin'
myapp.config['SIMPLELOGIN_PASSWORD'] = 'secret'
SimpleLogin(myapp)

@pytest.fixture
def app():
    return myapp

@pytest.fixture
def setup_url():
    return "http://127.0.0.1:8080/"

def login(client):
    return client.post("http://127.0.0.1:8080/login", data=dict(
        username="admin",
        password="secret"
    ))

#---------------------------------------------------
#TESTS
#---------------------------------------------------

def test_get_list_campagne(client,setup_url):
    resp = login(client)
    assert resp.status_code==200

    resp = client.get("/Campagne")
    assert resp.status_code==200

But it returns a permanent redirection, so i tried with the attribute "follow_redirects=True" :

def login(client):
    return client.post("http://127.0.0.1:8080/login", data=dict(
        username="admin",
        password="taleinfo"
    ), follow_redirects=True)

But i think it is just a way to get around the problem, i don't think i'm logged after that.

here the error : E AssertionError: assert 308 == 200 E + where 308 = < 263 bytes [308 PERMANENT REDIRECT]>.status_code

I do not found any similar problem. Thank you for your help.

like image 856
Valentin Boinard Avatar asked Apr 28 '20 08:04

Valentin Boinard


2 Answers

Had the same problem I solved it by adding a Slash /

try this code:

def login(client):
return client.post("http://127.0.0.1:8080/login/", data=dict(
    username="admin",
    password="secret"
))

as you can see add the Slash in the login/ like that. it sounds stupid I know but give it a try and let me know.

like image 53
Fady Ayoub Avatar answered Oct 13 '22 21:10

Fady Ayoub


I got this same error as a result of having two slashes in my path. It was something like:

def test_my_annoying_failing_test(client):
   response = client.post('/api//my_path/to-somewhere') # notice the double slash
   # ...test continues here..

Changing that double slash to a single slash fixed it for me:

def test_my_annoying_failing_test(client):
   response = client.post('/api/my_path/to-somewhere')
   # ...test continues here..

I am not sure what the root cause of that issue is though.

like image 39
Chidiebere Avatar answered Oct 13 '22 20:10

Chidiebere