Skip to content

Commit e0f6c07

Browse files
authored
feat: poc integration tests for identities management express.js samples (#2389)
* feat: poc integration tests for identities management express.js samples * refactor: remove unnecessary blank lines in identity login and signup tests * test: add integration tests for identity logout and login helpers * fix: remove unnecessary fallback for location header in identity login and signup tests * refactor: update routes to use specific paths and simplify server setup * refactor: improve logout route registration in server setup * refactor: streamline server setup by organizing middleware and route registrations
1 parent 3581575 commit e0f6c07

File tree

22 files changed

+1396
-52
lines changed

22 files changed

+1396
-52
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: JavaScript Integration Tests
2+
3+
on:
4+
workflow_dispatch:
5+
push:
6+
branches:
7+
- master
8+
pull_request:
9+
branches:
10+
- master
11+
12+
jobs:
13+
test-javascript-integration:
14+
timeout-minutes: 10
15+
runs-on: ubuntu-latest
16+
steps:
17+
- uses: actions/checkout@v4
18+
- uses: actions/setup-node@v4
19+
with:
20+
node-version: "20.x"
21+
cache: "npm"
22+
- name: Install dependencies
23+
run: npm ci
24+
- name: Install test app dependencies
25+
run: |
26+
cd tests/jest/apps/identity-get-started
27+
npm ci
28+
- name: Run JavaScript integration tests
29+
run: npm run test:integration
30+
env:
31+
ORY_SDK_URL: ${{ secrets.ORY_SDK_URL }}
32+
TEST_USER_EMAIL: ${{ secrets.TEST_USER_EMAIL }}
33+
TEST_USER_PASSWORD: ${{ secrets.TEST_USER_PASSWORD }}
Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
app.get("/", (req, res) => {
2-
ory
3-
.toSession({ cookie: req.header("cookie") })
4-
.then((data) => res.json(data))
5-
.catch(() =>
6-
res.redirect(`${process.env.ORY_SDK_URL}/self-service/login/browser`),
7-
)
8-
})
1+
export const registerLoginRoute = (
2+
app,
3+
ory,
4+
baseUrl = process.env.ORY_SDK_URL,
5+
) => {
6+
app.get("/login", (req, res) => {
7+
ory
8+
.toSession({ cookie: req.header("cookie") })
9+
.then((data) => res.json(data))
10+
.catch(() => res.redirect(`${baseUrl}/self-service/login/browser`))
11+
})
12+
}
Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
// Create logout route
2-
app.get("/logout", async (req, res) => {
3-
try {
4-
// Create a logout flow
5-
const { logout_url } = await ory.createBrowserLogoutFlow({
6-
cookie: req.header("cookie"),
7-
})
8-
// Redirect to logout URL
9-
res.redirect(logout_url)
10-
} catch (err) {
11-
res.redirect("/")
12-
}
13-
})
2+
export const registerLogoutRoute = (app, ory) => {
3+
app.get("/logout", async (req, res) => {
4+
try {
5+
// Create a logout flow
6+
const { data } = await ory.createBrowserLogoutFlow({
7+
cookie: req.header("cookie"),
8+
})
9+
const logoutUrl = data.logout_url || data.logoutUrl
10+
// Redirect to logout URL
11+
res.redirect(logoutUrl)
12+
} catch (err) {
13+
res.redirect("/")
14+
}
15+
})
16+
}
Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1-
const requireAuth = async (req, res, next) => {
2-
try {
3-
const session = await ory.toSession({ cookie: req.header("cookie") })
4-
req.session = session
5-
next()
6-
} catch (error) {
7-
res.redirect(`${process.env.ORY_SDK_URL}/self-service/login/browser`)
1+
export const createRequireAuth = (ory, baseUrl = process.env.ORY_SDK_URL) => {
2+
return async (req, res, next) => {
3+
try {
4+
const { data: session } = await ory.toSession({
5+
cookie: req.header("cookie"),
6+
})
7+
req.session = session
8+
next()
9+
} catch (error) {
10+
res.redirect(`${baseUrl}/self-service/login/browser`)
11+
}
812
}
913
}
1014

11-
app.get("/", requireAuth, (req, res) => {
12-
res.json(req.session.identity.traits) // { email: 'newtestuser@gmail.com' }
13-
})
15+
export const registerSessionRoute = (app, requireAuth) => {
16+
app.get("/session", requireAuth, (req, res) => {
17+
res.json(req.session.identity.traits) // { email: 'newtestuser@gmail.com' }
18+
})
19+
}
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
app.get("/refresh-session", async (req, res) => {
2-
// Redirect to login with refresh=true parameter
3-
res.redirect(`${baseUrl}/ui/login?refresh=true`)
4-
})
1+
export const registerRefreshSessionRoute = (app, baseUrl) => {
2+
app.get("/refresh-session", async (req, res) => {
3+
// Redirect to login with refresh=true parameter
4+
res.redirect(`${baseUrl}/ui/login?refresh=true`)
5+
})
6+
}

docs/identities/get-started/_common/code-examples/js/setupDev.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { Configuration, FrontendApi } from "@ory/client-fetch"
22

3-
const baseUrl = process.env.ORY_SDK_URL || "http://localhost:4000"
4-
const ory = new FrontendApi(
3+
export const baseUrl = process.env.ORY_SDK_URL || "http://localhost:4000"
4+
5+
export const ory = new FrontendApi(
56
new Configuration({
67
basePath: baseUrl,
78
}),
Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
app.get("/", (req, res) => {
2-
ory
3-
.toSession({ cookie: req.header("cookie") })
4-
.then((data) => res.json(data))
5-
.catch(() =>
6-
res.redirect(
7-
`${process.env.ORY_SDK_URL}/self-service/registration/browser`,
8-
),
9-
)
10-
})
1+
export const registerSignUpRoute = (app, ory, baseUrl) => {
2+
app.get("/signup", (req, res) => {
3+
ory
4+
.toSession({ cookie: req.header("cookie") })
5+
.then((data) => res.json(data))
6+
.catch(() => res.redirect(`${baseUrl}/self-service/registration/browser`))
7+
})
8+
}

docs/identities/get-started/setup.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ npm install @ory/client-fetch --save
3838

3939
```mdx-code-block
4040
</TabItem>
41-
<TabItem value="nextjs">
41+
<TabItem value="ts-nextjs">
4242
```
4343

4444
```shell

docs/identities/get-started/sign-in.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ to the registration page, it redirects to the login page.
3535
```
3636

3737
```mdx-code-block
38-
<CodeBlock className="language-js">{nextjsLogin}</CodeBlock>
38+
<CodeBlock className="language-tsx">{nextjsLogin}</CodeBlock>
3939
```
4040

4141
```mdx-code-block

docs/identities/get-started/sign-up.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ the Ory registration page.
4343
```
4444

4545
```mdx-code-block
46-
<CodeBlock className="language-js">{nextSignUp}</CodeBlock>
46+
<CodeBlock className="language-tsx">{nextSignUp}</CodeBlock>
4747
```
4848

4949
```mdx-code-block

0 commit comments

Comments
 (0)