Giter Site home page Giter Site logo

Comments (3)

tymondesigns avatar tymondesigns commented on May 7, 2024

I think you will find a good answer (if you haven't already) over at Laracasts :)

from jwt-auth.

jwigal avatar jwigal commented on May 7, 2024

Banged my head for a while trying to get some basic functional / integration tests written. Here's what I came up with that seems to be working for Laravel 5.1:

    class APIControllerTest extends TestCase{


        public function testCanGetOAuthTokenWithValidPassword(){
            $this->setupUser();
            $resp = $this->call('POST', '/api/login', 
                ["userid" => "testymctester", "password" => "testallthethings"]);
            $data = json_decode($resp->getContent());
            $this->assertTrue(array_key_exists("token", $data), "response was {$resp->getContent()}");
        }

        public function testCannotGetOAuthTokenWithInvalidPassword(){
            $this->setupUser();
            $resp = $this->call('POST', '/api/login', 
                ["userid" => "testymctester", "password" => "badpassword"]);
            $data = json_decode($resp->getContent());
            $this->assertFalse(array_key_exists("token", $data), "response was {$resp->getContent()}");
        }

        /**
         * A valid user can view a protected resource. 
         * @return [type] [description]
         */
        public function testCanGetProtectedAPIResourceWithOAuthTokenViaHeader(){
            $user = $this->setupUser();
            $token = JWTAuth::fromUser($user);
            $resp = $this->call('GET', '/api/user', [],[],[],["HTTP_AUTHORIZATION" => "Bearer $token"]);
            $this->assertResponseOk();
            $data = json_decode($resp->getContent());
            $this->assertTrue(!!$data->user, "data is {$resp->getContent()}");
        }


        public function testCanGetProtectedAPIResourceWithOAuthTokenViaParam(){
            $user = $this->setupUser();
            $token = JWTAuth::fromUser($user);
            $resp = $this->call('GET', '/api/user', ["token" => $token]);
            $this->assertResponseOk();
            $data = json_decode($resp->getContent());
            $this->assertTrue(!!$data->user, "data is {$resp->getContent()}");
        }



        /**
         * If an invalid token is passed, user can't view a protected resource.
         * @return [type] [description]
         */
        public function testCannotGetProtectedAPIResourceWithoutOAuthToken(){
            // $this->markTestSkipped("testing protected routes does not work till upgrade to Dingo.");
            $resp = $this->call('GET', '/api/user', 
                ["token" => "bogus.bogus.bogus"]);
            $this->assertResponseStatus(401);
        }


        protected function setupUser(){
            $user = User::whereUserid("testymctester");
            if ($user) $user->delete();
            $user = new User([
                "userid" => "testymctester",
                "password" => "testallthethings",
                "email" => "[email protected]",
                "first" => "T",
                "last" => "McTester",
            ]);
            $user->save();
            return $user;       
        }


    }

Our routes.php file looks something like this:

    $api = App::make('Dingo\Api\Routing\Router');

        $api->version('v1', function($api){

            $api->post("login", "App\Http\Controllers\Auth\APIController@authenticate");

            // Protected routes that require authentication
            $api->group(["protected" => true], function($api){
                $api->get('user', 'App\Http\Controllers\Auth\APIController@getAuthenticatedUser');
                $api->get('userinfo', 'App\Http\Controllers\Auth\APIController@getUserInfo');
                $api->get('dates', 'App\Http\Controllers\Auth\APIController@getCalendar');
                $api->get('directory', 'App\Http\Controllers\Auth\APIController@getDirectory');
                $api->get('news', 'App\Http\Controllers\Auth\APIController@getNews');
            });

        });

from jwt-auth.

jadjoubran avatar jadjoubran commented on May 7, 2024

Hope this helps, in my TestCase.php I setup the token that I needed to use inside $this->token
and then I setup helper methods
authGet, authPost, authPut & authDelete which would call get, post, put & delete with authentication.

The trick was to append ?token=$this->token to the url for all of these methods (as mentioned here)

from jwt-auth.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.