|
| 1 | +$here = (Split-Path -Parent $MyInvocation.MyCommand.Path) |
| 2 | +$src = Split-Path -Path $here -Parent |
| 3 | +. $src\doc-downloader.functions.ps1 |
| 4 | + |
| 5 | +$testfile = ($here + "\LogFile.txt") |
| 6 | + |
| 7 | +$API_KEY = "api-keys-are-cool" |
| 8 | +$BASE_URL = 'http://localhost:5000' |
| 9 | +$DRIVE_AXLE_HEADERS = @{ Authorization = ("DriveAxleKey key=" + $API_KEY) |
| 10 | + Accept = 'application/json'} |
| 11 | + |
| 12 | +if((Test-Path $testfile) -ne $True){ |
| 13 | + New-Item -Path $testfile -ItemType File |
| 14 | +} |
| 15 | + |
| 16 | +Describe "Consume API Function Tests" { |
| 17 | + Context 'Testing GetNextDoc function' { |
| 18 | + it 'GetNextDoc should return a 302 if there is a document in the queue' { |
| 19 | + $request = $BASE_URL + '/api/v1/documents/queued/next' |
| 20 | + $response = GetNextDoc $request $DRIVE_AXLE_HEADERS $testfile |
| 21 | + $response.StatusCode | should -Be 302 |
| 22 | + } |
| 23 | + it 'GetNextDoc should return a 304 if there is not a document in the queue' { |
| 24 | + $request = $BASE_URL + '/api/v1/documents/queued/next/empty' |
| 25 | + $response = GetNextDoc $request $DRIVE_AXLE_HEADERS $testfile |
| 26 | + $response.StatusCode | should -Be 304 |
| 27 | + } |
| 28 | + |
| 29 | + it 'GetNextDoc should throw an expection if a 404 status code is returned' { |
| 30 | + $request = $BASE_URL + '/api/v1/documents/queued/next/fail' |
| 31 | + $exception = $false |
| 32 | + try { |
| 33 | + GetNextDoc $request $DRIVE_AXLE_HEADERS $testfile |
| 34 | + } |
| 35 | + catch { |
| 36 | + $exception = $_.CategoryInfo.Reason -eq 'WebException' |
| 37 | + } |
| 38 | + $exception | Should -Be $true |
| 39 | + } |
| 40 | + |
| 41 | + |
| 42 | + it 'GetNextDoc should throw an exception if a 500 status code is returned' { |
| 43 | + $request = $BASE_URL + '/api/v1/documents/queued/next/badserver' |
| 44 | + $exception = $false |
| 45 | + try{ |
| 46 | + $request = GetNextDoc $request $DRIVE_AXLE_HEADERS $testfile -ErrorAction SilentlyContinue |
| 47 | + } |
| 48 | + catch { |
| 49 | + $exception = $_.CategoryInfo.Reason -eq "WebException" |
| 50 | + } |
| 51 | + $exception | Should -Be $true |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + Context 'Testing GetDocFromQueue function' { |
| 56 | + it 'GetDocFromQueue should return a 200 with a URL to download document' { |
| 57 | + $request = $BASE_URL + '/api/v1/documents/queued/1' |
| 58 | + $response = GetDocFromQueue $request $DRIVE_AXLE_HEADERS $testfile |
| 59 | + $response.StatusCode | should -Be 200 |
| 60 | + $response = $response | ConvertFrom-Json |
| 61 | + $response.downloadUrl | should -Not -Be $null |
| 62 | + } |
| 63 | + |
| 64 | + it 'GetDocFromQueue should throw an exception if a 404 status code is returned' { |
| 65 | + $request = $BASE_URL + '/api/v1/documents/queued/2' |
| 66 | + $exception = $false |
| 67 | + try{ |
| 68 | + $request = GetNextDoc $request $DRIVE_AXLE_HEADERS $testfile -ErrorAction SilentlyContinue |
| 69 | + } |
| 70 | + catch { |
| 71 | + $exception = $_.CategoryInfo.Reason -eq 'WebException' |
| 72 | + } |
| 73 | + $exception | Should -Be $true |
| 74 | + } |
| 75 | + |
| 76 | + it 'GetDocFromQueue should throw an exception if a 500 status code is returned' { |
| 77 | + $request = $BASE_URL + '/api/v1/documents/queued/2' |
| 78 | + $exception = $false |
| 79 | + try{ |
| 80 | + $request = GetNextDoc $request $DRIVE_AXLE_HEADERS $testfile -ErrorAction SilentlyContinue |
| 81 | + } |
| 82 | + catch { |
| 83 | + $exception = $_.CategoryInfo.Reason -eq 'WebException' |
| 84 | + } |
| 85 | + $exception | Should -Be $true |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + Context 'Testing RemoveDocFromQueue function' { |
| 90 | + it 'RemoveDocFromQueue should return a 200 if a document was successfully removed from the queue' { |
| 91 | + $request = $BASE_URL + '/api/v1/documents/queued/1' |
| 92 | + $response = RemoveDocFromQueue $request $DRIVE_AXLE_HEADERS $testfile |
| 93 | + $response = $response | ConvertFrom-Json |
| 94 | + $response.message | Should -Be "Document deleted successfully." |
| 95 | + } |
| 96 | + it 'RemoveDocFromQueue should return null if the document to be removed could not be found' { |
| 97 | + $request = $BASE_URL + '/api/v1/documents/queued/2' |
| 98 | + $response = RemoveDocFromQueue $request $DRIVE_AXLE_HEADERS $testfile |
| 99 | + $response | should -Be $null |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + Context 'Testing ExponentialDeleteRetry function' { |
| 104 | + it 'ExpWait should return a null if the document is never removed after the multiple retires due to 404 not found' { |
| 105 | + $request = $BASE_URL + '/api/v1/documents/queued/3' |
| 106 | + $response = ExponentialDeleteRetry $request $DRIVE_AXLE_HEADERS $testfile |
| 107 | + $response | should -Be $null |
| 108 | + } |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | + |
0 commit comments