From c1b829ab617317c03f6130441b3e1e55bd3d9263 Mon Sep 17 00:00:00 2001 From: Daniel-I-Am Date: Sat, 29 Aug 2020 15:30:45 +0200 Subject: [PATCH] Add tests for authorization --- tests/Feature/AuthorizationTest.php | 48 +++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 tests/Feature/AuthorizationTest.php diff --git a/tests/Feature/AuthorizationTest.php b/tests/Feature/AuthorizationTest.php new file mode 100644 index 0000000..10fe7e0 --- /dev/null +++ b/tests/Feature/AuthorizationTest.php @@ -0,0 +1,48 @@ +authToken = AuthToken::create([ + 'name' => 'AuthorizationTestToken'.Str::random(10), + 'token' => Str::random(config('chaosapi.token-length')) + ]); + } + + public function test400OnBearerFormattedIncorrect() + { + $this->withHeader('Authorization', 'asdf')->get('/api/v1/status')->assertStatus(400); + $this->withHeader('Authorization', $this->authToken->token)->get('/api/v1/status')->assertStatus(400); + } + + public function test401OnAuthorizationWrong() + { + $this->withHeader('Authorization', 'Bearer '.$this->authToken->token.'a')->get('/api/v1/status')->assertUnauthorized(); + } + + public function test403OnAuthorizationMissing() + { + $this->get('/api/v1/status')->assertForbidden(); + } + + public function test200OnAuthorizationCorrect() + { + $this->withHeader('Authorization', 'Bearer '.$this->authToken->token)->get('/api/v1/status')->assertOk(); + } +}