<commit_before>"use strict";

var sira = require('sira');
var s = require('./../support');
var t = s.t;

describe('User', function () {
    var validCredentials = {email: '<EMAIL>', password: '<PASSWORD>'};
    var validCredentialsWithTTL = {email: '<EMAIL>', password: '<PASSWORD>', ttl: 3600};
//    var invalidCredentials = {email: '<EMAIL>', password: '<PASSWORD>'};

    var User, AccessToken;
    var app;

    beforeEach(function (done) {
        app = s.sapp();
        app.boot(done);
    });

    beforeEach(function (done) {
        User = app.models.User;
        AccessToken = app.models.AccessToken;
        User.create(validCredentials, done);
    });

    afterEach(function (done) {
        s.cleanup(app, done);
    });

    describe('User.create', function () {
        it('Create a new user', function (done) {
            User.create({email: '<EMAIL>', password: '<PASSWORD>'}, function (err, user) {
                t(!err);
                t(user.id);
                t(user.email);
                done();
            });
        });

        it('Email can not be blank', function (done) {
            User.create({password: '<PASSWORD>'}, function (err) {
                t.ok(err);
                done();
            });
        });

        it('Requires a valid email', function (done) {
            User.create({email: 'foo@', password: '<PASSWORD>'}, function (err) {
                t(err);
                done();
            });
        });

        it('Hashes the given password', function () {
            var u = new User({username: 'foo', password: '<PASSWORD>'});
            t(u.password !== 'bar');
        });
    });


    describe('User.login', function () {
        it('Login a user by providing credentials', function (done) {
            User.login(validCredentials, function (err, accessToken) {
                t(accessToken.userId);
                t(accessToken.id);
                t.equal(accessToken.id.length, 64);

                done();
            });
        });

        it('Login a user by providing credentials with TTL', function (done) {
            User.login(validCredentialsWithTTL, function (err, accessToken) {
                t(accessToken.userId);
                t(accessToken.id);
                t.equal(accessToken.ttl, validCredentialsWithTTL.ttl);
                t.equal(accessToken.id.length, 64);

                done();
            });
        });

        it('Login a user using a custom createAccessToken', function (done) {
            var createToken = User.prototype.createAccessToken; // Save the original method
            // Override createAccessToken
            User.prototype.createAccessToken = function (ttl, cb) {
                // Reduce the ttl by half for testing purpose
                this.accessTokens.create({ttl: ttl / 2 }, cb);
            };
            User.login(validCredentialsWithTTL, function (err, accessToken) {
                t(accessToken.userId);
                t(accessToken.id);
                t.equal(accessToken.ttl, 1800);
                t.equal(accessToken.id.length, 64);

                User.findById(accessToken.userId, function (err, user) {
                    user.createAccessToken(120, function (err, accessToken) {
                        t(accessToken.userId);
                        t(accessToken.id);
                        t.equal(accessToken.ttl, 60);
                        t.equal(accessToken.id.length, 64);
                        // Restore create access token
                        User.prototype.createAccessToken = createToken;
                        done();
                    });
                });
            });
        });
    });

    describe('User.logout', function() {
        function verify(token, done) {
            t(token);

            return function (err) {
                if(err) return done(err);

                AccessToken.findById(token, function (err, accessToken) {
                    t(!accessToken, 'accessToken should not exist after logging out');
                    done(err);
                });
            }
        }

        it('Logout a user by providing the current accessToken id (using node)', function (done) {
            login(logout);

            function login(fn) {
                User.login({email: '<EMAIL>', password: '<PASSWORD>'}, fn);
            }

            function logout(err, accessToken) {
                User.logout(accessToken.id, verify(accessToken.id, done));
            }
        });

        it('Logout a user by providing the current accessToken id (over handler)', function(done) {
            login(logout);
            function login(fn) {
                sira.rekuest('user.login', {email: '<EMAIL>', password: '<PASSWORD>'})
                    .send(app, function (err, accessToken) {
                        if(err) return done(err);
<commit_msg>separate payload and params<commit_after>"use strict";

var sira = require('sira');
var s = require('./../support');
var t = s.t;

describe('User', function () {
    var validCredentials = {email: '<EMAIL>', password: '<PASSWORD>'};
    var validCredentialsWithTTL = {email: '<EMAIL>', password: '<PASSWORD>', ttl: 3600};
//    var invalidCredentials = {email: '<EMAIL>', password: '<PASSWORD>'};

    var User, AccessToken;
    var app;

    beforeEach(function (done) {
        app = s.sapp();
        app.boot(done);
    });

    beforeEach(function (done) {
        User = app.models.User;
        AccessToken = app.models.AccessToken;
        User.create(validCredentials, done);
    });

    afterEach(function (done) {
        s.cleanup(app, done);
    });

    describe('User.create', function () {
        it('Create a new user', function (done) {
            User.create({email: '<EMAIL>', password: '<PASSWORD>'}, function (err, user) {
                t(!err);
                t(user.id);
                t(user.email);
                done();
            });
        });

        it('Email can not be blank', function (done) {
            User.create({password: '<PASSWORD>'}, function (err) {
                t.ok(err);
                done();
            });
        });

        it('Requires a valid email', function (done) {
            User.create({email: 'foo@', password: '<PASSWORD>'}, function (err) {
                t(err);
                done();
            });
        });

        it('Hashes the given password', function () {
            var u = new User({username: 'foo', password: '<PASSWORD>'});
            t(u.password !== 'bar');
        });
    });


    describe('User.login', function () {
        it('Login a user by providing credentials', function (done) {
            User.login(validCredentials, function (err, accessToken) {
                t(accessToken.userId);
                t(accessToken.id);
                t.equal(accessToken.id.length, 64);

                done();
            });
        });

        it('Login a user by providing credentials with TTL', function (done) {
            User.login(validCredentialsWithTTL, function (err, accessToken) {
                t(accessToken.userId);
                t(accessToken.id);
                t.equal(accessToken.ttl, validCredentialsWithTTL.ttl);
                t.equal(accessToken.id.length, 64);

                done();
            });
        });

        it('Login a user using a custom createAccessToken', function (done) {
            var createToken = User.prototype.createAccessToken; // Save the original method
            // Override createAccessToken
            User.prototype.createAccessToken = function (ttl, cb) {
                // Reduce the ttl by half for testing purpose
                this.accessTokens.create({ttl: ttl / 2 }, cb);
            };
            User.login(validCredentialsWithTTL, function (err, accessToken) {
                t(accessToken.userId);
                t(accessToken.id);
                t.equal(accessToken.ttl, 1800);
                t.equal(accessToken.id.length, 64);

                User.findById(accessToken.userId, function (err, user) {
                    user.createAccessToken(120, function (err, accessToken) {
                        t(accessToken.userId);
                        t(accessToken.id);
                        t.equal(accessToken.ttl, 60);
                        t.equal(accessToken.id.length, 64);
                        // Restore create access token
                        User.prototype.createAccessToken = createToken;
                        done();
                    });
                });
            });
        });
    });

    describe('User.logout', function() {
        function verify(token, done) {
            t(token);

            return function (err) {
                if(err) return done(err);

                AccessToken.findById(token, function (err, accessToken) {
                    t(!accessToken, 'accessToken should not exist after logging out');
                    done(err);
                });
            }
        }

        it('Logout a user by providing the current accessToken id (using node)', function (done) {
            login(logout);

            function login(fn) {
                User.login({email: '<EMAIL>', password: 'bar'}, fn);
            }

            function logout(err, accessToken) {
                User.logout(accessToken.id, verify(accessToken.id, done));
            }
        });

        it('Logout a user by providing the current accessToken id (over handler)', function(done) {
            login(logout);
            function login(fn) {
                sira.rekuest('user.login')
                    .payload({email: '<EMAIL>', password: '<PASSWORD>'})
                    .send(app, function (err, accessToken) {
                        if(err) return done(err);

{'fraction_non_alphanumeric': 0.13348277970246586, 'fraction_numerical': 0.004483391073975953, 'mean_word_length': 1.5958741073790004, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 32, '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': '2850487', 'n_tokens_mistral': 2662, 'n_tokens_neox': 2528, 'n_words': 601}