Skip to content

Commit 6d20882

Browse files
committed
Add cy.currentUser() command
1 parent 2b18e29 commit 6d20882

File tree

5 files changed

+62
-9
lines changed

5 files changed

+62
-9
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,24 @@ test('authenticated users can see the dashboard', () => {
112112
});
113113
```
114114

115+
### cy.currentUser()
116+
117+
Fetch the currently authenticated user object from the server, if any. Equivalent to Laravel's `auth()->user()`.
118+
119+
```js
120+
test('authenticated users can see the dashboard', () => {
121+
cy.login({ email: 'joe@example.com' });
122+
123+
cy.currentUser().its('email').should('eq', 'joe@example.com');
124+
125+
// or...
126+
127+
cy.currentUser().then(user => {
128+
expect(user.email).to.eql('joe@example.com');
129+
});
130+
});
131+
```
132+
115133

116134
### cy.logout()
117135

src/Controllers/CypressController.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ public function login(Request $request)
5757
});
5858
}
5959

60+
public function currentUser()
61+
{
62+
return auth()->user()?->setHidden([])->setVisible([]);
63+
}
64+
6065
public function logout()
6166
{
6267
auth()->logout();

src/routes/cypress.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@
1010
Route::post('/__cypress__/run-php', [CypressController::class, 'runPhp'])->name('cypress.run-php');
1111
Route::get('/__cypress__/csrf_token', [CypressController::class, 'csrfToken'])->name('cypress.csrf-token');
1212
Route::post('/__cypress__/routes', [CypressController::class, 'routes'])->name('cypress.routes');
13+
Route::post('/__cypress__/current-user', [CypressController::class, 'currentUser'])->name('cypress.current-user');

src/stubs/support/laravel-commands.js

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,32 @@ Cypress.Commands.add('login', (attributes = {}) => {
3333
.its('body', { log: false });
3434
});
3535

36+
/**
37+
* Fetch the currently authenticated user object.
38+
*
39+
* @example cy.currentUser();
40+
*/
41+
Cypress.Commands.add('currentUser', () => {
42+
return cy.csrfToken().then((token) => {
43+
return cy
44+
.request({
45+
method: 'POST',
46+
url: '/__cypress__/current-user',
47+
body: { _token: token },
48+
log: false,
49+
})
50+
.then((response) => {
51+
if (!response.body) {
52+
cy.log('No authenticated user found.');
53+
}
54+
55+
Cypress.Laravel.currentUser = response?.body;
56+
57+
return response?.body;
58+
});
59+
});
60+
});
61+
3662

3763
/**
3864
* Logout the current user.
@@ -55,7 +81,6 @@ Cypress.Commands.add('logout', () => {
5581
});
5682
});
5783

58-
5984
/**
6085
* Fetch a CSRF token.
6186
*
@@ -71,7 +96,6 @@ Cypress.Commands.add('csrfToken', () => {
7196
.its('body', { log: false });
7297
});
7398

74-
7599
/**
76100
* Fetch and store all named routes.
77101
*
@@ -97,7 +121,6 @@ Cypress.Commands.add('refreshRoutes', () => {
97121
});
98122
});
99123

100-
101124
/**
102125
* Visit the given URL or route.
103126
*
@@ -116,7 +139,6 @@ Cypress.Commands.overwrite('visit', (originalFn, subject, options) => {
116139
return originalFn(subject, options);
117140
});
118141

119-
120142
/**
121143
* Create a new Eloquent factory.
122144
*
@@ -180,7 +202,6 @@ Cypress.Commands.add('create', (model, count = 1, attributes = {}, load = [], st
180202
.its('body', { log: false });
181203
});
182204

183-
184205
/**
185206
* Refresh the database state.
186207
*
@@ -193,7 +214,6 @@ Cypress.Commands.add('refreshDatabase', (options = {}) => {
193214
return cy.artisan('migrate:fresh', options);
194215
});
195216

196-
197217
/**
198218
* Seed the database.
199219
*
@@ -208,7 +228,6 @@ Cypress.Commands.add('seed', (seederClass) => {
208228
});
209229
});
210230

211-
212231
/**
213232
* Trigger an Artisan command.
214233
*
@@ -247,7 +266,6 @@ Cypress.Commands.add('artisan', (command, parameters = {}, options = {}) => {
247266
});
248267
});
249268

250-
251269
/**
252270
* Execute arbitrary PHP.
253271
*

tests/CypressControllerTest.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,17 @@ public function it_logs_a_new_user_in()
5151
$this->assertTrue(auth()->check());
5252
}
5353

54+
/** @test */
55+
public function it_fetches_the_currently_authenticated_user()
56+
{
57+
$this->post(route('cypress.login'), ['attributes' => ['email' => 'joe@example.com']]);
58+
59+
$response = $this->post(route('cypress.current-user'));
60+
61+
$this->assertNotNull($response->json());
62+
$this->assertEquals('joe@example.com', $response->json()['email']);
63+
}
64+
5465
/** @test */
5566
public function it_makes_all_logged_in_user_attributes_visible()
5667
{
@@ -66,7 +77,7 @@ public function it_makes_all_logged_in_user_attributes_visible()
6677
/** @test */
6778
public function it_logs_a_user_in()
6879
{
69-
$response = $this->post(route('cypress.login'), [
80+
$this->post(route('cypress.login'), [
7081
'attributes' => ['name' => 'Frank'],
7182
'state' => ['guest']
7283
]);

0 commit comments

Comments
 (0)