C
C
Crash2018-11-13 12:24:23
Web servers
Crash, 2018-11-13 12:24:23

How to log in using the HTTP client to the admin panel?

I'm making a script to check for broken links in the admin panel. It must login and follow all the links on a particular page. I use Guzzle as a client: https://github.com/guzzle/guzzle
But it turns out that when trying to log in from this client, in fact, I make a fake request, which CSRF protection successfully repels) How can I solve this problem? Temporarily disable CSRF while testing? Should be standard practice for this case.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2018-11-13
@Bandicoot

I'm making a script to check for broken links in the admin panel.
There should be a standard practice for this case.

Why not do it through Codeception? Namely, through acceptance tests ....
Through the PhpBrowser driver, cookies and a session work and you can log in and collect all the links :) And it is installed through the composer along with Codeception itself, a clear drawback to the web driver - it cannot wait (for example, requests via ajax)
In addition, the goal is still testing, so cover the entire admin panel with the simplest tests, and maybe even make a reserve for more complete testing
. An approximate view of the finished acceptance test:
// Тут предварительно логинимся (метод amLogin() не родной, 
     // надо создать его в классе AcceptanceTester)
     public function _before(AcceptanceTester $I)
    {
        $I->amLogin('admin', 'admin');
        $I->amOnPage('/admin');
    }

    /** Ниже добавляем через аннотацию провайдер всех ссылок 
     * и данный тест будет выполняться для каждой ссылки в админке
     * @dataProvider linksInAdminPanel
     * 
     *  Или вычесать все ссылки с каждой страницы в методе
     *  и записать в массив и потом по ним идти, проверяя -- была ссылка или нет
     *  
     */
    public function linkIsCorrect(AcceptanceTester $I, \Codeception\Example $pageAdmin)
    {
        $I->amOnPage($pageAdmin['url']);
        $I->seeResponseCodeIs(HttpCode::OK);
    }

    // Перечень ссылок в админке
    protected function linksInAdminPanel()
    {
        return [
            ['url' => '/admin/users'],
            ['url' => '/admin/settings'],
            ['url' => '/admin/products'],
            ....
        ];
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question