Otentikasi OAuth 2.0 Kanthi Gatling Nggunakake Token Bearer

Pos iki nerangake kepiye cara nggawe Otentikasi Oauth2 karo Gatling.

Ing conto iki, kita ngirim panjaluk kanggo nggawe pangguna. Nanging, endpoint pangguna dilindhungi lan mbutuhake access_token.

Kaping pisanan, kita bakal entuk bearer_token utawa access_token banjur dikirim minangka header menyang panjaluk API sabanjure kanggo nggawe pangguna.


Kanggo nggambarake iki, kita bakal nggunakake struktur proyek sing padha kanggo Gatling sing dibangun sadurunge:

Kerangka Pengujian Kinerja kanthi Gatling lan Maven


Nalika ngetutake langkah-langkah ing kiriman ing ndhuwur, kita bakal duwe struktur proyek kaya ing ngisor iki:





Nemtokake Parameter ing Konfigurasi

Pisanan kita nemtokake paramèter OAuth 2.0 ing Configuration.scala file obyek ing ngisor iki config folder:

object Configuration { val environment: String = System.getProperty('environment') val clientId: String = System.getProperty('CLIENT_ID') val clientSecret: String = System.getProperty('CLIENT_SECRET') val apiURL: String = 'https://some-sub-domain.' + environment + 'some-domain.com/api' var tokenPath: String = 'https://some-sub-domain' + environment + '.eu.auth0.com/oauth/token' val userPath = '/identity/iaa/v1/users' } Cathetan:Biasane, lingkungan, client_id lan client_secrets diekspor ing mesin tes bakal mlaku, mula kita bisa nggunakake System.getProperty () kanggo maca regane.

Panjaluk

Saiki kita kudu nulis kode sing ngirim panjaluk menyang server otorisasi kanggo entuk token bearer.


Panjaluk OAuth 2.0 - access_token

File iki AuthRequest.scala disimpen ing panjaluk folder ing struktur proyek kita.

import java.io.{BufferedWriter, FileWriter} import config.Configuration import io.gatling.core.Predef._ import io.gatling.http.Predef._ object AuthRequest { val getAccessToken = exec(http('Get access token')
.post(Configuration.tokenPath)
.body(StringBody(
s'''{

'client_id': '${Configuration.clientId}',

'client_secret': '${Configuration.clientSecret}',

'audience': 'https://some-domain-name.com/user',

'grant_type': 'client_credentials',

'scope': 'user:admin'
}'''
))
.asJson
.headers(Map('Content-Type' -> 'application/json'))
.check(status.is(200))
.check(jsonPath('$.access_token').saveAs('access_token')))
.exec {
session =>
val fw = new BufferedWriter(new FileWriter('access_token.txt', true))
try {

fw.write(session('access_token').as[String] + ' ')
}
finally fw.close()
session
} }

Ing potongan kode ing ndhuwur, kita uga nyimpen access_token menyang file.

Telpon ing ndhuwur, mung entuk akses_token.

Kita butuh panjaluk liyane kanggo nggawe pangguna kanthi ngirim access_token minangka header.


Panjaluk Anggota

Panjaluk pangguna ing file sing diarani UserRequests.scala lan disimpen ing sangisore panjaluk folder.

import config.Configuration.{apiURL, userPath} import io.gatling.core.Predef._ import io.gatling.http.Predef._ object UserRequests { private val auth0Headers = Map(
'Accept' -> 'application/json, text/javascript, */*; q=0.01',
'Content-Type' -> 'application/json',
'Authorization' -> 'Bearer ${access_token}') val createUser = exec(http('Create user')
.post(apiURL + userPath)
.headers(auth0Headers)
.body(ElFileBody('createUser.json'))
.check(status.is(201))) }


Skenario

Saiki kita nulis obyek skenario. Ing conto iki obyek kita diarani UserScenarios.scala lan disimpen ing sangisore skenario folder.

import requests.{AuthRequest, UserRequests} import io.gatling.core.Predef._ object UserScenarios { var userIds:Array[Map[String,String]] =
(100 to 900).toArray map ( x => { Map( 'userId' -> x.toString) }) val getAccessToken = scenario('Get token')
.exec(AuthRequest.getAccessToken) val createUserScenario = scenario('Create user')
.feed(userIds.circular)
.exec(UserAuthZRequest.getAccessToken)
.exec(UserRequests.createUser) }

Panjaluk ing ndhuwur, ngirim panjaluk POST kanggo nggawe pangguna kanthi access_token minangka bearer ing header.



Simulasi

Pungkasan file simulasi kita diarani UserSimulation.scala disimpen ing simulasi folder.


import scenario.UserScenarios import io.gatling.core.Predef._ import scala.concurrent.duration._ class UserSimulation extends Simulation { setUp(
UserScenarios.createUserScenario.inject(rampUsers(250) during (15 minutes)), ) }

Kanggo mbukak tes sing digunakake

mvn clean gatling:test