<commit_before><?php namespace Cub\CubLaravel\Test;

use Cub;
use Cub\CubLaravel\Test\Models\User;
use Firebase\JWT\JWT;

class CubAuthMiddlewareTest extends CubLaravelTestCase
{
    /** @test */
    public function request_with_valid_token()
    {
        $expected = [
            'code' => 200,
            'content' => json_encode(['message' => 'Hello, Cub User '.$this->details['id']]),
        ];

        $jwt = $this->getToken();

        $actual = $this->get('restricted?cub_token='.$jwt);

        $this->assertEquals($expected['code'], $actual->getStatusCode());
        $this->assertEquals($expected['content'], $actual->getContent());
    }

    /** @test */
    public function request_with_valid_token_header()
    {
        $expected = [
            'code' => 200,
            'content' => json_encode(['message' => 'Hello, Cub User '.$this->details['id']]),
        ];

        $jwt = $this->getToken();

        $actual = $this->get('restricted', ['HTTP_Authorization' => 'Bearer '.$jwt]);

        $this->assertEquals($expected['code'], $actual->getStatusCode());
        $this->assertEquals($expected['content'], $actual->getContent());
    }

    /** @test */
    public function request_needs_token()
    {
        $expected = [
            'code' => 400,
            'content' => json_encode(['error' => 'token_not_provided']),
        ];

        $actual = $this->get('restricted');

        $this->assertEquals($expected['code'], $actual->getStatusCode());
        $this->assertEquals($expected['content'], $actual->getContent());
    }

    /** @test */
    public function request_with_expired_token()
    {
        $expected = [
            'code' => 401,
            'content' => json_encode(['error' => 'expired_token']),
        ];

        $jwt = $this->getToken([
            'exp' => time() - 5000,
        ]);

        $actual = $this->get('restricted?cub_token='.$jwt);

        $this->assertEquals($expected['code'], $actual->getStatusCode());
        $this->assertEquals($expected['content'], $actual->getContent());
    }

    /** @test */
    public function request_with_expired_token_header()
    {
        $expected = [
            'code' => 401,
            'content' => json_encode(['error' => 'expired_token']),
        ];

        $jwt = $this->getToken([
            'exp' => time() - 5000,
        ]);

        $actual = $this->get('restricted', ['HTTP_Authorization' => 'Bearer '.$jwt]);

        $this->assertEquals($expected['code'], $actual->getStatusCode());
        $this->assertEquals($expected['content'], $actual->getContent());
    }

    /** @test */
    public function request_with_token_not_yet_valid()
    {
        $expected = [
            'code' => 401,
            'content' => json_encode(['error' => 'token_not_yet_valid']),
        ];

        $jwt = $this->getToken([
            'nbf' => time() + 5000,
        ]);

        $actual = $this->get('restricted?cub_token='.$jwt);

        $this->assertEquals($expected['code'], $actual->getStatusCode());
        $this->assertEquals($expected['content'], $actual->getContent());
    }

    /** @test */
    public function request_with_token_not_yet_valid_header()
    {
        $expected = [
            'code' => 401,
            'content' => json_encode(['error' => 'token_not_yet_valid']),
        ];

        $jwt = $this->getToken([
            'nbf' => time() + 5000,
        ]);

        $actual = $this->get('restricted', ['HTTP_Authorization' => 'Bearer '.$jwt]);

        $this->assertEquals($expected['code'], $actual->getStatusCode());
        $this->assertEquals($expected['content'], $actual->getContent());
    }

    /** @test */
    public function request_with_non_existent_user()
    {
        User::whereCubId($this->details['id'])->first()->delete();

        $expected = [
            'code' => 404,
            'content' => json_encode(['error' => 'user_not_found']),
        ];

        $jwt = $this->getToken();

        $actual = $this->get('restricted?cub_token='.$jwt);

        $this->assertEquals($expected['code'], $actual->getStatusCode());
        $this->assertEquals($expected['content'], $actual->getContent());
    }

    /** @test */
    public function request_with_non_existent_user_header()
    {
        User::whereCubId($this->details['id'])->first()->delete();

        $expected = [
            'code' => 404,
            'content' => json_encode(['error' => 'user_not_found']),
        ];

        $jwt = $this->getToken();

        $actual = $this->get('restricted', ['HTTP_Authorization' => 'Bearer '.$jwt]);

        $this->assertEquals($expected['code'], $actual->getStatusCode());
        $this->assertEquals($expected['content'], $actual->getContent());
    }

