Skip to content

Instantly share code, notes, and snippets.

@gnschenker
gnschenker / glossary-spec.js
Created December 30, 2019 16:12
Using the getToken util function to retrieve the bearer token
describe("Suite to test the glossary API", () => {
const uuidv1 = require('uuid/v1');
const https = require('https');
const httpsAgent = new https.Agent({ rejectUnauthorized: false });
const axios = require('axios');
const host = process.env.API_HOST || "localhost";
const baseUrl = `http://${host}:5000/api`;
console.log(`BaseURL: ${baseUrl}`);
const client = axios.create({
baseURL: baseUrl,
@gnschenker
gnschenker / utils.js
Created December 30, 2019 16:03
Logic to retrieve the token from Auth0
const axios = require('axios');
var Config = require('config-js');
var config = new Config('./config/config.js');
module.exports = {
getToken: async () => {
const url = config.get("auth0.url");
const body = {
client_id: config.get("auth0.client_id"),
client_secret: config.get("auth0.client_secret"),
@gnschenker
gnschenker / GloassaryControllerTests.cs
Created December 30, 2019 15:36
Deleting a glossary item using an access token
[Fact]
public async Task should_delete_glossary_item()
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
var response = await client.DeleteAsync("glossary/openid");
Assert.Equal(HttpStatusCode.NoContent, response.StatusCode);
}
@gnschenker
gnschenker / GlossaryControllerTests.cs
Created December 30, 2019 15:17
First integration test with base class
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json;
using api;
using Xunit;
namespace tests
{
public class GlossaryControllerTests : ControllerTestsBase
@gnschenker
gnschenker / ControllerTestsBase.cs
Created December 30, 2019 14:52
The base class for tests requiring a token
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Text.Json;
using Microsoft.Extensions.Configuration;
using Xunit;
namespace tests
{
@gnschenker
gnschenker / glossary-spec.js
Created December 30, 2019 13:11
First outside-in test
describe("Suite to test the glossary API", () => {
const https = require('https');
const httpsAgent = new https.Agent({ rejectUnauthorized: false });
const axios = require('axios');
const baseUrl = `https://localhost:5001/api`;
const client = axios.create({
baseURL: baseUrl,
httpsAgent
})
@gnschenker
gnschenker / Startup.cs
Created December 30, 2019 12:45
Configuring Authentication via Bearer tokens
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(options =>
{
options.Authority = $"https://{Configuration["Auth0:Domain"]}/";
options.Audience = Configuration["Auth0:Audience"];
@gnschenker
gnschenker / UnitTest1.cs
Created December 30, 2019 12:35
Integration test for API
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using api;
using Xunit;
namespace tests
{
@gnschenker
gnschenker / bash
Created October 16, 2019 15:00
VS is executing the following task inside the running container
docker exec \
-i 0dbf706877a9bb59911178ce01ff0786cdd1addb0219cb0417bb1781b903e3ee \
/bin/sh -c "if PID=$(pidof dotnet); then kill $PID; fi"
@gnschenker
gnschenker / bash
Created October 16, 2019 14:44
Docker run command of VS to enable debugging inside a container
docker run -dt \
-v "C:\Users\gnsch\vsdbg\vs2017u5:/remote_debugger:rw" \
-v "C:\Users\gnsch\source\repos\WebApplication1\WebApplication1:/app" \
-v "C:\Users\gnsch\source\repos\WebApplication1:/src" \
-v "C:\Users\gnsch\AppData\Roaming\Microsoft\UserSecrets:/root/.microsoft/usersecrets:ro" \
-v "C:\Users\gnsch\AppData\Roaming\ASP.NET\Https:/root/.aspnet/https:ro" \
-v "C:\Users\gnsch\.nuget\packages\:/root/.nuget/fallbackpackages2" \
-v "C:\Program Files\dotnet\sdk\NuGetFallbackFolder:/root/.nuget/fallbackpackages" \
-e "DOTNET_USE_POLLING_FILE_WATCHER=1" \
-e "ASPNETCORE_ENVIRONMENT=Development" \