Skip to content

Commit

Permalink
fix: update a QueryBuilder.returning call because of the lucid brea…
Browse files Browse the repository at this point in the history
…king change (#196)

* fix: update a `QueryBuilder.returning` call because of the lucid breaking changes

* fix: make it work for sqlite too

* fix: create posix path on `setupApplication` sqlite filename for windows users

* refactor: migrate to new japa version

* test: close pending redis connection
  • Loading branch information
Julien-R44 authored Mar 3, 2022
1 parent b6117c1 commit e734a49
Show file tree
Hide file tree
Showing 16 changed files with 245 additions and 192 deletions.
7 changes: 7 additions & 0 deletions bin/japaTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Assert } from '@japa/assert'

declare module '@japa/runner' {
interface TestContext {
assert: Assert
}
}
38 changes: 38 additions & 0 deletions bin/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { assert } from '@japa/assert'
import { specReporter } from '@japa/spec-reporter'
import { runFailedTests } from '@japa/run-failed-tests'
import { processCliArgs, configure, run } from '@japa/runner'
import 'reflect-metadata'

/*
|--------------------------------------------------------------------------
| Configure tests
|--------------------------------------------------------------------------
|
| The configure method accepts the configuration to configure the Japa
| tests runner.
|
| The first method call "processCliArgs" process the command line arguments
| and turns them into a config object. Using this method is not mandatory.
|
| Please consult japa.dev/runner-config for the config docs.
*/
configure({
...processCliArgs(process.argv.slice(2)),
...{
files: ['test/**/*.spec.ts'],
plugins: [assert(), runFailedTests()],
reporters: [specReporter()],
importer: (filePath: string) => import(filePath),
},
})

/*
|--------------------------------------------------------------------------
| Run tests
|--------------------------------------------------------------------------
|
| The following "run" method is required to execute all the tests.
|
*/
run()
7 changes: 0 additions & 7 deletions japaFile.js

This file was deleted.

9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
"scripts": {
"mrm": "mrm --preset=@adonisjs/mrm-preset",
"pretest": "npm run lint",
"test": "node japaFile.js",
"clean": "del build",
"test": "node -r @adonisjs/require-ts/build/register ./bin/test.ts",
"clean": "del-cli build",
"copyfiles": "copyfiles \"templates/**/*.txt\" build",
"compile": "npm run lint && npm run clean && tsc",
"build": "npm run compile && npm run copyfiles",
Expand Down Expand Up @@ -55,6 +55,10 @@
"@adonisjs/require-ts": "^2.0.10",
"@adonisjs/session": "^6.1.4",
"@adonisjs/sink": "^5.2.2",
"@japa/assert": "^1.2.3",
"@japa/run-failed-tests": "^1.0.3",
"@japa/runner": "^1.2.0",
"@japa/spec-reporter": "^1.1.7",
"@poppinss/dev-utils": "^2.0.2",
"@types/node": "^17.0.21",
"@types/supertest": "^2.0.11",
Expand All @@ -67,7 +71,6 @@
"eslint-plugin-prettier": "^4.0.0",
"github-label-sync": "^2.0.2",
"husky": "^7.0.4",
"japa": "^4.0.0",
"mrm": "^3.0.10",
"np": "^7.6.0",
"phc-bcrypt": "^1.0.7",
Expand Down
5 changes: 3 additions & 2 deletions src/TokenProviders/Database/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,9 @@ export class TokenDatabaseProvider implements TokenProviderContract {
...token.meta,
}

const [persistedToken] = await client.table(this.config.table).insert(payload).returning('id')
return String(persistedToken)
const [row] = await client.table(this.config.table).insert(payload).returning('id')

return String(typeof row === 'number' ? row : row.id)
}

/**
Expand Down
6 changes: 3 additions & 3 deletions test-helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/

import 'reflect-metadata'
import { join } from 'path'
import { join, sep, posix } from 'path'
import { MarkOptional } from 'ts-essentials'
import { Filesystem } from '@poppinss/dev-utils'
import { LucidModel } from '@ioc:Adonis/Lucid/Orm'
Expand Down Expand Up @@ -100,13 +100,13 @@ export async function setupApplication(
primary: {
client: 'sqlite3',
connection: {
filename: '${join(fs.basePath, 'primary.sqlite3')}',
filename: '${join(fs.basePath, 'primary.sqlite3').split(sep).join(posix.sep)}',
},
},
secondary: {
client: 'sqlite3',
connection: {
filename: '${join(fs.basePath, 'secondary.sqlite3')}',
filename: '${join(fs.basePath, 'secondary.sqlite3').split(sep).join(posix.sep)}',
},
},
}
Expand Down
22 changes: 11 additions & 11 deletions test/auth-manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* file that was distributed with this source code.
*/

