|
| 1 | +import http from 'k6/http'; |
| 2 | +import { sleep } from 'k6'; |
| 3 | +import { URL } from 'https://jslib.k6.io/url/1.0.0/index.js'; |
| 4 | +import { expect } from 'https://jslib.k6.io/k6chaijs/4.3.4.3/index.js'; |
| 5 | + |
| 6 | +const CLIENT_ID = __ENV.CLIENT_ID; |
| 7 | +const CLIENT_SECRET = __ENV.CLIENT_SECRET; |
| 8 | +const REDIRECT_URI = __ENV.REDIRECT_URI; |
| 9 | +const OTP_BASE_URL = __ENV.OTP_BASE_URL; |
| 10 | + |
| 11 | +const SCENARIO = __ENV.SCENARIO || 'smoke'; |
| 12 | + |
| 13 | +const scenarios = { |
| 14 | + smoke: { |
| 15 | + executor: 'constant-vus', |
| 16 | + vus: 1, |
| 17 | + duration: '10s', |
| 18 | + tags: { test_type: 'smoke' }, |
| 19 | + }, |
| 20 | + load: { |
| 21 | + executor: 'ramping-arrival-rate', |
| 22 | + startRate: 1, |
| 23 | + timeUnit: '1s', |
| 24 | + preAllocatedVUs: 100, |
| 25 | + stages: [ |
| 26 | + { target: 5, duration: '10s' }, |
| 27 | + { target: 10, duration: '10s' }, |
| 28 | + { target: 15, duration: '10s' }, |
| 29 | + { target: 15, duration: '30s' }, |
| 30 | + ], |
| 31 | + tags: { test_type: 'load' }, |
| 32 | + }, |
| 33 | +}; |
| 34 | + |
| 35 | +export let options = { |
| 36 | + thresholds: { |
| 37 | + // Fail if any request takes longer than 5s |
| 38 | + http_req_duration: [{ threshold: 'max<5000', abortOnFail: true }], |
| 39 | + http_req_failed: [{ threshold: 'rate<0.001', abortOnFail: true }], // fail if more than 0.1% fail |
| 40 | + }, |
| 41 | + tags: { |
| 42 | + testid: __ENV.TEST_ID, |
| 43 | + rds_min_acu: __ENV.RDS_MIN_ACU, |
| 44 | + rds_max_acu: __ENV.RDS_MAX_ACU, |
| 45 | + fargate_tasks: __ENV.FARGATE_TASKS, |
| 46 | + fargate_cpu: __ENV.FARGATE_CPU, |
| 47 | + fargate_mem: __ENV.FARGATE_MEM, |
| 48 | + }, |
| 49 | + scenarios: {}, |
| 50 | +}; |
| 51 | + |
| 52 | +options.scenarios[SCENARIO] = scenarios[SCENARIO]; |
| 53 | + |
| 54 | +export default function () { |
| 55 | + console.log(`starting with vu ${__VU}`); |
| 56 | + const authParams = { |
| 57 | + client_id: CLIENT_ID, |
| 58 | + redirect_uri: REDIRECT_URI, |
| 59 | + response_type: 'code', |
| 60 | + scope: 'openid', |
| 61 | + }; |
| 62 | + |
| 63 | + const jar = http.cookieJar(); |
| 64 | + |
| 65 | + const authUrl = |
| 66 | + `${OTP_BASE_URL}/auth?` + |
| 67 | + Object.entries(authParams) |
| 68 | + .map(([k, v]) => `${k}=${encodeURIComponent(v)}`) |
| 69 | + .join('&'); |
| 70 | + |
| 71 | + let res = http.get(authUrl, { redirects: 0, jar }); |
| 72 | + let cookies = res.cookies; |
| 73 | + |
| 74 | + const loginPath = `${OTP_BASE_URL}${cookies._interaction[0].path}/otp`; |
| 75 | + |
| 76 | + // Generating randId to avoid email dupes |
| 77 | + const randID = Math.floor(Math.random() * 100000000000); |
| 78 | + |
| 79 | + res = http.post( |
| 80 | + loginPath, |
| 81 | + { |
| 82 | + email: `test${randID}@mail.com`, |
| 83 | + }, |
| 84 | + { |
| 85 | + jar, |
| 86 | + redirects: 0, |
| 87 | + headers: { |
| 88 | + 'Content-Type': 'application/x-www-form-urlencoded', |
| 89 | + }, |
| 90 | + }, |
| 91 | + ); |
| 92 | + |
| 93 | + const authnPath = `${OTP_BASE_URL}${cookies._interaction[0].path}/login`; |
| 94 | + |
| 95 | + res = http.post( |
| 96 | + authnPath, |
| 97 | + { |
| 98 | + code1: '1', |
| 99 | + code2: '1', |
| 100 | + code3: '1', |
| 101 | + code4: '1', |
| 102 | + code5: '1', |
| 103 | + code6: '1', |
| 104 | + }, |
| 105 | + { |
| 106 | + jar, |
| 107 | + redirects: 0, |
| 108 | + headers: { |
| 109 | + 'Content-Type': 'application/x-www-form-urlencoded', |
| 110 | + }, |
| 111 | + }, |
| 112 | + ); |
| 113 | + |
| 114 | + // Adjust to https in case location header is http |
| 115 | + const redirectUri = res.headers.Location.replace('http://', 'https://'); |
| 116 | + |
| 117 | + const newRes = http.get(redirectUri, { |
| 118 | + redirects: 0, |
| 119 | + jar, |
| 120 | + }); |
| 121 | + |
| 122 | + const url = new URL(newRes.headers.Location); |
| 123 | + const authCode = url.searchParams.get('code'); |
| 124 | + |
| 125 | + const tokenUrl = `${OTP_BASE_URL}/token`; |
| 126 | + const payload = { |
| 127 | + grant_type: 'authorization_code', |
| 128 | + code: authCode, |
| 129 | + redirect_uri: REDIRECT_URI, |
| 130 | + client_id: CLIENT_ID, |
| 131 | + client_secret: CLIENT_SECRET, |
| 132 | + }; |
| 133 | + |
| 134 | + const tokenRes = http.post(tokenUrl, payload, { |
| 135 | + redirects: 0, |
| 136 | + headers: { |
| 137 | + 'Content-Type': 'application/x-www-form-urlencoded', |
| 138 | + }, |
| 139 | + }); |
| 140 | + |
| 141 | + expect(tokenRes.status).to.equal(200); |
| 142 | + expect(JSON.parse(tokenRes.body).access_token).to.be.a('string'); |
| 143 | + sleep(1); |
| 144 | +} |
0 commit comments