|
| 1 | +import { IBuildOptions, VulcanBuilder } from '@vulcan-sql/build'; |
| 2 | +import { ServeConfig, VulcanServer } from '@vulcan-sql/serve'; |
| 3 | +import * as supertest from 'supertest'; |
| 4 | +import defaultConfig from './projectConfig'; |
| 5 | + |
| 6 | +describe('Example1-3: get user profile by GET /auth/user-profile API with Authorization', () => { |
| 7 | + let server: VulcanServer; |
| 8 | + let projectConfig: ServeConfig & IBuildOptions; |
| 9 | + |
| 10 | + beforeEach(async () => { |
| 11 | + projectConfig = { |
| 12 | + ...defaultConfig, |
| 13 | + auth: { |
| 14 | + enabled: true, |
| 15 | + options: { |
| 16 | + basic: { |
| 17 | + 'users-list': [ |
| 18 | + { |
| 19 | + name: 'user1', |
| 20 | + // md5('test1') |
| 21 | + md5Password: '5a105e8b9d40e1329780d62ea2265d8a', |
| 22 | + attr: { |
| 23 | + role: 'admin', |
| 24 | + }, |
| 25 | + }, |
| 26 | + ], |
| 27 | + }, |
| 28 | + }, |
| 29 | + }, |
| 30 | + }; |
| 31 | + }); |
| 32 | + |
| 33 | + afterEach(async () => { |
| 34 | + await server?.close(); |
| 35 | + }); |
| 36 | + |
| 37 | + it('Example1-3-1: set Authorization in header with default options', async () => { |
| 38 | + const builder = new VulcanBuilder(projectConfig); |
| 39 | + await builder.build(); |
| 40 | + server = new VulcanServer(projectConfig); |
| 41 | + const httpServer = (await server.start())['http']; |
| 42 | + |
| 43 | + const agent = supertest(httpServer); |
| 44 | + const result = await agent |
| 45 | + .get('/auth/user-profile') |
| 46 | + .set('Authorization', 'basic dXNlcjE6dGVzdDE='); |
| 47 | + expect(result.body).toEqual({ |
| 48 | + name: 'user1', |
| 49 | + attr: { |
| 50 | + role: 'admin', |
| 51 | + }, |
| 52 | + }); |
| 53 | + }, 10000); |
| 54 | + |
| 55 | + it('Example1-3-2: set Authorization in querying with default options', async () => { |
| 56 | + const builder = new VulcanBuilder(projectConfig); |
| 57 | + await builder.build(); |
| 58 | + server = new VulcanServer(projectConfig); |
| 59 | + const httpServer = (await server.start())['http']; |
| 60 | + |
| 61 | + const auth = Buffer.from( |
| 62 | + JSON.stringify({ Authorization: 'basic dXNlcjE6dGVzdDE=' }) |
| 63 | + ).toString('base64'); |
| 64 | + |
| 65 | + const agent = supertest(httpServer); |
| 66 | + const result = await agent.get(`/auth/user-profile?auth=${auth}`); |
| 67 | + |
| 68 | + expect(result.body).toEqual({ |
| 69 | + name: 'user1', |
| 70 | + attr: { |
| 71 | + role: 'admin', |
| 72 | + }, |
| 73 | + }); |
| 74 | + }, 10000); |
| 75 | + |
| 76 | + it('Example1-3-3: set Authorization in querying with specific auth "key" options', async () => { |
| 77 | + projectConfig['auth-source'] = { |
| 78 | + options: { |
| 79 | + key: 'x-auth', |
| 80 | + }, |
| 81 | + }; |
| 82 | + const builder = new VulcanBuilder(projectConfig); |
| 83 | + await builder.build(); |
| 84 | + server = new VulcanServer(projectConfig); |
| 85 | + const httpServer = (await server.start())['http']; |
| 86 | + |
| 87 | + const auth = Buffer.from( |
| 88 | + JSON.stringify({ Authorization: 'basic dXNlcjE6dGVzdDE=' }) |
| 89 | + ).toString('base64'); |
| 90 | + |
| 91 | + const agent = supertest(httpServer); |
| 92 | + const result = await agent.get(`/auth/user-profile?x-auth=${auth}`); |
| 93 | + |
| 94 | + expect(result.body).toEqual({ |
| 95 | + name: 'user1', |
| 96 | + attr: { |
| 97 | + role: 'admin', |
| 98 | + }, |
| 99 | + }); |
| 100 | + }, 10000); |
| 101 | + |
| 102 | + it('Example1-3-4: set Authorization in json payload specific auth "x-key" options', async () => { |
| 103 | + projectConfig['auth-source'] = { |
| 104 | + options: { |
| 105 | + key: 'x-auth', |
| 106 | + in: 'payload', |
| 107 | + }, |
| 108 | + }; |
| 109 | + const builder = new VulcanBuilder(projectConfig); |
| 110 | + await builder.build(); |
| 111 | + server = new VulcanServer(projectConfig); |
| 112 | + const httpServer = (await server.start())['http']; |
| 113 | + |
| 114 | + const auth = Buffer.from( |
| 115 | + JSON.stringify({ Authorization: 'basic dXNlcjE6dGVzdDE=' }) |
| 116 | + ).toString('base64'); |
| 117 | + |
| 118 | + const agent = supertest(httpServer); |
| 119 | + |
| 120 | + const result = await agent |
| 121 | + .get('/auth/user-profile') |
| 122 | + .send({ |
| 123 | + ['x-auth']: auth, |
| 124 | + }) |
| 125 | + .set('Accept', 'application/json'); |
| 126 | + |
| 127 | + expect(result.body).toEqual({ |
| 128 | + name: 'user1', |
| 129 | + attr: { |
| 130 | + role: 'admin', |
| 131 | + }, |
| 132 | + }); |
| 133 | + }, 10000); |
| 134 | +}); |
0 commit comments