Skip to content

Instantly share code, notes, and snippets.

View mutaimwiti's full-sized avatar
🕵️‍♂️
JavaScripting

Mutai Mwiti mutaimwiti

🕵️‍♂️
JavaScripting
View GitHub Profile
@mutaimwiti
mutaimwiti / index.js
Created September 3, 2023 17:03 — forked from morphatic/index.js
Example of polling a REST API every 1.5 seconds with Observables in NodeJS
/**
* Instructions
*
* 1. mkdir test
* 2. cd test
* 3. npm init
* 4. accept all defaults is fine
* 5. npm install @akanass/rx-http-request
* 6. paste code below into new file index.js
* 7. update username, password, other variables
@mutaimwiti
mutaimwiti / redux.test.js
Created September 1, 2021 01:17 — forked from arjunattam/redux.test.js
Redux store testing with Playwright
const pw = require('playwright');
(async () => {
const browser = await pw.chromium.launch();
const context = await browser.newContext();
// set addInitScript to run this on every page load in this context
// in the app, use window.IS_PLAYWRIGHT to set window.store = store;
await context.addInitScript('window.IS_PLAYWRIGHT = true;')
@mutaimwiti
mutaimwiti / jwt-expiration.md
Created November 16, 2020 05:44 — forked from soulmachine/jwt-expiration.md
How to deal with JWT expiration?

First of all, please note that token expiration and revoking are two different things.

  1. Expiration only happens for web apps, not for native mobile apps, because native apps never expire.
  2. Revoking only happens when (1) uses click the logout button on the website or native Apps;(2) users reset their passwords; (3) users revoke their tokens explicitly in the administration panel.

1. How to hadle JWT expiration

A JWT token that never expires is dangerous if the token is stolen then someone can always access the user's data.

Quoted from JWT RFC:

@mutaimwiti
mutaimwiti / app.js
Last active November 25, 2019 18:14
Wrapper ES6
// test/testUtils/app.js
import supertest from 'supertest';
import appDef from '../../src/app';
const { generateAuthToken } = require('../../src/utils');
const { User } = require('../../src/models');
class App {
constructor() {
@mutaimwiti
mutaimwiti / app.js
Last active November 25, 2019 18:13
Wrapper es5
// test/testUtils/app.js
var supertest = require('supertest');
var appDef = require('../../src/app');
var User = require('../../src/models').User;
var generateAuthToken = require('../../src/utils').generateAuthToken;
var app = {
client: supertest(appDef),
@mutaimwiti
mutaimwiti / article.spec.js
Last active November 27, 2019 08:10
Using wrapper es5
// test/article.spec.js
var app = require('./testUtils/app');
describe('Articles', function() {
describe('GET', function() {
it('should not allow unauthenticated users to list all articles', function(done) {
app.get('/articles').expect(401, done);
});
it('should allow authenticated users to list all articles', function(done) {
@mutaimwiti
mutaimwiti / article.spec.js
Last active November 27, 2019 08:08
Using wrapper es6
// test/article.spec.js
import app from './testUtils/app';
describe('Articles', () => {
describe('GET', () => {
it('should not allow unauthenticated users to list all articles', async () => {
const res = await app.get('/articles');
expect(res.status).toBe(401);
@mutaimwiti
mutaimwiti / article.spec.js
Created November 15, 2019 10:54
supertest-scenario-3
const request = require('supertest');
const request = supertest(app);
describe('Articles', function () {
it('should allow users to list all articles', function (done) {
request
.get('/articles')
.set('authorization', generateAuthToken())
.expect(200, done)
@mutaimwiti
mutaimwiti / article.spec.js
Created November 15, 2019 10:43
supertest-scenario-2
const request = require('supertest');
describe('Articles', function () {
it('should allow users to list all articles', function (done) {
request(app).get('/articles').expect(200, done)
});
it('should allow users to get one article', function (done) {
request(app).get('/articles/2').expect(200, done)
});
@mutaimwiti
mutaimwiti / article.spec.js
Last active November 18, 2019 16:05
supertest-scenario-1
const request = require('supertest');
const request = supertest(app);
describe('Articles', function () {
it('should allow users to list all articles', function (done) {
request.get('/articles').expect(200, done)
});
it('should allow users to get one article', function (done) {