import test from 'japa'
import { test } from '@japa/runner'
import 'reflect-metadata'
import { UserProviderContract } from '@ioc:Adonis/Addons/Auth'
import { ApplicationContract } from '@ioc:Adonis/Core/Application'
Expand All @@ -33,20 +33,20 @@ import {
let app: ApplicationContract

test.group('Auth Manager', (group) => {
group.before(async () => {
group.setup(async () => {
app = await setupApplication()
await setup(app)
})

group.after(async () => {
group.teardown(async () => {
await cleanup(app)
})

group.afterEach(async () => {
group.each.teardown(async () => {
await reset(app)
})

test('make an instance of the session guard with lucid provider', (assert) => {
test('make an instance of the session guard with lucid provider', ({ assert }) => {
const User = getUserModel(app.container.use('Adonis/Lucid/Orm').BaseModel)

const manager = new AuthManager(app, {
Expand Down Expand Up @@ -82,7 +82,7 @@ test.group('Auth Manager', (group) => {
assert.instanceOf(mapping.provider, LucidProvider)
})

test('make an instance of the session guard with database provider', (assert) => {
test('make an instance of the session guard with database provider', ({ assert }) => {
const User = getUserModel(app.container.use('Adonis/Lucid/Orm').BaseModel)

const manager = new AuthManager(app, {
Expand Down Expand Up @@ -118,7 +118,7 @@ test.group('Auth Manager', (group) => {
assert.instanceOf(mapping.provider, DatabaseProvider)
})

test('make an instance of auth class for a given http request', (assert) => {
test('make an instance of auth class for a given http request', ({ assert }) => {
const User = getUserModel(app.container.use('Adonis/Lucid/Orm').BaseModel)

const manager = new AuthManager(app, {
Expand Down Expand Up @@ -153,7 +153,7 @@ test.group('Auth Manager', (group) => {
assert.instanceOf(auth, Auth)
})

test('extend by adding custom provider', (assert) => {
test('extend by adding custom provider', ({ assert }) => {
class MongoDBProvider implements UserProviderContract<any> {
constructor(config: any) {
assert.deepEqual(config, { driver: 'mongodb' })
Expand Down Expand Up @@ -189,7 +189,7 @@ test.group('Auth Manager', (group) => {
assert.instanceOf(manager.makeMapping(ctx, 'admin' as any).provider, MongoDBProvider)
})

test('extend by adding custom guard', (assert) => {
test('extend by adding custom guard', ({ assert }) => {
class MongoDBProvider implements UserProviderContract<any> {
constructor(config: any) {
assert.deepEqual(config, { driver: 'mongodb' })
Expand Down Expand Up @@ -236,7 +236,7 @@ test.group('Auth Manager', (group) => {
assert.instanceOf(manager.makeMapping(ctx, 'admin' as any).provider, MongoDBProvider)
})

test('make an instance of the oat guard with lucid provider', (assert) => {
test('make an instance of the oat guard with lucid provider', ({ assert }) => {
const User = getUserModel(app.container.use('Adonis/Lucid/Orm').BaseModel)

const manager = new AuthManager(app, {
Expand Down Expand Up @@ -272,7 +272,7 @@ test.group('Auth Manager', (group) => {
assert.instanceOf(mapping.provider, LucidProvider)
})

test('make an instance of the basic auth guard with lucid provider', (assert) => {
test('make an instance of the basic auth guard with lucid provider', ({ assert }) => {
const User = getUserModel(app.container.use('Adonis/Lucid/Orm').BaseModel)

const manager = new AuthManager(app, {
Expand Down
8 changes: 4 additions & 4 deletions test/auth-provider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@
* file that was distributed with this source code.
*/

import test from 'japa'
import { test } from '@japa/runner'
import { fs, setupApplication } from '../test-helpers'
import { AuthManager } from '../src/AuthManager'

test.group('Auth Provider', (group) => {
group.afterEach(async () => {
group.each.teardown(async () => {
await fs.cleanup()
})

test('register auth provider', async (assert) => {
test('register auth provider', async ({ assert }) => {
const app = await setupApplication(['../../providers/AuthProvider'])
assert.instanceOf(app.container.use('Adonis/Addons/Auth'), AuthManager)
})

test('define auth property on http context', async (assert) => {
test('define auth property on http context', async ({ assert }) => {
const app = await setupApplication(['../../providers/AuthProvider'])
assert.isTrue(app.container.use('Adonis/Core/HttpContext')['hasGetter']('auth'))
})
Expand Down
24 changes: 12 additions & 12 deletions test/auth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* file that was distributed with this source code.
*/

import test from 'japa'
import { test } from '@japa/runner'
import 'reflect-metadata'
import { ApplicationContract } from '@ioc:Adonis/Core/Application'

Expand All @@ -33,20 +33,20 @@ import {
let app: ApplicationContract

test.group('Auth', (group) => {
group.before(async () => {
group.setup(async () => {
app = await setupApplication()
await setup(app)
})

group.after(async () => {
group.teardown(async () => {
await cleanup(app)
})

group.afterEach(async () => {
group.each.teardown(async () => {
await reset(app)
})

test('make and cache instance of the session guard', (assert) => {
test('make and cache instance of the session guard', ({ assert }) => {
const User = getUserModel(app.container.use('Adonis/Lucid/Orm').BaseModel)

const manager = new AuthManager(app, {
Expand Down Expand Up @@ -85,7 +85,7 @@ test.group('Auth', (group) => {
assert.instanceOf(mapping.provider, LucidProvider)
})

test('proxy all methods to the default driver', async (assert) => {
test('proxy all methods to the default driver', async ({ assert }) => {
const User = getUserModel(app.container.use('Adonis/Lucid/Orm').BaseModel)

const manager = new AuthManager(app, {
Expand Down Expand Up @@ -212,7 +212,7 @@ test.group('Auth', (group) => {
assert.isTrue(auth.authenticationAttempted)
})

test('update default guard', (assert) => {
test('update default guard', ({ assert }) => {
const User = getUserModel(app.container.use('Adonis/Lucid/Orm').BaseModel)

const manager = new AuthManager(app, {
Expand Down Expand Up @@ -249,7 +249,7 @@ test.group('Auth', (group) => {
assert.instanceOf(auth.provider, DatabaseProvider)
})

test('serialize toJSON', (assert) => {
test('serialize toJSON', ({ assert }) => {
const User = getUserModel(app.container.use('Adonis/Lucid/Orm').BaseModel)

const manager = new AuthManager(app, {
Expand Down Expand Up @@ -298,7 +298,7 @@ test.group('Auth', (group) => {
})
})

test('make oat guard', (assert) => {
test('make oat guard', ({ assert }) => {
const User = getUserModel(app.container.use('Adonis/Lucid/Orm').BaseModel)

const manager = new AuthManager(app, {
Expand Down Expand Up @@ -335,7 +335,7 @@ test.group('Auth', (group) => {
assert.instanceOf(auth.tokenProvider, TokenDatabaseProvider)
})

test('make oat guard with redis driver', (assert) => {
test('make oat guard with redis driver', ({ assert }) => {
const User = getUserModel(app.container.use('Adonis/Lucid/Orm').BaseModel)

const manager = new AuthManager(app, {
Expand Down Expand Up @@ -372,7 +372,7 @@ test.group('Auth', (group) => {
assert.instanceOf(auth.tokenProvider, TokenRedisProvider)
})

test('return user_id when foreignKey is missing', (assert) => {
test('return user_id when foreignKey is missing', ({ assert }) => {
const User = getUserModel(app.container.use('Adonis/Lucid/Orm').BaseModel)

const manager = new AuthManager(app, {
Expand Down Expand Up @@ -408,7 +408,7 @@ test.group('Auth', (group) => {
assert.equal(auth.tokenProvider.foreignKey, 'user_id')
})

test('return the foreignKey when not missing', (assert) => {
test('return the foreignKey when not missing', ({ assert }) => {
const User = getUserModel(app.container.use('Adonis/Lucid/Orm').BaseModel)

const manager = new AuthManager(app, {
Expand Down
Loading

0 comments on commit e734a49

Please sign in to comment.