    /** @test */
    public function request_with_token_with_scope_claim()
    {
        $expected = [
            'code' => 401,
            'content' => json_encode(['error' => 'invalid_token']),
        ];

        $jwt = $this->getToken([
            'scope' => 'fail',
        ]);

        $actual = $this->get('restricted?cub_token='.$jwt);

        $this->assertEquals($expected['code'], $actual->getStatusCode());
        $this->assertEquals($expected['content'], $actual->getContent());
    }

    /** @test */
    public function request_with_token_with_scope_claim_header()
    {
        $expected = [
            'code' => 401,
            'content' => json_encode(['error' => 'invalid_token']),
        ];

        $jwt = $this->getToken([
            'scope' => 'wat',
        ]);

        $actual = $this->get('restricted', ['HTTP_Authorization' => 'Bearer '.$jwt]);

        $this->assertEquals($expected['code'], $actual->getStatusCode());
        $this->assertEquals($expected['content'], $actual->getContent());
    }

    /** @test */
    public function request_with_bad_token()
    {
        User::whereCubId($this->details['id'])->first()->delete();

        $expected = [
            'code' => 500,
            'content' => json_encode(['error' => 'error_processing_token']),
        ];

        $token = [
            'user' => $this->details['id'],
        ];
        $jwt = JWT::encode($token, '<PASSWORD>!');

        $actual = $this->get('restricted?cub_token='.$jwt);

        $this->assertEquals($expected['code'], $actual->getStatusCode());
        $this->assertEquals($expected['content'], $actual->getContent());
    }

    /** @test */
    public function request_with_bad_token_header()
    {
        User::whereCubId($this->details['id'])->first()->delete();

        $expected = [
            'code' => 500,
            'content' => json_encode(['error' => 'error_processing_token']),
        ];

        $token = [
            'user' => $this->details['id'],
        ];
        $jwt = JWT::encode($token, '<PASSWORD>!');

        $actual = $this->get('restricted', ['HTTP_Authorization' => 'Bearer '.$jwt]);

        $this->assertEquals($expected['code'], $actual->getStatusCode());
        $this->assertEquals($expected['content'], $actual->getContent());
    }
}
<commit_msg>Use cub login in middleware test<commit_after><?php namespace Cub\CubLaravel\Test;

use Cub;
use Cub\CubLaravel\Test\Models\User;
use Firebase\JWT\JWT;

class CubAuthMiddlewareTest extends CubLaravelTestCase
{
    /** @test */
    public function request_with_valid_token()
    {
        $expected = [
            'code' => 200,
            'content' => json_encode(['message' => 'Hello, Cub User '.$this->details['id']]),
        ];

        $login = Cub::login($this->credentials['username'], $this->credentials['password']);
        $jwt = $login->getToken();

        $actual = $this->get('restricted?cub_token='.$jwt);

        $this->assertEquals($expected['code'], $actual->getStatusCode());
        $this->assertEquals($expected['content'], $actual->getContent());
    }

    /** @test */
    public function request_with_valid_token_header()
    {
        $expected = [
            'code' => 200,
            'content' => json_encode(['message' => 'Hello, Cub User '.$this->details['id']]),
        ];

        $login = Cub::login($this->credentials['username'], $this->credentials['password']);
        $jwt = $login->getToken();

        $actual = $this->get('restricted', ['HTTP_Authorization' => 'Bearer '.$jwt]);

        $this->assertEquals($expected['code'], $actual->getStatusCode());
        $this->assertEquals($expected['content'], $actual->getContent());
    }

    /** @test */
    public function request_needs_token()
    {
        $expected = [
            'code' => 400,
            'content' => json_encode(['error' => 'token_not_provided']),
        ];

        $actual = $this->get('restricted');

        $this->assertEquals($expected['code'], $actual->getStatusCode());
        $this->assertEquals($expected['content'], $actual->getContent());
    }

    /** @test */
    public function request_with_expired_token()
    {
        $expected = [
            'code' => 401,
            'content' => json_encode(['error' => 'expired_token']),
        ];

        $jwt = $this->getToken([
            'exp' => time() - 5000,
        ]);

        $actual = $this->get('restricted?cub_token='.$jwt);

        $this->assertEquals($expected['code'], $actual->getStatusCode());
        $this->assertEquals($expected['content'], $actual->getContent());
    }

    /** @test */
    public function request_with_expired_token_header()
    {
        $expected = [
            'code' => 401,
            'content' => json_encode(['error' => 'expired_token']),
        ];

        $jwt = $this->getToken([
            'exp' => time() - 5000,
        ]);

        $actual = $this->get('restricted', ['HTTP_Authorization' => 'Bearer '.$jwt]);

        $this->assertEquals($expected['code'], $actual->getStatusCode());
        $this->assertEquals($expected['content'], $actual->getContent());
    }

