<commit_before>. ../Functions/Delete-Entities.ps1
. ../Functions/Get-Token.ps1
. ../Functions/CRUD-User.ps1
. ../Functions/CRUD-Coffee.ps1

$usedEntitieSets = @("Coffees", "Users", "CoffeeOrders");
$entityPrefix = "CoffeeOrdersIntegrationTest";
$baseUri = "CoffeeTracker/api/";

Describe "CoffeeOrdersController" -Tags "CoffeeOrdersController" {
	$adminName = "<EMAIL>";
	$adminPassword = "<PASSWORD>";
	$adminToken = Get-Token -UserName $adminName -Password $adminPassword;
	
	BeforeEach {
		# Create user
		$userName = <EMAIL>-{<EMAIL>" -f [guid]::NewGuid();;
		$userPassword = "<PASSWORD>";

		$user = CRUD-User -UserName $userName -Password $userPassword -Create;
		$token = Get-Token -UserName $userName -Password $userPassword;

		# Create second user
		$userSecondName = <EMAIL>-{<EMAIL>" -f [guid]::NewGuid();;
		$userSecondPassword = "<PASSWORD>";

		$Seconduser = CRUD-User -UserName $userSecondName -Password $user<PASSWORD> -Create;
		$SecondUsertoken = Get-Token -UserName $userSecondName -Password $userSecondPassword;

		# Create Coffee
		$coffeeName = "$entityPrefix-{0}" -f [guid]::NewGuid();
		$coffeeBrand = "Test-Brand-{0}" -f [guid]::NewGuid();
		$coffeeStock = 10;

		$coffee = CRUD-Coffee -Name $coffeeName -Brand $coffeeBrand -Stock $coffeeStock -Token $adminToken -Create;
	}

	Context "Create-CoffeeOrder" {
		<#
		BeforeEach {
			$uri = "{0}{1}" -f $baseUri, "CoffeeOrders"
			$name = "$entityPrefix-{0}" -f [guid]::NewGuid();

			$authString = "bearer {0}" -f $token;

			$headers = [System.Collections.Generic.Dictionary[[String],[String]]]::New();
			$headers.Add("Authorization", $authString);

			$body = @{
				Name = $name
				UserId = $user.Id
				CoffeeId = $coffee.Id
			}
		}

		It "Warmup" -Test {
			$true | Should Be $true;
		}

		It "Create-CoffeeOrderDecreasesStockFromCoffeeSucceeds" -test {
			# Arrange
			# N/A

			# Act
			$result = Invoke-RestMethod -Method Post -Uri $uri -Body $body -Headers $headers;

			# Assert
			$coffeeUri = "{0}{1}({2})" -f $baseUri, "Coffees", $coffee.Id;
			$resultCoffee = Invoke-RestMethod -Method Get -Uri $coffeeUri -Headers $headers;
			$expectedStock = $coffeeStock - 1;

			$result | Should Not Be $null;
			$result.UserId | Should Be $user.Id;
			$result.CoffeeId | Should Be $coffee.Id;
			$resultCoffee.Stock | Should Be $expectedStock;
		}

		It "Create-CoffeeOrderWithCoffeeNotOnStockThrows400" -test {
			# Arrange
			CRUD-Coffee -Name $coffeeName -Brand $coffeeBrand -Stock 0 -Token $adminToken -Update;

			# Act / Assert
			{ Invoke-RestMethod -Method Post -Uri $uri -Body $body -Headers $headers; } | Should Throw "400";
		}

		It "Create-CoffeeOrderAsOtherUserThrows403" -test {
			# Arrange
			$otherUserName = <EMAIL>" -f [Guid]::NewGuid();
			$otherUserPassword = "<PASSWORD>";
			
			$otherUser = CRUD-User -UserName $otherUserName -Password $otherUserPassword -Create;
			$body["UserId"] = $otherUser.Id;

			# Act / Assert
			{ Invoke-RestMethod -Method Post -Uri $uri -Body $body -Headers $headers; } | Should Throw "403";
		}

		It "Create-CoffeeOrderWithoutCoffeeIdThrows400" -test {
			# Arrange
			$body.Remove("CoffeeId");

			# Act / Assert
			{ $result = Invoke-RestMethod -Method Post -Uri $uri -Body $body -Headers $headers } | Should Throw "400";
		}

		It "Create-CoffeeOrderWithoutUserIdThrows400" -test {
			# Arrange
			$body.Remove("UserId");

			# Act / Assert
			{ $result = Invoke-RestMethod -Method Post -Uri $uri -Body $body -Headers $headers } | Should Throw "400";
		}

		It "Create-CoffeeOrderAsOtherUserThrows403" -test {
			# Arrange
			$body["UserId"] = $secondUser.Id;

			# Act / Assert
			{ $result = Invoke-RestMethod -Method Post -Uri $uri -Body $body -Headers $headers } | Should Throw "403";
		}


		#>
	}

	Context "Update-CoffeeOrder" {
		BeforeEach {
			$coffeeOrdersUpdateUri = "{0}{1}" -f $baseUri, "CoffeeOrders"

			$name = "$entityPrefix-{0}" -f [guid]::NewGuid();

			$authString = "bearer {0}" -f $token;

			$headers = [System.Collections.Generic.Dictionary[[String],[String]]]::New();
			$headers.Add("Authorization", $authString);

			$body = @{
				Name = $name
				UserId = $user.Id
				CoffeeId = $coffee.Id;
			}

			$coffeeOrder = Invoke-RestMethod -Method Post -Uri $coffeeOrdersUpdateUri -Body $body -Headers $headers;
		}
		
		It "Warmup" -Test {
			$true | Should Be $true;
		}

		It "Update-CoffeeOrderCoffeeForeignKeySucceeds" -test {
			# Arrange
			$coffeeOrdersUpdateUri = "{0}({1})" -f $coffeeOrdersUpdateUri, $coffeeOrder.Id;

			# # Create Coffee
			$newCoffeeName = "$entityPrefix-{0}" -f [guid]::NewGuid();
			$newCoffeeBrand = "Test-Brand-{0}" -f [guid]::NewGuid();

			$newCoffee = CRUD-Coffee -Name $newCoffeeName -Brand $newCoffeeBrand -Token $adminToken -Create;

			$body["CoffeeId"] = $newCoffee.Id;

			$updatedCoffeeOrderBodyJson = $body | ConvertTo-Json;

			$authString = "bearer {0}" -f $token;

			$headers = [System.Collections.Generic.Dictionary[[String],[String]]]::New();
			$headers.Add("Authorization", $authString);
			$headers.Add("Content-Type", "application/json");

			# Act
			Invoke-RestMethod -Method Put -Uri $coffeeOrdersUpdateUri -Body $updatedCoffeeOrderBodyJson -Headers $headers;

			# Assert
			$result = Invoke-RestMethod -Method Get -Uri $coffeeOrdersUpdateUri -Headers $headers;
			$result | Should Not Be $null;
			$result.UserId | Should Be $user.Id;
			$result.CoffeeId | Should Be $newCoffee.Id;
		}

	<#
		It "Update-CoffeeOrderChangeCoffeeOrderIdThrows" -test {
			# Arrange
			$uri = "{0}{1}({2}L)" -f $baseUri, "CoffeeOrders", $coffeeOrder.Id;
			$coffeeOrder.Id = $coffeeOrder.Id + 1;
			$coffeeOrderJson = $coffeeOrder | ConvertTo-Json;

			# Act / Assert
			{ Invoke-RestMethod -Method Put -Uri $uri -Body $coffeeOrderJson -Headers $headers } | Should Throw;
		}

	#>
	}
	AfterAll {<commit_msg>FIX PESTER TEST<commit_after>. ../Functions/Delete-Entities.ps1
. ../Functions/Get-Token.ps1
. ../Functions/CRUD-User.ps1
. ../Functions/CRUD-Coffee.ps1

$usedEntitieSets = @("Coffees", "Users", "CoffeeOrders");
$entityPrefix = "CoffeeOrdersIntegrationTest";
$baseUri = "CoffeeTracker/api/";

Describe "CoffeeOrdersController" -Tags "CoffeeOrdersController" {
	$adminName = "<EMAIL>";
	$adminPassword = "<PASSWORD>";
	$adminToken = Get-Token -UserName $adminName -Password $adminPassword;
	
	BeforeEach {
		# Create user
		$userName = <EMAIL>" -f [guid]::NewGuid();;
		$userPassword = "<PASSWORD>";

		$user = CRUD-User -UserName $userName -Password $userPassword -Create;
		$token = Get-Token -UserName $userName -Password $userPassword;

		# Create second user
		$userSecondName = <EMAIL>" -f [guid]::NewGuid();;
		$userSecondPassword = "<PASSWORD>";

		$Seconduser = CRUD-User -UserName $userSecondName -Password $<PASSWORD> -Create;
		$SecondUsertoken = Get-Token -UserName $userSecondName -Password $<PASSWORD>;

		# Create Coffee
		$coffeeName = "$entityPrefix-{0}" -f [guid]::NewGuid();
		$coffeeBrand = "Test-Brand-{0}" -f [guid]::NewGuid();
		$coffeeStock = 10;

		$coffee = CRUD-Coffee -Name $coffeeName -Brand $coffeeBrand -Stock $coffeeStock -Token $adminToken -Create;
	}

	Context "Create-CoffeeOrder" {
		<#
		BeforeEach {
			$uri = "{0}{1}" -f $baseUri, "CoffeeOrders"
			$name = "$entityPrefix-{0}" -f [guid]::NewGuid();

			$authString = "bearer {0}" -f $token;

			$headers = [System.Collections.Generic.Dictionary[[String],[String]]]::New();
			$headers.Add("Authorization", $authString);

			$body = @{
				Name = $name
				UserId = $user.Id
				CoffeeId = $coffee.Id
			}
		}

		It "Warmup" -Test {
			$true | Should Be $true;
		}

		It "Create-CoffeeOrderDecreasesStockFromCoffeeSucceeds" -test {
			# Arrange
			# N/A

			# Act
			$result = Invoke-RestMethod -Method Post -Uri $uri -Body $body -Headers $headers;

			# Assert
			$coffeeUri = "{0}{1}({2})" -f $baseUri, "Coffees", $coffee.Id;
			$resultCoffee = Invoke-RestMethod -Method Get -Uri $coffeeUri -Headers $headers;
			$expectedStock = $coffeeStock - 1;

			$result | Should Not Be $null;
			$result.UserId | Should Be $user.Id;
			$result.CoffeeId | Should Be $coffee.Id;
			$resultCoffee.Stock | Should Be $expectedStock;
		}

		It "Create-CoffeeOrderWithCoffeeNotOnStockThrows400" -test {
			# Arrange
			CRUD-Coffee -Name $coffeeName -Brand $coffeeBrand -Stock 0 -Token $adminToken -Update;

			# Act / Assert
			{ Invoke-RestMethod -Method Post -Uri $uri -Body $body -Headers $headers; } | Should Throw "400";
		}

		It "Create-CoffeeOrderAsOtherUserThrows403" -test {
			# Arrange
			$otherUserName = <EMAIL>" -f [Guid]::NewGuid();
			$otherUserPassword = "<PASSWORD>";
			
			$otherUser = CRUD-User -UserName $otherUserName -Password $otherUserPassword -Create;
			$body["UserId"] = $otherUser.Id;

			# Act / Assert
			{ Invoke-RestMethod -Method Post -Uri $uri -Body $body -Headers $headers; } | Should Throw "403";
		}

		It "Create-CoffeeOrderWithoutCoffeeIdThrows400" -test {
			# Arrange
			$body.Remove("CoffeeId");

			# Act / Assert
			{ $result = Invoke-RestMethod -Method Post -Uri $uri -Body $body -Headers $headers } | Should Throw "400";
		}

		It "Create-CoffeeOrderWithoutUserIdThrows400" -test {
			# Arrange
			$body.Remove("UserId");

			# Act / Assert
			{ $result = Invoke-RestMethod -Method Post -Uri $uri -Body $body -Headers $headers } | Should Throw "400";
		}

		It "Create-CoffeeOrderAsOtherUserThrows403" -test {
			# Arrange
			$body["UserId"] = $secondUser.Id;

			# Act / Assert
			{ $result = Invoke-RestMethod -Method Post -Uri $uri -Body $body -Headers $headers } | Should Throw "403";
		}


		#>
	}

	Context "Update-CoffeeOrder" {
		BeforeEach {
			$coffeeOrdersUpdateUri = "{0}{1}" -f $baseUri, "CoffeeOrders"

			$name = "$entityPrefix-{0}" -f [guid]::NewGuid();

			$authString = "bearer {0}" -f $token;

			$headers = [System.Collections.Generic.Dictionary[[String],[String]]]::New();
			$headers.Add("Authorization", $authString);

			$body = @{
				Name = $name
				UserId = $user.Id
				CoffeeId = $coffee.Id;
			}

			$coffeeOrder = Invoke-RestMethod -Method Post -Uri $coffeeOrdersUpdateUri -Body $body -Headers $headers;
		}
		
		It "Warmup" -Test {
			$true | Should Be $true;
		}

		It "Update-CoffeeOrderCoffeeForeignKeySucceeds" -test {
			# Arrange
			$coffeeOrdersUpdateUri = "{0}({1})" -f $coffeeOrdersUpdateUri, $coffeeOrder.Id;

			# # Create Coffee
			$newCoffeeName = "$entityPrefix-{0}" -f [guid]::NewGuid();
			$newCoffeeBrand = "Test-Brand-{0}" -f [guid]::NewGuid();

			$newCoffee = CRUD-Coffee -Name $newCoffeeName -Brand $newCoffeeBrand -Token $adminToken -Create;

			$body["CoffeeId"] = $newCoffee.Id;

			$updatedCoffeeOrderBodyJson = $body | ConvertTo-Json;

			$authString = "bearer {0}" -f $token;

			$headers = [System.Collections.Generic.Dictionary[[String],[String]]]::New();
			$headers.Add("Authorization", $authString);
			$headers.Add("Content-Type", "application/json");

			# Act
			Invoke-RestMethod -Method Put -Uri $coffeeOrdersUpdateUri -Body $updatedCoffeeOrderBodyJson -Headers $headers;

			# Assert
			$result = Invoke-RestMethod -Method Get -Uri $coffeeOrdersUpdateUri -Headers $headers;
			$result | Should Not Be $null;
			$result.UserId | Should Be $user.Id;
			$result.CoffeeId | Should Be $newCoffee.Id;
		}

		It "Update-CoffeeOrderUserIdThrows403" -test {
			# Arrange
			$body["UserId"] = $secondUser.Id;
			$coffeeOrdersUpdateUri = "{0}({1})" -f $coffeeOrdersUpdateUri, $coffeeOrder.Id;

			$updatedCoffeeBodyJson = $body | ConvertTo-Json;

			$authString = "bearer {0}" -f $token;
			$headers = [System.Collections.Generic.Dictionary[[String],[String]]]::New();
			$headers.Add("Authorization", $authString);
			$headers.Add("Content-Type", "application/json");

			# Act / Assert
			{ Invoke-RestMethod -Method Put -Uri $coffeeOrdersUpdateUri -Headers $headers -Body $updatedCoffeeBodyJson } | Should Throw "403";
		}

	#>
	}
	AfterAll {
{'fraction_non_alphanumeric': 0.16052720513687058, 'fraction_numerical': 0.010561000337952011, 'mean_word_length': 3.729125049940072, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '685221', 'n_tokens_mistral': 4908, 'n_tokens_neox': 4123, 'n_words': 918}