Skip to content

Commit

Permalink
test: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Jul 17, 2023
1 parent 72f7ebc commit dee4752
Show file tree
Hide file tree
Showing 10 changed files with 332 additions and 23 deletions.
6 changes: 4 additions & 2 deletions bin/test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { assert } from '@japa/assert'
import { expectTypeOf } from '@japa/expect-type'
import { fileSystem } from '@japa/file-system'
import { processCLIArgs, configure, run } from '@japa/runner'

/*
Expand All @@ -16,8 +18,8 @@ import { processCLIArgs, configure, run } from '@japa/runner'
*/
processCLIArgs(process.argv.slice(2))
configure({
files: ['test/**/*.spec.ts'],
plugins: [assert()],
files: ['tests/**/*.spec.ts'],
plugins: [assert(), expectTypeOf(), fileSystem()],
})

/*
Expand Down
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"release": "np",
"version": "npm run build",
"sync-labels": "github-label-sync --labels .github/labels.json adonisjs/ally",
"quick:test": "node --loader=ts-node/esm bin/test.ts"
"quick:test": "node --enable-source-maps --loader=ts-node/esm bin/test.ts"
},
"keywords": [
"adonis",
Expand All @@ -54,6 +54,7 @@
"@commitlint/cli": "^17.6.6",
"@commitlint/config-conventional": "^17.6.6",
"@japa/assert": "^2.0.0-1",
"@japa/expect-type": "^2.0.0-0",
"@japa/runner": "^3.0.0-6",
"@swc/core": "^1.3.69",
"@types/node": "^20.4.2",
Expand All @@ -71,6 +72,7 @@
"typescript": "^5.1.6"
},
"dependencies": {
"@japa/file-system": "^2.0.0-1",
"@poppinss/oauth-client": "^5.1.0-3",
"@poppinss/utils": "^6.5.0-3"
},
Expand Down Expand Up @@ -107,7 +109,10 @@
"html"
],
"exclude": [
"tests/**"
"tests/**",
"src/drivers/**",
"src/abstract_drivers/**",
"stubs/**"
]
}
}
5 changes: 4 additions & 1 deletion src/ally_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ export class AllyManager<KnownSocialProviders extends Record<string, AllyManager
)
}

return driver(this.#ctx) as ReturnType<KnownSocialProviders[SocialProvider]>
const driverInstance = driver(this.#ctx) as ReturnType<KnownSocialProviders[SocialProvider]>
this.#driversCache.set(provider, driverInstance)

return driverInstance
}
}
31 changes: 20 additions & 11 deletions src/redirect_request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ export class RedirectRequest<Scopes extends string> extends UrlBuilder {
this.#scopeSeparator = scopeSeparator
}

/**
* Register a custom function to transform scopes. Exposed for drivers
* to implement.
*/
transformScopes(callback: (scopes: LiteralStringUnion<Scopes>[]) => string[]): this {
this.#scopesTransformer = callback
return this
Expand All @@ -41,14 +45,6 @@ export class RedirectRequest<Scopes extends string> extends UrlBuilder {
return this
}

/**
* Clear existing scopes
*/
clearScopes(): this {
this.clearParam(this.#scopeParamName)
return this
}

/**
* Merge to existing scopes
*/
Expand All @@ -57,10 +53,23 @@ export class RedirectRequest<Scopes extends string> extends UrlBuilder {
scopes = this.#scopesTransformer(scopes)
}

const params = this.getParams()
const mergedScopes = (params[this.#scopeParamName] || []).concat(scopes)
this.scopes(mergedScopes)
const existingScopes = this.getParams()[this.#scopeParamName]
const scopesString = scopes.join(this.#scopeSeparator)

if (!existingScopes) {
this.param(this.#scopeParamName, scopesString)
return this
}

this.param(this.#scopeParamName, `${existingScopes}${this.#scopeSeparator}${scopesString}`)
return this
}

/**
* Clear existing scopes
*/
clearScopes(): this {
this.clearParam(this.#scopeParamName)
return this
}
}
7 changes: 0 additions & 7 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,6 @@ export type DiscordToken = {
* Extra options available for Discord
*/
export type DiscordDriverConfig = Oauth2ClientConfig & {
driver: 'discord'
userInfoUrl?: string
scopes?: LiteralStringUnion<DiscordScopes>[]
prompt?: 'consent' | 'none'
Expand Down Expand Up @@ -278,7 +277,6 @@ export type GithubToken = {
* Extra options available for Github
*/
export type GithubDriverConfig = Oauth2ClientConfig & {
driver: 'github'
login?: string
scopes?: LiteralStringUnion<GithubScopes>[]
allowSignup?: boolean
Expand Down Expand Up @@ -310,7 +308,6 @@ export type TwitterToken = {
* Extra options available for twitter
*/
export type TwitterDriverConfig = Oauth1ClientConfig & {
driver: 'twitter'
userInfoUrl?: string
}

Expand Down Expand Up @@ -383,7 +380,6 @@ export type GoogleToken = Oauth2AccessToken & {
* https://developers.google.com/identity/protocols/oauth2/openid-connect#re-consent
*/
export type GoogleDriverConfig = Oauth2ClientConfig & {
driver: 'google'
userInfoUrl?: string

/**
Expand Down Expand Up @@ -436,7 +432,6 @@ export type LinkedInToken = {
* https://docs.microsoft.com/en-us/linkedin/shared/authentication/authorization-code-flow?context=linkedin%2Fcontext&tabs=HTTPS#step-2-request-an-authorization-code
*/
export type LinkedInDriverConfig = Oauth2ClientConfig & {
driver: 'linkedin'
userInfoUrl?: string
userEmailUrl?: string

Expand Down Expand Up @@ -534,7 +529,6 @@ export type FacebookToken = {
* https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow
*/
export type FacebookDriverConfig = Oauth2ClientConfig & {
driver: 'facebook'
userInfoUrl?: string

/**
Expand Down Expand Up @@ -596,7 +590,6 @@ export type SpotifyToken = {
* Extra options available for Spotify
*/
export type SpotifyDriverConfig = Oauth2ClientConfig & {
driver: 'spotify'
scopes?: LiteralStringUnion<SpotifyScopes>[]
showDialog?: boolean
}
Expand Down
47 changes: 47 additions & 0 deletions tests/ally_manager.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* @adonisjs/ally
*
* (c) AdonisJS
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

import { test } from '@japa/runner'
import { HttpContextFactory } from '@adonisjs/core/factories/http'

import { AllyManager } from '../src/ally_manager.js'
import { GithubDriver } from '../src/drivers/github.js'

test.group('Ally manager', () => {
test('create an instance of a driver', ({ assert, expectTypeOf }) => {
const ctx = new HttpContextFactory().create()

const ally = new AllyManager(
{
github: ($ctx) => {
return new GithubDriver($ctx, {
clientId: '',
clientSecret: '',
callbackUrl: '',
})
},
},
ctx
)

assert.instanceOf(ally.use('github'), GithubDriver)
assert.strictEqual(ally.use('github'), ally.use('github'))
expectTypeOf(ally.use).parameters.toEqualTypeOf<['github']>()
expectTypeOf(ally.use('github')).toMatchTypeOf<GithubDriver>()
})

test('throw error when making an unknown driver', () => {
const ctx = new HttpContextFactory().create()

const ally = new AllyManager({}, ctx)
;(ally.use as any)('github')
}).throws(
'Unknown ally provider "github". Make sure it is registered inside the config/ally.ts file'
)
})
51 changes: 51 additions & 0 deletions tests/configure.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* @adonisjs/static
*
* (c) AdonisJS
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

import { test } from '@japa/runner'
import { fileURLToPath } from 'node:url'
import { IgnitorFactory } from '@adonisjs/core/factories'
import Configure from '@adonisjs/core/commands/configure'

const BASE_URL = new URL('./tmp/', import.meta.url)

test.group('Configure', (group) => {
group.each.setup(({ context }) => {
context.fs.baseUrl = BASE_URL
context.fs.basePath = fileURLToPath(BASE_URL)
})

test('create config file and register provider', async ({ assert }) => {
const ignitor = new IgnitorFactory()
.withCoreProviders()
.withCoreConfig()
.create(BASE_URL, {
importer: (filePath) => {
if (filePath.startsWith('./') || filePath.startsWith('../')) {
return import(new URL(filePath, BASE_URL).href)
}

return import(filePath)
},
})

const app = ignitor.createApp('web')
await app.init()
await app.boot()

const ace = await app.container.make('ace')
const command = await ace.create(Configure, ['../../index.js'])
await command.exec()

await assert.fileExists('config/ally.ts')
await assert.fileExists('contracts/ally.ts')
await assert.fileExists('.adonisrc.json')
await assert.fileContains('.adonisrc.json', '@adonisjs/ally/ally_provider')
await assert.fileContains('config/ally.ts', 'defineConfig')
})
})
37 changes: 37 additions & 0 deletions tests/define_config.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* @adonisjs/ally
*
* (c) AdonisJS
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

import { test } from '@japa/runner'
import { HttpContextFactory } from '@adonisjs/core/factories/http'

import { AllyManager, defineConfig } from '../index.js'
import { GithubDriver } from '../src/drivers/github.js'
import type { GithubDriverContract } from '../src/types.js'

test.group('Define config', () => {
test('define manager config from user defined config', ({ assert, expectTypeOf }) => {
const managerConfig = defineConfig({
github: {
driver: 'github',
clientId: '',
clientSecret: '',
callbackUrl: '',
scopes: ['admin:org'],
},
})

const ctx = new HttpContextFactory().create()
const ally = new AllyManager(managerConfig, ctx)

assert.instanceOf(ally.use('github'), GithubDriver)
assert.strictEqual(ally.use('github'), ally.use('github'))
expectTypeOf(ally.use).parameters.toEqualTypeOf<['github']>()
expectTypeOf(ally.use('github')).toMatchTypeOf<GithubDriverContract>()
})
})
Loading

0 comments on commit dee4752

Please sign in to comment.