question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question] How to act as logged user in tests?

See original GitHub issue

Describe your question I’m using sessionAttribute and AccessManager for authentication / authorization. I do like writing acceptance tests on REST API level. Question: Is there any common pattern to act as a logged user in tests?

Currently, I added an optional endpoint (added only in tests) like:

if (testMode) {
    app.post("/test-login", ctx -> {
        ctx.sessionAttribute(PRINCIPAL_SESSION_KEY, ctx.bodyAsClass(Principal.class));
    });
}

// ..
public record Principal(UUID userId, String username, Set<Role> roles) implements Serializable {
}

and then in tests I’m using this endpoint before tested endpoint, but that’s quite verbose (?)

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
tipsycommented, Jun 30, 2022

Another way is to perform a check against an environment variable, or check for localhost, inside the AccessManager:

config.accessManager { ...
    if (evn == local) ctx.sessionAttribute(PRINCIPAL_SESSION_KEY, ctx.bodyAsClass(Principal.class));
    // normal access manager code
}
0reactions
tipsycommented, Jul 5, 2022

A tutorial would be cool !

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Write Test Cases For a Login Page (Sample Scenarios)
1, Verify if a user will be able to login with a valid username and valid password. Positive ; 2, Verify if a...
Read more >
How to test the current logged in user in test class?
and then go into your IDE or Developer Console and ask to run the test, you would be the running user. UserInfo.
Read more >
How do i get logged in user in another test case in laravel ...
I dont want to create user in testLoginFalse function, because user already in database and it's logged in by testLoginTrue function. – Hardik ......
Read more >
Laravel unit testing issue with logged user - Laracasts
I need to test the login form and the users menu. Unfortunately the be() method does not logged me in. Here is my...
Read more >
Interview Questions: How to Test a Login Form- 110 Test Cases
Test Cases · 1. Login with correct email and password, remember me = NO · 2. Login with correct username and password, remember...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found