<commit_before>var should = require('chai').should();

describe('Grasshopper core - testing tokens', function(){
    'use strict';

    var grasshopper = require('../lib/grasshopper'),
        adminToken = '',
        readerToken = '',
        readerToken2 = '',
        readerToken3 = '',
        userId = '5245ce1d56c02c066b000001';

    before(function(done){

        grasshopper.configure(function(){
            this.config = {
                'crypto': {
                    'secret_passphrase' : '<PASSWORD>'
                },
                'db': {
                    'type': 'mongodb',
                    'host': 'mongodb://localhost:27017/test',
                    'database': 'test',
                    'username': '',
                    'password': '',
                    'debug': false
                }
            };
        });


        grasshopper.auth('admin', 'TestPassword').then(function(token){
                adminToken = token;
                grasshopper.auth('apitestuserreader', 'TestPassword').then(function(token){
                        readerToken = token;
                        grasshopper.auth('apitestuserreader', '<PASSWORD>').then(function(token){
                            readerToken2 = token;
                            grasshopper.auth('apitestuserreader', 'TestPassword').then(function(token){
                                readerToken3 = token;
                                done();
                            });
                        });
                    });
            });
    });

    describe('tokens.deleteById', function(){

        it('a user has to be logged in to use tokens.deleteById function.', function(done) {
            grasshopper.request().tokens.deleteById(readerToken).then(
                function(){
                    throw new Error('Payload should be null');
                },
                function(err){
                    err.errorCode.should.equal(401);
                }
            ).done(done);
        });

        it('a user has to have a valid auth token to use token.deleteById function.', function(done){
            grasshopper.request('12345678989').tokens.deleteById(readerToken).then(
                function(){
                    throw new Error('Payload should be null');
                },
                function(err){
                    err.errorCode.should.equal(401);
                }
            ).done(done);
        });

        it('an administrator (only) should be able to delete any token that is not their own.', function(done) {
            grasshopper.request(adminToken).tokens.deleteById(readerToken).then(
                function(payload){
                    payload.should.equal(true);
                },
                function(err){
                    should.not.exist(err);
                }
            ).done(done);
        });

        it('a user should not be able to delete a token that is not their own.', function(done) {
            grasshopper.request(readerToken2).tokens.deleteById(adminToken).then(
                function(payload){
                    should.not.exist(payload);
                },
                function(err){
                    err.errorCode.should.equal(403);
                }
            ).done(done);
        });

        it('a user should be able to authenticate then log themselves out.', function(done) {
            grasshopper.request(readerToken2).tokens.deleteById(readerToken2).then(
                function(payload){
                    payload.should.equal(true);
                },
                function(err){
                    should.not.exist(err);
                }
            ).done(done);
        });

        it('a user should be able to authenticate then log themselves out using a convenience "logout" method.', function(done) {
            grasshopper.auth('apitestuserreader', 'TestPassword').then(
                function(token){
                    grasshopper.request(token).tokens.logout().then(
                        function(payload){
                            payload.should.equal(true);
                        },
                        function(err){
                            should.not.exist(err);
                        }
                    ).done(done);
                },
                function(err){
                    should.not.exist(err);
                }
            ).done();
        });
    });

    describe('tokens.getNew', function(){

        it('a user should be able to create a new version of their token that they can use elsewhere', function(done) {
            grasshopper.request(readerToken3).tokens.getNew().then(
                function(payload){
                    payload.should.be.string;
                },
                function(err){
                    should.not.exist(err);
                }
            ).done(done);
        });

        it('if a user is currently not logged in then they should receive a 401 error.', function(done){
            grasshopper.request().tokens.getNew().then(
                function(payload){
                    should.not.exist(payload);
                },<commit_msg>Fixed test.<commit_after>var should = require('chai').should();

describe('Grasshopper core - testing tokens', function(){
    'use strict';

    var grasshopper = require('../lib/grasshopper'),
        adminToken = '',
        readerToken = '',
        readerToken2 = '',
        readerToken3 = '',
        userId = '5245ce1d56c02c066b000001';

    before(function(done){

        grasshopper.configure(function(){
            this.config = {
                'crypto': {
                    'secret_passphrase' : '<PASSWORD>'
                },
                'db': {
                    'type': 'mongodb',
                    'host': 'mongodb://localhost:27017/test',
                    'database': 'test',
                    'username': '',
                    'password': '',
                    'debug': false
                }
            };
        });


        grasshopper.auth('admin', 'TestPassword').then(function(token){
                adminToken = token;
                grasshopper.auth('apitestuserreader', 'TestPassword').then(function(token){
                        readerToken = token;
                        grasshopper.auth('apitestuserreader', 'TestPassword').then(function(token){
                            readerToken2 = token;
                            grasshopper.auth('apitestuserreader', 'TestPassword').then(function(token){
                                readerToken3 = token;
                                done();
                            });
                        });
                    });
            });
    });

    describe('tokens.deleteById', function(){

        it('a user has to be logged in to use tokens.deleteById function.', function(done) {
            grasshopper.request().tokens.deleteById(readerToken).then(
                function(){
                    throw new Error('Payload should be null');
                },
                function(err){
                    err.errorCode.should.equal(401);
                }
            ).done(done);
        });

        it('a user has to have a valid auth token to use token.deleteById function.', function(done){
            grasshopper.request('12345678989').tokens.deleteById(readerToken).then(
                function(){
                    throw new Error('Payload should be null');
                },
                function(err){
                    err.errorCode.should.equal(401);
                }
            ).done(done);
        });

        it('an administrator (only) should be able to delete any token that is not their own.', function(done) {
            grasshopper.request(adminToken).tokens.deleteById(readerToken).then(
                function(payload){
                    payload.should.equal('Success');
                },
                function(err){
                    should.not.exist(err);
                }
            ).done(done);
        });

        it('a user should not be able to delete a token that is not their own.', function(done) {
            grasshopper.request(readerToken2).tokens.deleteById(adminToken).then(
                function(payload){
                    should.not.exist(payload);
                },
                function(err){
                    err.errorCode.should.equal(403);
                }
            ).done(done);
        });

        it('a user should be able to authenticate then log themselves out.', function(done) {
            grasshopper.request(readerToken2).tokens.deleteById(readerToken2).then(
                function(payload){
                    payload.should.equal('Success');
                },
                function(err){
                    should.not.exist(err);
                }
            ).done(done);
        });

        it('a user should be able to authenticate then log themselves out using a convenience "logout" method.', function(done) {
            grasshopper.auth('apitestuserreader', '<PASSWORD>').then(
                function(token){
                    grasshopper.request(token).tokens.logout().then(
                        function(payload){
                            payload.should.equal('Success');
                        },
                        function(err){
                            should.not.exist(err);
                        }
                    ).done(done);
                },
                function(err){
                    should.not.exist(err);
                }
            ).done();
        });
    });

    describe('tokens.getNew', function(){

        it('a user should be able to create a new version of their token that they can use elsewhere', function(done) {
            grasshopper.request(readerToken3).tokens.getNew().then(
                function(payload){
                    payload.should.be.string;
                },
                function(err){
                    should.not.exist(err);
                }
            ).done(done);
        });

        it('if a user is currently not logged in then they should receive a 401 error.', function(done){
            grasshopper.request().tokens.getNew().then(
                function(payload){
                    should.not.exist(payload);
                },
{'fraction_non_alphanumeric': 0.11590712014644956, 'fraction_numerical': 0.010405626746314674, 'mean_word_length': 1.2768150910287344, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6293297', 'n_tokens_mistral': 2572, 'n_tokens_neox': 2335, 'n_words': 481}