Search code examples
javascriptgetjasmine

make http get call after sigin in with jasmine and request


I'm adding tests to an application that already (partially) exists. It was written using angular and php/MariaDB in the backend. I'm now working on the http calls to the server. I plan to use jasmine with request.

I was able to make some simple tests, and can login. But I cannot test the pages that require to be logged in. I cannot find a way to add the token to the calls.

If I understand things correctly, on the received message from the sig in I should get a token that I should then use in the following calls. Who wrote the app followed the instructions given by the angular documentation which handles everything, so we are learning toguether how things really work under the hood.

Going through the received answer on the login, the only thing that looks like a token is a cookie set in the header, whose name is 'PHPSESSID'. I read and parse that cookie to get the token and make the next call like this:

        request.get(
        {
            url: 'http://xxx.xxx.com/php/authentication/session.php',
            'auth': {
                'bearer': mytoken
            }
        }, function(err, res) {
                console.log(res['body']);
                done();
        })

the response is what I should get if the user is NOT logged in.

Using Postman, everything works. Aparently it saves the token and uses it for the next call. That is, f I make the sign in and then make the get call to session.php I get the correct answer. I just cannot figure out what exact call postman makes or how to use the token in the next call using jasmine and request.


Solution

  • Since the token was passed as a cookie on the response call, what I was supposed to do was set the same cookie in the next call. Here is the code of the whole test just in case somebody needs a hand. First I sign in, then I made a call to an address that should return my email if indeed I'm logged in.

    var request = require('request');
    
    describe("login test", function() {
    
        it("should log in", (done) => {
            var user = {'email':'[email protected]', 'password':'blablabla'};
    
            request.post(
            {
                url: 'http://xxx/test/public/php/authentication/login.php',
                body: JSON.stringify(user)
            },
            (err, res) => {
                var result = JSON.parse(res['body']);
                var cookieString = res['headers']['set-cookie'];
                expect(result['success']).toBe(true);
    
                request.get(
                {
                    url: 'http://xxx/test/public/php/authentication/session.php',
                    headers: {
                        'Cookie': cookieString
                    }
                }, function(err, res) {
                        var result = JSON.parse(res['body']);
                        expect(result.user.email).toBe(user.email);
                        done();
                })
    
            });
        });
    });