1
1
import { consentList } from "@/services/hydra"
2
- import axios from "axios"
2
+ import { sleep } from "@/utils"
3
+ import puppeteer from "puppeteer"
3
4
4
5
import { createUserAndWalletFromPhone , getUserIdByPhone , randomPhone } from "test/helpers"
5
6
6
7
let userId : UserId
7
8
const phone = randomPhone ( )
8
9
// const phone = "+14152991378" as PhoneNumber
9
10
10
- const redirectUri = "http://localhost/callback"
11
- const scope = "offline read write"
12
- const grant_types = [ "authorization_code" , "refresh_token" ]
13
-
14
11
beforeAll ( async ( ) => {
15
12
await createUserAndWalletFromPhone ( phone )
16
13
userId = await getUserIdByPhone ( phone )
17
14
} )
18
15
19
- async function createOAuthClient ( ) {
20
- const hydraAdminUrl = "http://localhost:4445/admin/clients"
16
+ async function performOAuthLogin ( ) {
17
+ // create oauth2 client
18
+
19
+ const browser = await puppeteer . launch ( )
20
+ // const browser = await puppeteer.launch({ headless: true })
21
+ const page = await browser . newPage ( )
22
+
23
+ // Navigate the page to a URL
24
+ await page . goto ( "http://localhost:3001/api/auth/signin" )
25
+
26
+ await page . waitForSelector ( ".button" )
27
+
28
+ // Click the button with the text "Sign in with Blink"
29
+ await page . click ( ".button" )
30
+
31
+ console . log ( "New URL:" , page . url ( ) )
32
+
33
+ await page . waitForSelector ( '[data-testid="sign_in_with_phone_btn"]' )
34
+ await page . waitForFunction (
35
+ "document.querySelector(\"[data-testid='sign_in_with_phone_btn']\").isConnected" ,
36
+ )
37
+ await sleep ( 500 )
38
+ await page . click ( '[data-testid="sign_in_with_phone_btn"]' )
39
+
40
+ await page . waitForSelector ( '[data-testid="phone_number_input"]' )
41
+ await page . type ( '[data-testid="phone_number_input"]' , phone )
42
+
43
+ console . log ( "phone" , phone )
44
+
45
+ await page . click ( "#accept" )
46
+
47
+ console . log ( "New URL:" , page . url ( ) )
48
+
49
+ await sleep ( 500 )
50
+
51
+ await page . screenshot ( { path : "screenshot1.png" } )
52
+
53
+ page . on ( "request" , ( request ) => {
54
+ console . log ( request . url ( ) )
55
+ } )
56
+
57
+ page . on ( "response" , ( response ) => {
58
+ console . log ( response . url ( ) )
59
+ } )
60
+
61
+ await page . waitForSelector ( "#code" )
62
+ await page . type ( "#code" , "000000" , { delay : 100 } )
63
+
64
+ await sleep ( 3000 )
65
+ await page . screenshot ( { path : "screenshot2.png" } )
21
66
22
67
try {
23
- const response = await axios . post ( hydraAdminUrl , {
24
- client_name : "integration_test" ,
25
- grant_types,
26
- response_types : [ "code" , "id_token" ] ,
27
- redirect_uris : [ redirectUri ] ,
28
- scope,
29
- skip_consent : true ,
30
- } )
31
-
32
- const clientId = response . data . client_id
33
- const clientSecret = response . data . client_secret
34
-
35
- return { clientId, clientSecret }
36
- } catch ( error ) {
37
- console . error ( "Error creating OAuth client:" , error . response )
38
- }
39
- }
68
+ await page . click ( '[data-testid="verification_code_submit_btn"]' )
69
+ } catch { }
40
70
41
- async function performOAuthLogin ( {
42
- clientId,
43
- clientSecret,
44
- } : {
45
- clientId : string
46
- clientSecret : string
47
- } ) {
48
- // create oauth2 client
71
+ await page . screenshot ( { path : "screenshot3.png" } )
72
+ await sleep ( 3000 )
49
73
50
- const responseType = "code"
51
- const randomState = "MKfNw-q60talMJ4GU_h1kHFvcPtnQkZI0XLpTkHvJL4"
74
+ await page . screenshot ( { path : "screenshot4.png" } )
75
+ await sleep ( 3000 )
76
+ await page . screenshot ( { path : "screenshot5.png" } )
52
77
53
- const authUrl = `http://localhost:4444/oauth2/auth?response_type=${ responseType } &client_id=${ clientId } &redirect_uri=${ encodeURIComponent ( redirectUri ) } &scope=${ encodeURIComponent ( scope ) } &state=${ randomState } `
78
+ // const authUrl = `http://localhost:4444/oauth2/auth?response_type=${responseType}&client_id=${clientId}&redirect_uri=${encodeURIComponent(redirectUri)}&scope=${encodeURIComponent(scope)}&state=${randomState}`
54
79
55
80
// https://oauth.blink.sv/oauth2/auth?client_id=73ae7c3e-e526-412a-856c-25d1ae0cbc55&scope=read%20write&response_type=code&redirect_uri=https%3A%2F%2Fdashboard.blink.sv%2Fapi%2Fauth%2Fcallback%2Fblink&state=MKfNw-q60talMJ4GU_h1kHFvcPtnQkZI0XLpTkHvJL4
56
81
57
82
// Simulate user going to the authorization URL and logging in
58
83
// This part would require a real user interaction or a browser automation tool like puppeteer
59
84
60
- let data
61
- try {
62
- const res = await axios . get ( authUrl )
63
- data = res . data
64
- } catch ( error ) {
65
- console . error ( "Error getting auth URL:" , error )
66
- return
67
- }
85
+ // let data
86
+ // try {
87
+ // const res = await axios.get(authUrl)
88
+ // data = res.data
89
+ // } catch (error) {
90
+ // console.error("Error getting auth URL:", error)
91
+ // return
92
+ // }
68
93
69
94
// You need to extract the code from the callback response
70
- const code = data . code // Simplified: Actual extraction depends on your OAuth provider
71
-
72
- console . log ( "data" , data )
73
- console . log ( "code" , code )
74
-
75
- try {
76
- // Exchange the code for a token
77
- const tokenResponse = await axios . post ( "http://localhost:4444/oauth2/token" , {
78
- code,
79
- redirect_uri : redirectUri ,
80
- client_id : clientId ,
81
- client_secret : clientSecret ,
82
- grant_type : "authorization_code" ,
83
- } )
84
-
85
- const accessToken = tokenResponse . data . access_token
86
-
87
- // Use the access token to get user info or other secured resources
88
- // Update the consent list as needed
89
- return accessToken // This might be used for further secured requests
90
- } catch ( error ) {
91
- console . error ( "Error exchanging code for token:" , error )
92
- }
95
+ // const code = data.code // Simplified: Actual extraction depends on your OAuth provider
96
+
97
+ // console.log("data", data)
98
+ // console.log("code", code)
99
+
100
+ // try {
101
+ // // Exchange the code for a token
102
+ // const tokenResponse = await axios.post("http://localhost:4444/oauth2/token", {
103
+ // code,
104
+ // redirect_uri: redirectUri,
105
+ // client_id: clientId,
106
+ // client_secret: clientSecret,
107
+ // grant_type: "authorization_code",
108
+ // })
109
+
110
+ // const accessToken = tokenResponse.data.access_token
111
+
112
+ // // Use the access token to get user info or other secured resources
113
+ // // Update the consent list as needed
114
+ // return accessToken // This might be used for further secured requests
115
+ // } catch (error) {
116
+ // console.error("Error exchanging code for token:", error)
117
+ // }
93
118
}
94
119
95
120
describe ( "Hydra" , ( ) => {
@@ -99,12 +124,7 @@ describe("Hydra", () => {
99
124
} )
100
125
101
126
it ( "get consent list when the user had perform oauth2 login" , async ( ) => {
102
- const res = await createOAuthClient ( )
103
- if ( ! res ) return
104
- const { clientId, clientSecret } = res
105
- console . log ( "clientId" , clientId , "clientSecret" , clientSecret )
106
-
107
- const accessToken = await performOAuthLogin ( { clientId, clientSecret } )
127
+ const accessToken = await performOAuthLogin ( )
108
128
console . log ( "accessToken" , accessToken )
109
129
} )
110
130
} )
0 commit comments