R
R
Ruslan Absalyamov2018-10-07 18:23:29
Laravel
Ruslan Absalyamov, 2018-10-07 18:23:29

Why can't I log in through the test?

I'm doing an http test, but I'm getting

Expected status code 200 but received 500.
Failed asserting that false is true.

At the same time, with such a request in the view, when I send data via axios, then everything works out for me.
Here is the test code
public function testLogin()
    {
        $responca = $this->post('/api/login', [
            'username' => 'r.absalyamov',
            'password' => 'kazan1811'
        ]);
        $responca->assertStatus(200);
    }

Who can tell what the problem could be, why I get a 500 error here, and when I make the wrong password, the 200 statement is triggered. Well, accordingly, when I enter the login incorrectly, the error is Expected status code 200 but received 401.
I only have this the option is not clear, when I enter the correct usename and password, why does a 500 error work for me, although this status does not come in the test
The controller that is being tested
public function login(Request $request)
    {
// Проверяем на пустые поля
        $errors = Validator::make($request->all(), [
            'username' => 'required',
            'password' => 'required'
        ])->errors();

Если есть значение то выводим 401 ошибку
        if (count($errors)){
            return response(['errors' => $errors], 401);
        }
//Проверяем есть ли пользователь
        $user = User::where('name', $request->username)->first();
        if (!$user)
            return response(['status' => 'error', 'message' => 'Пользователь не найден'], 401);//Если пользователя нет, то выводим 401 ошибку
//Проверяем пароль
        if (Hash::check($request->password, $user->password)){
//Если все нормально, то создаем api токен
            $response = new ApiTokenUser();
            $response = $response->setToken($user->email, $request->password);
            $response = json_decode((string) $response->getBody(), true); // И формируем ответ
        } else {
            $response = ['errors' => true, 'message' => 'Неверный пароль']; // формируем ответ
        }
//Выводим клиенту что пришло
        return response($response);
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Ukolov, 2018-10-07
@rusline18

Open the logs - everything is written there.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question