Skip to content

Instantly share code, notes, and snippets.

@cklanac
cklanac / demo.js
Created June 20, 2018 15:13
single jquery REST calls
// Create an Item
// var settings = {
// 'url': 'https://thinkful-list-api.herokuapp.com/chris/items',
// 'method': 'POST',
// 'data': JSON.stringify({ name: 'Milk' }),
// 'success': function (res) {
// console.log(res);
// },
// 'error': function (err) {
// console.log(err);
@cklanac
cklanac / challenge-10-postgres-testing.md
Last active November 3, 2018 08:54
Challenge 10: Knex JS Testing

Noteful Challenge - Integration Testing

In this challenge you will re-implement testing on the Noteful app, this time we a real database. And you'll update the tests to cross-check the API results against the database.

Requirements

  • NPM install devDependencies for testing and update package.json file
  • Update server.js to prevent app.listen from running during tests
  • Add a test property to knexfile.js
  • Create a test database
@cklanac
cklanac / drop-create-import.sql
Last active April 23, 2018 15:25
Drop, Create and Import
~ $ dropdb dev-restaurants-app
~ $ createdb -U dev dev-restaurants-app
~ $ psql -U dev -f ~/Desktop/nyc-restaurants-data-backup.mssql -d dev-restaurants-app
~ $ psql -U dev -d dev-restaurants-app < ~/Desktop/temp.sql
@cklanac
cklanac / seed-database.js
Last active August 24, 2018 05:34
Sample seed DB
'use strict';
const mongoose = require('mongoose');
const { MONGODB_URI } = require('../config');
const Note = require('../models/note');
const Folder = require('../models/folder');
const Tag = require('../models/tag');
const User = require('../models/user');
@cklanac
cklanac / promise.js
Created April 19, 2018 18:05
promises demo
'use strict';
function coinFlip(delay) {
return new Promise((resolve, reject) => {
setTimeout(function () {
if (Math.round(Math.random())) {
resolve('Heads!');
} else {
reject('Tails!');
}
@cklanac
cklanac / api.js
Created April 18, 2018 13:28
Feature / Noteful App V1 POST and DELETE Client update
/* global $ */
'use strict';
const api = {
search: function (query, callback) {
$.ajax({
type: 'GET',
url: '/api/notes/',
dataType: 'json',
@cklanac
cklanac / challenge-20-testing-jwts.md
Last active November 3, 2018 08:50
Challenge 20 Testing JWTs

Challenge - Testing JWTs and Protected Endpoints

In this challenge you will update integration tests to support to test JWT authorization. You can use the Node JWT Auth as a guide.

Access Protected Endpoint

Chia-Http makes accessing endpoints protected by JWTs is relatively easy. Simply create a valid JWT using the site's JWT_SECRET and add it as a Bearer token to each request. But there's a wrinkle, the resources (eg. notes, folders and tags) belong to an specifec individual so the JWT must be created using the seed data. And the tests must be updated to check for the specific user's resources.

We'll tackle this challenge in 2 steps. First, configure the tests to generate a valid JWT and use it to test an endpoint. Second, update the tests to verify the data based on the user.

@cklanac
cklanac / challenge-19.1-testing-auth.md
Last active November 3, 2018 08:48
Challenge: 19.1 Testing Local Auth

Challenge - Testing Local Auth

In this challenge you will create a tests which verify the login process.

Create a Local Auth test

To test the login process, you first need to seed the database a user, then POST a username and password to the endpoint. The endpoint will return a JWT that you must verify using your JWT_SECRET.

To get started, create a /test/login.test.js file. And add the require packages and setup to the start of the file.

@cklanac
cklanac / users.test.js
Last active December 5, 2018 21:20
Starter for testing user authentication
'use strict';
const app = require('../server');
const chai = require('chai');
const chaiHttp = require('chai-http');
const mongoose = require('mongoose');
const { TEST_MONGODB_URI } = require('../config');
const User = require('../models/user');
@cklanac
cklanac / users.json
Last active April 4, 2018 17:46
Seed users with hashed password
[
{
"_id": "333333333333333333333300",
"fullname": "User Zero",
"username": "user0",
"password": "$2a$10$QJCIX42iD5QMxLRgHHBJre2rH6c6nI24UysmSYtkmeFv6X8uS1kgi"
},
{
"_id": "333333333333333333333301",
"fullname": "User One",