{"size": 1326, "ext": "js", "max_stars_repo_path": "test/token.test.js", "max_stars_repo_name": "Eli-Goldberg/auth-tools", "max_stars_repo_stars_event_min_datetime": "2018-07-28T05:51:26.000Z", "max_stars_repo_stars_event_max_datetime": "2018-07-28T05:51:26.000Z", "max_issues_repo_path": "test/token.test.js", "max_issues_repo_name": "Eli-Goldberg/auth-tools", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "test/token.test.js", "max_forks_repo_name": "Eli-Goldberg/auth-tools", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 28.8260869565, "max_line_length": 87, "alphanum_fraction": 0.6093514329}
const { expect } = require('chai');
const { signToken, verifyToken } = require('../libs/token');

const secret = 'secret123';
const shaPassword = 'pass456';

const wait = async (ms) => new Promise(resolve => setTimeout(resolve, ms));

describe('token', () => {
  it('should throw an error if payload is a string', async () => {
    let catched;
    try {
      await signToken({ payload: 'this is a sample string', secret, expiresIn: '7d' });
    }
    catch (error) {
      catched = error;
    }
    expect(catched).to.exist;
  });
  it('should be able to verify a signed token', async () => {
    const testPayload = { msg: '123' };
    const token = await signToken({ payload: testPayload, secret, expiresIn: '7d' });

    const decoded = await verifyToken({ token, secret });
    expect(decoded).to.contain(testPayload);
  });
  it('should not be able to verify an expired token', async () => {
    const testPayload = { msg: '123' };
    // Set the expiry for 50 ms
    const token = await signToken({ payload: testPayload, secret, expiresIn: '50' });

    // Wait for 100ms
    await wait(100);

    let catched;
    let decoded;
    try {
      decoded = await verifyToken({ token, secret });

    } catch (error) {
      catched = error;
    }
    expect(catched).to.exist;
    expect(decoded).to.not.exist;
  });
});