Skip to content

Instantly share code, notes, and snippets.

@palmerandy
palmerandy / Example.Tests.ps1
Created August 25, 2021 12:01
Example Powershell Module, Manifest and Pester Test
$ThisModule = $MyInvocation.MyCommand.Path -replace '\.Tests\.ps1$'
$ThisModuleName = $ThisModule | Split-Path -Leaf
Get-Module -Name $ThisModuleName -All | Remove-Module -Force -ErrorAction Ignore
Import-Module -Name "$ThisModule.psm1" -Force -ErrorAction Stop
InModuleScope $ThisModuleName {
describe 'ExampleFunction' {
it 'Write host with supplied string' {
Mock Write-Host {}
$expectedMessage = "A";
@palmerandy
palmerandy / ApiHttpClientHelper.cs
Created June 20, 2020 11:24
C# End-to-end automated integration testing for Web API. More infromation available at https://andypalmer.dev/end-to-end-testing-mvc-signalr-api
using Newtonsoft.Json;
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
public class ApiHttpClientHelper
{
public HttpClient Client { get; }
@palmerandy
palmerandy / SignalrExample.cs
Last active June 20, 2020 11:26
C# End-to-end automated integration testing for SignalR Hub Action. More infromation available at https://andypalmer.dev/end-to-end-testing-mvc-signalr-api
using Gt.Fmc.IntegrationTests.Models;
public async Task SignalRExample(string hubName, string, hubAction, T exampleParam)
{
//ensure we are logged in so that MvcCookieContainer is set allowing us to auth with SignalR Hub.
await new MvcHttpClientHelper(MvcHttpClient).LoginHttpRequest();
using (var hubConnection = new HubConnection(Environment.WebSiteUrl))
{
var jobHub = hubConnection.CreateHubProxy(hubName);
hubConnection.CookieContainer = MvcCookieContainer;
@palmerandy
palmerandy / AntiForgeryHelper.cs
Last active June 20, 2020 11:25
C# End-to-end automated integration testing for MVC. More infromation available at https://andypalmer.dev/end-to-end-testing-mvc-signalr-api
using System;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
public static class AntiForgeryHelper
{
public static string ExtractAntiForgeryToken(string htmlResponseText)
{
if (htmlResponseText == null)
@palmerandy
palmerandy / BaseRepository.cs
Last active June 14, 2024 08:52
C# SQL Database pagination sample using Dapper ORM.
using System;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
namespace Samples
{
public abstract class BaseRepository
{
protected async Task<T> DbAction<T>(Func<IDbConnection, Task<T>> action, string connectionString = null)
@palmerandy
palmerandy / AzureAppServiceAuthenticator.cs
Last active August 7, 2019 03:12
OAuth implementation for Azure Active Directory Authentication of an App Services
public class AzureAppServiceAuthenticator
{
// Found in the Azure Portal > Azure Active Directory > App Registration (matching Function app name) > Overview > Directory (tenant) ID
protected internal const string AzureActiveDirectoryTenantId = "abcd1234-abcd-1234-abcd-1234abcd123";
private static readonly HttpClient HttpClient = new HttpClient();
public async Task<string> GetBearerToken()
{
var clientId = GetClientId();
var clientSecret = GetClientSecret();
@palmerandy
palmerandy / azure-function-arm-template-with-staging-slot.json
Last active February 18, 2020 08:30
Azure Function ARM template with Staging and Production slots.
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"azureSubscriptionId": {
"type": "string",
"metadata": {
"description": "The azure subscription Id. Something like 12345678-abcd-abcd-abcd-1234567890ab."
}
},
--Start with this file, rename and remove as required.
--Use PascalCase for table names and column names.
-- create new table with composite PK; this is OK if there is a clear and simple PK from one or two other columns
if not exists(select * from sys.tables t where t.name = 'TableName')
begin
create table TableName
(
CompositeId1 int not null,
CompositeId2 int not null,