• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

daycry / auth / 16344800892

17 Jul 2025 12:13PM UTC coverage: 66.494% (+6.6%) from 59.854%
16344800892

push

github

daycry
Add comprehensive unit tests for helpers and services

This commit introduces extensive unit tests for helper functions (auth, checkEndpoint, checkIp, email), libraries (CheckIpInRange, Logger), and service classes (AttemptHandler, ExceptionHandler, RequestLogger). Also fixes ReflectionProperty usage in ExceptionHandler to correctly pass the exception object. These tests improve code coverage and ensure reliability of authentication, endpoint, IP checking, email, logging, and exception handling features.

1 of 1 new or added line in 1 file covered. (100.0%)

136 existing lines in 8 files now uncovered.

2177 of 3274 relevant lines covered (66.49%)

32.78 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

67.31
/src/Filters/AuthFilter.php
1
<?php
2

3
declare(strict_types=1);
4

5
/**
6
 * This file is part of Daycry Auth.
7
 *
8
 * (c) Daycry <daycry9@proton.me>
9
 *
10
 * For the full copyright and license information, please view
11
 * the LICENSE file that was distributed with this source code.
12
 */
13

14
namespace Daycry\Auth\Filters;
15

16
use CodeIgniter\Filters\FilterInterface;
17
use CodeIgniter\HTTP\IncomingRequest;
18
use CodeIgniter\HTTP\RequestInterface;
19
use CodeIgniter\HTTP\ResponseInterface;
20
use Config\Services;
21
use Daycry\Auth\Authentication\Authenticators\Session;
22
use Daycry\Auth\Config\Auth;
23
use Daycry\Auth\Interfaces\AuthenticatorInterface;
24
use Daycry\Auth\Result;
25

26
/**
27
 * Authentication Filter.
28
 *
29
 * JSON Web Token authentication for web applications
30
 * Access Token authentication for web applications
31
 */
32
class AuthFilter implements FilterInterface
33
{
34
    public function before(RequestInterface $request, $arguments = null)
35
    {
36
        helper('checkEndpoint');
9✔
37

38
        if (! $request instanceof IncomingRequest) {
9✔
39
            return;
×
40
        }
41

42
        $endpoint = checkEndpoint();
9✔
43

44
        $alias = $this->determineAuthenticator($arguments, $endpoint);
9✔
45

46
        /** @var AuthenticatorInterface $authenticator */
47
        $authenticator = auth($alias)->getAuthenticator();
9✔
48

49
        /** @var Auth $config */
50
        $config = config(Auth::class);
9✔
51

52
        if ($authenticator instanceof Session) {
9✔
53
            return $this->handleSessionAuthentication($authenticator, $config);
3✔
54
        }
55

56
        return $this->handleTokenAuthentication($authenticator);
6✔
57
    }
58

59
    /**
60
     * Determine which authenticator to use
61
     *
62
     * @param mixed $endpoint
63
     */
64
    private function determineAuthenticator(?array $arguments, $endpoint): string
65
    {
66
        $alias = $arguments ? $arguments[0] : service('settings')->get('Auth.defaultAuthenticator');
9✔
67

68
        return ($endpoint && $endpoint->auth) ? $endpoint->auth : $alias;
9✔
69
    }
70

71
    /**
72
     * Handle session-based authentication
73
     */
74
    private function handleSessionAuthentication(Session $authenticator, Auth $config)
75
    {
76
        if (auth()->loggedIn()) {
3✔
77
            if (setting('Auth.recordActiveDate')) {
2✔
78
                $authenticator->recordActiveDate();
2✔
79
            }
80

81
            $user = $authenticator->getUser();
2✔
82

83
            // Check if user is banned
84
            if ($user->isBanned()) {
2✔
85
                $error = $user->getBanMessage() ?? lang('Auth.logOutBannedUser');
1✔
86
                $authenticator->logout();
1✔
87

88
                return redirect()->to($config->logoutRedirect())
1✔
89
                    ->with('error', $error);
1✔
90
            }
91

92
            // Check if user needs activation
93
            if ($user !== null && ! $user->isActivated()) {
1✔
UNCOV
94
                $hasAction = $authenticator->startUpAction('register', $user);
×
UNCOV
95
                if ($hasAction) {
×
UNCOV
96
                    return redirect()->route('auth-action-show')
×
UNCOV
97
                        ->with('error', lang('Auth.activationBlocked'));
×
98
                }
99
            }
100

101
            return;
1✔
102
        }
103

104
        // Handle pending actions
105
        if ($authenticator->isPending()) {
1✔
UNCOV
106
            return redirect()->route('auth-action-show')
×
UNCOV
107
                ->with('error', $authenticator->getPendingMessage());
×
108
        }
109

110
        // Save current URL for redirect after login
111
        if (uri_string() !== route_to('login')) {
1✔
112
            session()->setTempdata('beforeLoginUrl', current_url(), 300);
1✔
113
        }
114

115
        return redirect()->route('login');
1✔
116
    }
117

118
    /**
119
     * Handle token-based authentication
120
     */
121
    private function handleTokenAuthentication(AuthenticatorInterface $authenticator)
122
    {
123
        $result = $authenticator->attempt();
6✔
124

125
        if (! $result->isOK()) {
6✔
126
            return service('response')
4✔
127
                ->setStatusCode(ResponseInterface::HTTP_UNAUTHORIZED)
4✔
128
                ->setJson(['message' => $result->reason()]);
4✔
129
        }
130

131
        if (setting('Auth.recordActiveDate')) {
2✔
132
            $authenticator->recordActiveDate();
2✔
133
        }
134

135
        // Handle additional access token validation if enabled
136
        if (service('settings')->get('Auth.accessTokenEnabled')) {
2✔
UNCOV
137
            return $this->validateAccessToken();
×
138
        }
139
    }
140

141
    /**
142
     * Validate access token when enabled
143
     */
144
    private function validateAccessToken()
145
    {
UNCOV
146
        $accessToken = (Services::auth(false))->setAuthenticator('access_token')->attempt();
×
147

UNCOV
148
        if (! $accessToken->isOK() && service('settings')->get('Auth.strictApiAndAuth')) {
×
UNCOV
149
            return service('response')
×
UNCOV
150
                ->setStatusCode(ResponseInterface::HTTP_UNAUTHORIZED)
×
UNCOV
151
                ->setJson([
×
UNCOV
152
                    'message' => ($accessToken instanceof Result)
×
UNCOV
153
                        ? $accessToken->reason()
×
UNCOV
154
                        : lang('Auth.badToken'),
×
UNCOV
155
                ]);
×
156
        }
157
    }
158

159
    /**
160
     * We don't have anything to do here.
161
     *
162
     * @param array|null $arguments
163
     */
164
    public function after(RequestInterface $request, ResponseInterface $response, $arguments = null): void
165
    {
166
        // Nothing required
167
    }
3✔
168
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc