Best JavaScript code snippet using devicefarmer-stf
test_main.js
Source:test_main.js
1const log4js = require('log4js');2const jwtUtil = require('./jwt_util');34log4js.configure({5 appenders: {6 out: {7 type: 'stdout',8 layout: {9 type: 'pattern',10 pattern: '%d{yyyy-MM-dd hh:mm:ss.SSS} %p [%f{1}.%l] - %m'11 }12 }13 },14 categories: {15 default: {16 appenders: ['out'],17 level: 'debug',18 enableCallStack: true19 }20 }21});2223const logger = log4js.getLogger();2425function main() {26 logger.info('main started.');2728 var secret = 'secretkey';29 var header = '{"alg":"HS256","typ":"JWT"}';30 var payload = '{"loggedInAs":"admin","iat":1422779638}';3132 var headerBase64 = jwtUtil.base64UrlEncode(header);33 var payloadBase64 = jwtUtil.base64UrlEncode(payload);34 logger.info('headerBase64=%s', headerBase64);35 logger.info('payloadBase64=%s', payloadBase64);3637 var sign = jwtUtil.hmacSHA256Base64Url(headerBase64 + '.' + payloadBase64, secret);38 logger.info('sign=%s', sign);3940 var headerDecode = jwtUtil.base64UrlDecodeToString(headerBase64);41 var payloadDecode = jwtUtil.base64UrlDecodeToString(payloadBase64);42 logger.info('headerDecode=%s', headerDecode);43 logger.info('payloadDecode=%s', payloadDecode);4445 var jwtHmacSHA256Obj = jwtUtil.generateJwtObjectHmacSHA256(header, payload, secret);46 logger.info('jwtHmacSHA256Obj=%s', jwtHmacSHA256Obj);4748 var jwtHmacSHA256 = jwtUtil.generateJwtHmacSHA256(header, payload, secret);49 logger.info('jwtHmacSHA256=%s', jwtHmacSHA256);5051 var jwtObjEqualsString = true;52 if (!jwtHmacSHA256Obj || !jwtHmacSHA256) {53 jwtObjEqualsString = false;54 }55 if (jwtHmacSHA256Obj.toString() !== jwtHmacSHA256) {56 jwtObjEqualsString = false;57 }5859 if (jwtObjEqualsString) {60 logger.info('jwtHmacSHA256Obj == jwtHmacSHA256');61 } else {62 logger.error('jwtHmacSHA256Obj != jwtHmacSHA256');63 }6465 logger.info('jwtHmacSHA256Obj verify=%s', jwtUtil.verifyJwtObjectHmacSHA256(jwtHmacSHA256Obj, secret));66 logger.info('jwtHmacSHA256 verify=%s', jwtUtil.verifyJwtHmacSHA256(jwtHmacSHA256, secret));6768 var testSecret = '_qUJSDZMX%sw=8DVPXQRTesxPh^+PkFa';69 jwtHmacSHA256 = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.' +70 'eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.' +71 '_njXJMMpxRK10OW47Tpu_iPgG8tbkFbsun2Brl1O6zI';72 var jwtObject = new jwtUtil.JWTObject(jwtHmacSHA256);73 logger.info('Test verify=%s', jwtUtil.verifyJwtObjectHmacSHA256(jwtObject, testSecret));74 logger.info('Test payload=%s', jwtUtil.base64UrlDecodeToString(jwtObject.payload));75}76
...
jwt.test.js
Source:jwt.test.js
1const assert = require('assert');2const utils = require("../index");3const sinon = require('sinon');4describe('#jwt #config', function () {5 it('config should be equal default values: ', function () {6 const result = utils.jwtUtil.getConfig();7 assert.deepStrictEqual(result, { expiresIn: '1h', secret: 'sample' });8 });9 it('config should be equal set values: ', function () {10 utils.jwtUtil.setConfig({11 expiresIn: '2h'12 });13 const result = utils.jwtUtil.getConfig();14 assert.deepStrictEqual(result, { expiresIn: '2h', secret: 'sample' });15 });16 it('config should be equal set 2h: ', function () {17 utils.jwtUtil.setConfig({18 expiresIn: '2h'19 });20 const result = utils.jwtUtil.getConfig("expiresIn");21 assert.strictEqual(result, '2h');22 });23});24describe('#jwt #sign', function () {25 it('signed data should be string and not equal null or empty: ', async function () {26 const result = await utils.jwtUtil.sign({ mission: "test" });27 assert.deepStrictEqual(typeof result, 'string');28 });29 it('signed data should be string and not equal null or empty: ', async function () {30 const signedData = await utils.jwtUtil.sign({ mission: "test" });31 const resultData = await utils.jwtUtil.verify(signedData);32 assert.deepStrictEqual(resultData, { mission: "test" });33 });34 it('signed data should be equal verified data: ', async function () {35 const signedData = await utils.jwtUtil.sign({ mission: "test" }, "!mandh.");36 const resultData = await utils.jwtUtil.verify(signedData, "!mandh.");37 assert.deepStrictEqual(resultData, { mission: "test" });38 });39 it('res.cookie should be run: ', async function () {40 const res = {41 cookie: sinon.fake(),42 }43 const signedData = await utils.jwtUtil.sign({ mission: "test" });44 await utils.jwtUtil.cookie(res, { mission: "test" });45 assert(res.cookie.calledOnceWithExactly("jwt", signedData))46 });47 it('res.cookie should be run: ', async function () {48 const res = {49 clearCookie: sinon.fake(),50 }51 await utils.jwtUtil.clear(res);52 assert(res.clearCookie.calledOnceWithExactly("jwt"))53 });...
jwt.js
Source:jwt.js
1const jwt = require('jsonwebtoken');2const fs = require('fs');3const jwtUtil = {};4jwtUtil.issuer = process.env.ISSUER;5jwtUtil.privateKEY = fs.readFileSync(`${__dirname}/../ssh/private.key`, 'utf8');6jwtUtil.publicKEY = fs.readFileSync(`${__dirname}/../ssh/public.key`, 'utf8');7jwtUtil.createSecretToken = (data) => {8 return jwt.sign(9 data,10 jwtUtil.privateKEY,11 {12 expiresIn: '7300d',13 issuer: jwtUtil.issuer,14 algorithm: 'RS256'15 }16 );17};18jwtUtil.verify = (token) => {19 let decoded = {};20 if (token) {21 try {22 decoded = jwt.verify(23 token,24 jwtUtil.publicKEY, {25 algorithms: ['RS256'],26 issuer: jwtUtil.issuer,27 }28 );29 } catch (err) {30 console.log(err);31 }32 }33 return decoded;34};...
Using AI Code Generation
1var jwtutil = require('devicefarmer-stf-jwtutil');2console.log(jwtutil.generateToken(1, 1, 1, 1));3var jwtutil = require('devicefarmer-stf-jwtutil');4console.log(jwtutil.generateToken(1, 1, 1, 1));5var jwtutil = require('devicefarmer-stf-jwtutil');6console.log(jwtutil.generateToken(1, 1, 1, 1));7var jwtutil = require('devicefarmer-stf-jwtutil');8console.log(jwtutil.generateToken(1, 1, 1, 1));9var jwtutil = require('devicefarmer-stf-jwtutil');10console.log(jwtutil.generateToken(1, 1, 1, 1));11var jwtutil = require('devicefarmer-stf-jwtutil');12console.log(jwtutil.generateToken(1, 1, 1, 1));13var jwtutil = require('devicefarmer-stf-jwtutil');14console.log(jwtutil.generateToken(1, 1, 1, 1));15var jwtutil = require('devicefarmer-stf-jwtutil');16console.log(jwtutil.generateToken(1, 1, 1, 1));17var jwtutil = require('devicefarmer-stf-jwtutil');18console.log(jwtutil.generateToken(1, 1, 1, 1));
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!