    /** @test */
    public function request_with_token_not_yet_valid()
    {
        $expected = [
            'code' => 401,
            'content' => json_encode(['error' => 'token_not_yet_valid']),
        ];

        $jwt = $this->getToken([
            'nbf' => time() + 5000,
        ]);

        $actual = $this->get('restricted?cub_token='.$jwt);

        $this->assertEquals($expected['code'], $actual->getStatusCode());
        $this->assertEquals($expected['content'], $actual->getContent());
    }

    /** @test */
    public function request_with_token_not_yet_valid_header()
    {
        $expected = [
            'code' => 401,
            'content' => json_encode(['error' => 'token_not_yet_valid']),
        ];

        $jwt = $this->getToken([
            'nbf' => time() + 5000,
        ]);

        $actual = $this->get('restricted', ['HTTP_Authorization' => 'Bearer '.$jwt]);

        $this->assertEquals($expected['code'], $actual->getStatusCode());
        $this->assertEquals($expected['content'], $actual->getContent());
    }

    /** @test */
    public function request_with_non_existent_user()
    {
        User::whereCubId($this->details['id'])->first()->delete();

        $expected = [
            'code' => 404,
            'content' => json_encode(['error' => 'user_not_found']),
        ];

        $jwt = $this->getToken();

        $actual = $this->get('restricted?cub_token='.$jwt);

        $this->assertEquals($expected['code'], $actual->getStatusCode());
        $this->assertEquals($expected['content'], $actual->getContent());
    }

    /** @test */
    public function request_with_non_existent_user_header()
    {
        User::whereCubId($this->details['id'])->first()->delete();

        $expected = [
            'code' => 404,
            'content' => json_encode(['error' => 'user_not_found']),
        ];

        $jwt = $this->getToken();

        $actual = $this->get('restricted', ['HTTP_Authorization' => 'Bearer '.$jwt]);

        $this->assertEquals($expected['code'], $actual->getStatusCode());
        $this->assertEquals($expected['content'], $actual->getContent());
    }

    /** @test */
    public function request_with_token_with_scope_claim()
    {
        $expected = [
            'code' => 401,
            'content' => json_encode(['error' => 'invalid_token']),
        ];

        $jwt = $this->getToken([
            'scope' => 'fail',
        ]);

        $actual = $this->get('restricted?cub_token='.$jwt);

        $this->assertEquals($expected['code'], $actual->getStatusCode());
        $this->assertEquals($expected['content'], $actual->getContent());
    }

    /** @test */
    public function request_with_token_with_scope_claim_header()
    {
        $expected = [
            'code' => 401,
            'content' => json_encode(['error' => 'invalid_token']),
        ];

        $jwt = $this->getToken([
            'scope' => 'wat',
        ]);

        $actual = $this->get('restricted', ['HTTP_Authorization' => 'Bearer '.$jwt]);

        $this->assertEquals($expected['code'], $actual->getStatusCode());
        $this->assertEquals($expected['content'], $actual->getContent());
    }

    /** @test */
    public function request_with_bad_token()
    {
        User::whereCubId($this->details['id'])->first()->delete();

        $expected = [
            'code' => 500,
            'content' => json_encode(['error' => 'error_processing_token']),
        ];

        $token = [
            'user' => $this->details['id'],
        ];
        $jwt = JWT::encode($token, '<PASSWORD>!');

        $actual = $this->get('restricted?cub_token='.$jwt);

        $this->assertEquals($expected['code'], $actual->getStatusCode());
        $this->assertEquals($expected['content'], $actual->getContent());
    }

    /** @test */
    public function request_with_bad_token_header()
    {
        User::whereCubId($this->details['id'])->first()->delete();

        $expected = [
            'code' => 500,
            'content' => json_encode(['error' => 'error_processing_token']),
        ];

        $token = [
            'user' => $this->details['id'],
        ];
        $jwt = JWT::encode($token, '<PASSWORD>!');

        $actual = $this->get('restricted', ['HTTP_Authorization' => 'Bearer '.$jwt]);

        $this->assertEquals($expected['code'], $actual->getStatusCode());
        $this->assertEquals($expected['content'], $actual->getContent());
    }
}

{'fraction_non_alphanumeric': 0.20359365168146543, 'fraction_numerical': 0.007690694259945466, 'mean_word_length': 2.502448579823702, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 299, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3949797', 'n_tokens_mistral': 4391, 'n_tokens_neox': 4130, 'n_words': 605}