Skip to content

no3Ldev/BackendTest-WebAPI

Repository files navigation

Backend Test - Web API with WebSocket

This is a sample ASP.NET Core Web API project that exposes both classic Web API and WebSockets endpoints to process API requests in JSON format from client. It uses Microsoft Azure Database for PostgreSQL as its datasource and Entity Framework Core for its data operations such as user registration and authentication, session management, and verification code system.

Technologies Used:

  • ASP.NET Core 5.0 Web API
  • Configured for HTTPS
  • Enabled Docker Support (Windows)
  • Enabled OpenAPI support
  • Raw WebSocket protocol
  • MSTest Unit Testing
  • Built-in Swagger Support
  • Azure Database for PostgreSQL v11
  • Microsoft Entity Framework Core (PostgreSQL)
  • SHA-256 HMAC hashing

Visual Studio Projects

  • BackendTest-WebAPI - main web api project
  • BackendTest-UnitTest - unit testing project to test web api functions

Unit-Test Project: BackendTest-UnitTest

  • _BaseClass.cs - shared methods used among all test classes
  • 1-5*Test.cs - unit testing classes for web api
  • 6-WebSocketTest.cs - unresolved. Supposed unit testing for web socket
  • 6-WebSocketTest.html - unit testing for web socket using javascript

How to Test?

  • MSTest Unit-Testing for Web API - on visual studio, press Ctrl+R, A or go to Test menu, then click Run All Test item
  • Swagger for Web API - on visual studio, press F5 or go to Debug menu then click Start Debugging item
  • Web Client (Javascript) for Web Socket - on BackEndTest-UnitTest project, open 6-WebSocketTest.html on your web browser
  • Use Third-Party WebSocket Test Client for Chrome - install WebSocket Test Client extension found here in Chrome Web Store

Sample JSON Request and Response

/* Client Request */
{
    "command": "emailVerification",
    "email": "[email protected]",
    "username": "johndoe"
}
/* Server Response */
{
    "command": "emailVerification",
    "success": true,
    "remarks": null
}
/* Remarks will contain reason when success returns false */

Web API Controllers:

  • AuthenticateController.cs - generate login salt
  • AvailableController.cs - checks if username or email has already been used
  • BaseController.cs - shared methods across all controllers
  • HashController.cs - generate sha 256 hmac hash of text and key
  • LoginController.cs - login user and details
  • RegisterController.cs - register new user to database
  • VerifyController.cs - generate verification code

Middleware Service

  • WebSocketExtension.cs - add middleware as service to project
  • WebSockHandler.cs - socket connection and events handler
  • WebSocketmanager.cs - sockets connections manager
  • WebSocketMiddleware.cs - implements sockets management and events
  • WebSocketRequestHandler.cs - handles requests and response to/from clients

Database Tables

  • User - where registered users are stored
  • Authentication - stores login salt requests and validity
  • Login - successful logins history
  • Verification - stores verification codes and validity

Configuration: AppSettings

  • base_address - actual url path to web api
  • https_port - port number for https connections
  • superSecretKey - secret key to further hash stored passwords
  • salt_expiry - login salt expiration in seconds
  • session_expiry - login session expiration in seconds
  • verification_expiry - verification code expiration in seconds
  • verification_max - maximum verification code requests per day

Testing Screenshots:

image image image

About

(Sidekick) Backend Test - WebAPI

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published