Skip to content
ML SystemPart 4: InfrastructureChapter 12
CH.12 ~40 min

Testing & Auth

Integration testing, Bcrypt, and JWT.

TestingAuthJWT
  • Write and run integration tests for an Express API using Supertest.
  • Implement secure password hashing using Bcrypt.
  • Generate and verify JSON Web Tokens for user authorization.

01 Integration Testing & Supertest Viz

Definition

Supertest

A library for testing Node.js HTTP servers that allows making requests to endpoints without explicitly binding to a physical network port.

Test Isolation

Each test should run in a sterile environment. We use hooks like beforeEach to clear the database and seed it with fresh data before every test case.

02 Stateless Auth with JWT

  • **Bcrypt**: Used for one-way hashing of passwords with 10 salt rounds.
  • **jwt.sign**: Creates a token signed with a server-side secret key.
  • **Authorization Header**: Clients send tokens using the 'Bearer <token>' scheme.

Key Takeaways

  1. 1Integration tests ensure that different parts of the backend work together correctly.
  2. 2Supertest allows for fast, port-free testing of Express applications.
  3. 3JWT and Bcrypt are essential for secure, stateless user authentication.

CH.12

Chapter Complete

Up next:Frontend Testing

Chapter Progress

Reading
Exercise

Interact with the visualization

Quiz

Chapter Quiz

No quiz available for this chapter.