oauth2 authentication request intercepted

This commit is contained in:
nooma
2023-08-23 09:12:24 +08:00
committed by Evis Chang
parent 6e4e27c325
commit f8ab5f984c
3 changed files with 49 additions and 8 deletions

View File

@@ -33,6 +33,8 @@ attach to process
![Set breakpoint](./images/SetBreakpoint.png)
and set breakpoint.
You may also use auto attach mode, which will automatically attach to the process when you start the relay server or run testing suite. If auto
# Testing
In the root directory, run:
```shell
@@ -42,3 +44,12 @@ or run tests for a specific package:
```shell
yarn run test --scope @unirep-app/relay
```
To run specific test file:
```shell
yarn test test/login.test.ts
```
We use nock to mock the HTTP requests. If you want to run the tests with real HTTP requests. You can turn on nock debug mode by setting `DEBUG=nock.*` in front of your shell command and run the tests.
```shell
DEBUG=nock.* yarn test test/login.test.ts
```

View File

@@ -10,7 +10,7 @@ import { HTTP_SERVER } from './configs'
import { deployContracts, startServer } from './environment'
let userState: UserState
//nock.recorder.rec();
describe('LOGIN /login', () => {
let unirep, app;
let db, prover, provider, TransactionManager;
@@ -36,13 +36,42 @@ describe('LOGIN /login', () => {
// https://developer.twitter.com/en/docs/authentication/oauth-2-0/user-access-token
const mockState = 'state'
const mockCode = 'VGNibzFWSWREZm01bjN1N3dicWlNUG1oa2xRRVNNdmVHelJGY2hPWGxNd2dxOjE2MjIxNjA4MjU4MjU6MToxOmFjOjE'
const scope = nock('http://api.twitter.com')
.persist()
.intercept('/', 'PATCH')
.reply(200, { access_token: 'test_token' })
scope.on('request', (req, interceptor) => {
console.log('interceptor matched request', interceptor.uri)
});
nock('https://api.twitter.com', {"encodedQueryParams":true})
.post('/2/oauth2/token')
.query({
"code":"VGNibzFWSWREZm01bjN1N3dicWlNUG1oa2xRRVNNdmVHelJGY2hPWGxNd2dxOjE2MjIxNjA4MjU4MjU6MToxOmFjOjE",
"grant_type":"authorization_code",
"client_id":"eDlSQVpZVFlRUmNLa0VMR3NhbTA6MTpjaQ",
"redirect_uri":"http%3A%2F%2Flocalhost%3A8000%2Fapi%2Fuser"
})
.matchHeader('Content-type', 'application/x-www-form-urlencoded')
.matchHeader('Authorization', 'Basic ZURsU1FWcFpWRmxSVW1OTGEwVk1SM05oYlRBNk1UcGphUTpGVnpkalhjMW5Tdlh5UnFwYkduVWJrbko3cEF2cFpDZHlvMVRqNTBPaDZ4Z3NTTFFRQg==')
.reply(200, {
"token_type":"bearer",
"refresh_token": 'mock-refresh-token',
"access_token":"AAAA%2FAAA%3DAAAAAAAAxxxxxx"
});
nock('https://api.twitter.com')
.post('/2/oauth2/token')
.query({
client_id: 'eDlSQVpZVFlRUmNLa0VMR3NhbTA6MTpjaQ',
grant_type: 'refresh_token',
refresh_token: 'mock-refresh-token'
})
.matchHeader('Content-type', 'application/x-www-form-urlencoded')
.matchHeader('Authorization', 'Basic ZURsU1FWcFpWRmxSVW1OTGEwVk1SM05oYlRBNk1UcGphUTpGVnpkalhjMW5Tdlh5UnFwYkduVWJrbko3cEF2cFpDZHlvMVRqNTBPaDZ4Z3NTTFFRQg==')
.reply(200,{
"access_token":"AAAA%2FAAA%3DAAAAAAAAxxxxxx"
});
nock('https://api.twitter.com', {"encodedQueryParams":true})
.get('/2/users/me')
.reply(200,{
"data": {
"id": "2244994945",
"name": "SocialTWDev",
"username": "SocialTW Dev"
}
});
const response = (await fetch(`${HTTP_SERVER}/api/user?state=${mockState}&code=${mockCode}`, {
method: 'GET',
}))

View File

@@ -1,6 +1,7 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"sourceMap": true,
"outDir": "./build"
},
"include": ["./src", ".env", "./test"],