{"metadata":{"image":[],"title":"","description":""},"api":{"url":"","auth":"required","results":{"codes":[]},"settings":"","params":[]},"next":{"description":"","pages":[]},"title":"JWT Auth Tests","type":"basic","slug":"jwt-auth-tests","excerpt":"","body":"When doing TDD, most of our tests would usually cover protected endpoints. Endpoints that require authentication. This repository provides test helpers for protected endpoints, without having to deal with manually sending the token.\n\n# Available methods\n[block:callout]\n{\n  \"type\": \"info\",\n  \"body\": \"The below methods have the same signature as Laravel's test helper methods (get, post, put, delete, call).\"\n}\n[/block]\n\n[block:parameters]\n{\n  \"data\": {\n    \"0-0\": \"authUserGet()\",\n    \"0-1\": \"GET with authentication\",\n    \"1-0\": \"authUserPost()\",\n    \"1-1\": \"POST with authentication\",\n    \"2-0\": \"authUserPut()\",\n    \"2-1\": \"PUT with authentication\",\n    \"3-0\": \"authUserDelete()\",\n    \"3-1\": \"DELETE with authentication\",\n    \"4-0\": \"authUserCall()\",\n    \"4-1\": \"Custom method, cookies, files, server, etc.. with authentication\"\n  },\n  \"cols\": 2,\n  \"rows\": 5\n}\n[/block]\n\n[block:callout]\n{\n  \"type\": \"success\",\n  \"body\": \"You can access the current authenticated user (which is a model factory) using `$this->getAuthUser()` and the token using `$this->getAuthUserToken()`. They are available in `/tests/TestCase.php`.\"\n}\n[/block]\n# Example\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<?php\\n\\n$api->group(['middleware' => 'api.auth'], function ($api) {\\n\\n    $api->post('posts', 'PostsController:::at:::create');\\n\\n});\",\n      \"language\": \"php\",\n      \"name\": \"Routes.php\"\n    }\n  ]\n}\n[/block]\n\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<?php\\n\\nclass FavoritePostsTest extends TestCase\\n{\\n public function testCreatingNewPost()\\n    {\\n        $post = factory(App\\\\Post::class)->make();\\n      \\n        $this->authUserPost('/api/posts', ['name' => $post->name])\\n        ->seeApiSuccess()\\n        ->seeJsonObject('post')\\n        ->seeJsonKeyValueString('name', $post->name);\\n    }\\n  \\n  \\n}\",\n      \"language\": \"php\",\n      \"name\": \"FavoritePostsTest\"\n    }\n  ]\n}\n[/block]\n# Approach for multi-tenant applications\n\nSay you have mentors and mentees on your platform. Wouldn't you love to have the following test helpers?\n\n`->mentorGet()`, `->mentorPost()`, `->menteeGet()`, `->menteePut()`, etc..\n\nIf you open `TestCase.php`, you can see that we're manually logging in the user by calling `$this->setAuthUserToken()`. This is where we create a factory of User.\n\nYour approach would be to go through all of the authUser methods and variables and rename them to mentor, and then modify the setMentorToken (previously setAuthUserToken) to generate a factory of type user which is always a mentor:\n```\n $mentor = factory(App\\User::class)->create(['type' => 'mentor']);\n```\nAnd then do the same for the mentee.","updates":["574b402bf2070e17000fc89c"],"order":1,"isReference":false,"hidden":false,"sync_unique":"","link_url":"","link_external":false,"_id":"57ffd85f755a2e1700c2bb1d","category":{"sync":{"isSync":false,"url":""},"pages":[],"title":"Testing","slug":"testing","order":6,"from_sync":false,"reference":false,"_id":"57ffd85f755a2e1700c2bb0c","project":"567083021dabd80d00b122ae","createdAt":"2015-12-18T23:14:31.389Z","version":"57ffd85f755a2e1700c2bb05","__v":0},"version":{"version":"3.4","version_clean":"3.4.0","codename":"","is_stable":true,"is_beta":false,"is_hidden":false,"is_deprecated":false,"categories":["57ffd85f755a2e1700c2bb06","57ffd85f755a2e1700c2bb07","57ffd85f755a2e1700c2bb08","57ffd85f755a2e1700c2bb09","57ffd85f755a2e1700c2bb0a","57ffd85f755a2e1700c2bb0b","57ffd85f755a2e1700c2bb0c","57ffd85f755a2e1700c2bb0d"],"_id":"57ffd85f755a2e1700c2bb05","project":"567083021dabd80d00b122ae","releaseDate":"2016-10-13T18:54:23.018Z","__v":1,"createdAt":"2016-10-13T18:54:23.018Z"},"githubsync":"","parentDoc":null,"project":"567083021dabd80d00b122ae","__v":0,"user":"567082433a32d20d00c45cab","createdAt":"2015-12-18T23:26:36.719Z"}
When doing TDD, most of our tests would usually cover protected endpoints. Endpoints that require authentication. This repository provides test helpers for protected endpoints, without having to deal with manually sending the token. # Available methods [block:callout] { "type": "info", "body": "The below methods have the same signature as Laravel's test helper methods (get, post, put, delete, call)." } [/block] [block:parameters] { "data": { "0-0": "authUserGet()", "0-1": "GET with authentication", "1-0": "authUserPost()", "1-1": "POST with authentication", "2-0": "authUserPut()", "2-1": "PUT with authentication", "3-0": "authUserDelete()", "3-1": "DELETE with authentication", "4-0": "authUserCall()", "4-1": "Custom method, cookies, files, server, etc.. with authentication" }, "cols": 2, "rows": 5 } [/block] [block:callout] { "type": "success", "body": "You can access the current authenticated user (which is a model factory) using `$this->getAuthUser()` and the token using `$this->getAuthUserToken()`. They are available in `/tests/TestCase.php`." } [/block] # Example [block:code] { "codes": [ { "code": "<?php\n\n$api->group(['middleware' => 'api.auth'], function ($api) {\n\n $api->post('posts', '[email protected]');\n\n});", "language": "php", "name": "Routes.php" } ] } [/block] [block:code] { "codes": [ { "code": "<?php\n\nclass FavoritePostsTest extends TestCase\n{\n public function testCreatingNewPost()\n {\n $post = factory(App\\Post::class)->make();\n \n $this->authUserPost('/api/posts', ['name' => $post->name])\n ->seeApiSuccess()\n ->seeJsonObject('post')\n ->seeJsonKeyValueString('name', $post->name);\n }\n \n \n}", "language": "php", "name": "FavoritePostsTest" } ] } [/block] # Approach for multi-tenant applications Say you have mentors and mentees on your platform. Wouldn't you love to have the following test helpers? `->mentorGet()`, `->mentorPost()`, `->menteeGet()`, `->menteePut()`, etc.. If you open `TestCase.php`, you can see that we're manually logging in the user by calling `$this->setAuthUserToken()`. This is where we create a factory of User. Your approach would be to go through all of the authUser methods and variables and rename them to mentor, and then modify the setMentorToken (previously setAuthUserToken) to generate a factory of type user which is always a mentor: ``` $mentor = factory(App\User::class)->create(['type' => 'mentor']); ``` And then do the same for the mentee.