<commit_before>/**
 * Copyright (c) 2014 "Fronteer LTD"
 * Grasshopper Event Engine
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

var _ = require('lodash');
var assert = require('assert');

var TestsUtil = require('gh-tests/lib/util');

/**
 * Assert that the me feed can be retrieved
 *
 * @param  {RestClient}         client                          The REST client to make the request with
 * @param  {Function}           callback                        Standard callback function
 * @param  {Object}             callback.me                     The retrieved me feed
 * @throws {AssertionError}                                     Error thrown when an assertion failed
 */
var assertGetMe = module.exports.assertGetMe = function(client, callback) {
    client.user.getMe(function(err, me) {
        assert.ok(!err);
        assert.ok(me);
        return callback(me);
    });
};

/**
 * Assert that a user can be updated
 *
 * @param  {RestClient}         client                          The REST client to make the request with
 * @param  {Number}             id                              The id of the user to update
 * @param  {Object}             update                          The updates to make
 * @param  {String}             [update.displayName]            The updated displayName
 * @param  {String}             [update.email]                  The updated email
 * @param  {String}             [update.emailPreference]        The updated email preference
 * @param  {Function}           callback                        Standard callback function
 * @param  {User}               callback.user                   The updated user
 * @throws {AssertionError}                                     Error thrown when an assertion failed
 */
var assertUpdateUser = module.exports.assertUpdateUser = function(client, id, update, callback) {
    client.user.updateUser(id, update, function(err, user) {
        assert.ok(!err);
        assert.ok(user);

        if (update.displayName) {
            assert.strictEqual(user.displayName, update.displayName);
        }
        if (update.email) {
            assert.strictEqual(user.email, update.email);
        }
        if (update.emailPreference) {
            assert.strictEqual(user.emailPreference, update.emailPreference);
        }

        return callback(user);
    });
};

/**
 * Assert that a user can not be updated
 *
 * @param  {RestClient}         client                          The REST client to make the request with
 * @param  {Number}             id                              The id of the user to update
 * @param  {Object}             update                          The updates to make
 * @param  {String}             [update.displayName]            The updated displayName
 * @param  {String}             [update.email]                  The updated email
 * @param  {String}             [update.emailPreference]        The updated email preference
 * @param  {Number}             code                            The expected HTTP error code
 * @param  {Function}           callback                        Standard callback function
 * @throws {AssertionError}                                     Error thrown when an assertion failed
 */
var assertUpdateUserFails = module.exports.assertUpdateUserFails = function(client, id, update, code, callback) {
    client.user.updateUser(id, update, function(err, user) {
        assert.ok(err);
        assert.strictEqual(err.code, code);
        assert.ok(!user);
        return callback();
    });
};

/**
 * Assert that the users for an application can be retrieved
 *
 * @param  {RestClient}         client                          The REST client to make the request with
 * @param  {Number}             [app]                           The id of the app to get the users for
 * @param  {String}             [query]                         The string to query users by
 * @param  {Number}             [limit]                         The maximum number of results to retrieve. Defaults to 10
 * @param  {Number}             [offset]                        The number to start paging from. Defaults to 0
 * @param  {Function}           callback                        Standard callback function
 * @param  {User[]}             callback.users                  The users in the application
 * @throws {AssertionError}                                     Error thrown when an assertion failed
 */
var getUsers = module.exports.getUsers = function(client, app, query, limit, offset, callback) {
    client.user.getUsers(app, query, limit, offset, function(err, users) {
        assert.ok(!err);
        assert.ok(users);
        return callback(users);
    });
};

/**
 * Assert that the users for an application can not be retrieved
 *
 * @param  {RestClient}         client                          The REST client to make the request with
 * @param  {Number}             [app]                           The id of the app to get the users for
 * @param  {String}             [query]                         The string to query users by
 * @param  {Number}             [limit]                         The maximum number of results to retrieve. Defaults to 10
 * @param  {Number}             [offset]                        The number to start paging from. Defaults to 0
 * @param  {Number}             code                            The expected HTTP error code
 * @param  {Function}           callback                        Standard callback function
 * @throws {AssertionError}                                     Error thrown when an assertion failed
 */
var getUsersFails = module.exports.getUsersFails = function(client, app, query, limit, offset, code, callback) {
    client.user.getUsers(app, query, limit, offset, function(err, users) {
        assert.ok(err);
        assert.strictEqual(err.code, code);
        assert.ok(!users);
        return callback(users);
    });
};

/**
 * Assert that the calendar token for a user can be reset
 *
 * @param  {RestClient}         client                          The REST client to make the request with
 * @param  {Number}             id                              The id of the user to reset the calendar token for
 * @param  {Function}           callback                        Standard callback function
 * @param  {User}               callback.user                   The updated user
 * @throws {AssertionError}                                     Error thrown when an assertion failed
 */
var assertResetUserCalendarToken = module.exports.assertResetUserCalendarToken = function(client, id, callback) {
    client.user.resetUserCalendarToken(id, function(err, user) {
        assert.ok(!err);
        assert.ok(user);

        // TODO: Once the /api/users/:id endpoint is implemented we should verify that the token has been reset
        return callback(user);
    });
};

/**
 * Assert that the calendar token for a user can not be reset
 *
 * @param  {RestClient}         client                          The REST client to make the request with
 * @param  {Number}             id                              The id of the user to reset the calendar token for
 * @param  {Number}             code                            The expected HTTP error code
 * @param  {Function}           callback                        Standard callback function
 * @throws {AssertionError}                                     Error thrown when an assertion failed
 */
var assertResetUserCalendarTokenFails = module.exports.assertResetUserCalendarTokenFails = function(client, id, code, callback) {
    client.user.resetUserCalendarToken(id, function(err, user) {
        assert.ok(err);
        assert.strictEqual(err.code, code);
        assert.ok(!user);
        return callback();
    });
};

/**
 * Assert that the calendar for a user can be retrieved
 *
 * @param  {RestClient}         client                          The REST client to make the request with
 * @param  {Number}             id                              The id of the user to get the calendar for
 * @param  {String}             start                           The timestamp (ISO 8601) from which to get the calendar for the user
 * @param  {String}             end                             The timestamp (ISO 8601) until which to get the calendar for the user
 * @param  {Event[]}            expectedEvents                  A set of events that should be in the calendar
 * @param  {Function}           callback                        Standard callback function
 * @param  {Event[]}            callback.calendar               The retrieved calendar
 * @throws {AssertionError}                                     Error thrown when an assertion failed
 */
var assertGetUserCalendar = module.exports.assertGetUserCalendar = function(client, id, start, end, expectedEvents, callback) {
    client.user.getUserCalendar(id, start, end, function(err, calendar) {
        assert.ok(!err);
        assert.ok(calendar);
        assert.ok(calendar.results);
        assert.ok(_.isArray(calendar.results));

        // Assert the calendar contains the expected events
        if (expectedEvents) {
            _.each(expectedEvents, function(event) {
                assert.ok(_.find(calendar.results, {'id': event.id}));
            });
        }

        return callback(calendar);<commit_msg>Asserting that the correct data is returned from the user calendars<commit_after>/**
 * Copyright (c) 2014 "Fronteer LTD"
 * Grasshopper Event Engine
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

var _ = require('lodash');
var assert = require('assert');

var TestsUtil = require('gh-tests/lib/util');

/**
 * Assert that the me feed can be retrieved
 *
 * @param  {RestClient}         client                          The REST client to make the request with
 * @param  {Function}           callback                        Standard callback function
 * @param  {Object}             callback.me                     The retrieved me feed
 * @throws {AssertionError}                                     Error thrown when an assertion failed
 */
var assertGetMe = module.exports.assertGetMe = function(client, callback) {
    client.user.getMe(function(err, me) {
        assert.ok(!err);
        assert.ok(me);
        return callback(me);
    });
};

/**
 * Assert that a user can be updated
 *
 * @param  {RestClient}         client                          The REST client to make the request with
 * @param  {Number}             id                              The id of the user to update
 * @param  {Object}             update                          The updates to make
 * @param  {String}             [update.displayName]            The updated displayName
 * @param  {String}             [update.email]                  The updated email
 * @param  {String}             [update.emailPreference]        The updated email preference
 * @param  {Function}           callback                        Standard callback function
 * @param  {User}               callback.user                   The updated user
 * @throws {AssertionError}                                     Error thrown when an assertion failed
 */
var assertUpdateUser = module.exports.assertUpdateUser = function(client, id, update, callback) {
    client.user.updateUser(id, update, function(err, user) {
        assert.ok(!err);
        assert.ok(user);

        if (update.displayName) {
            assert.strictEqual(user.displayName, update.displayName);
        }
        if (update.email) {
            assert.strictEqual(user.email, update.email);
        }
        if (update.emailPreference) {
            assert.strictEqual(user.emailPreference, update.emailPreference);
        }

        return callback(user);
    });
};

/**
 * Assert that a user can not be updated
 *
 * @param  {RestClient}         client                          The REST client to make the request with
 * @param  {Number}             id                              The id of the user to update
 * @param  {Object}             update                          The updates to make
 * @param  {String}             [update.displayName]            The updated displayName
 * @param  {String}             [update.email]                  The updated email
 * @param  {String}             [update.emailPreference]        The updated email preference
 * @param  {Number}             code                            The expected HTTP error code
 * @param  {Function}           callback                        Standard callback function
 * @throws {AssertionError}                                     Error thrown when an assertion failed
 */
var assertUpdateUserFails = module.exports.assertUpdateUserFails = function(client, id, update, code, callback) {
    client.user.updateUser(id, update, function(err, user) {
        assert.ok(err);
        assert.strictEqual(err.code, code);
        assert.ok(!user);
        return callback();
    });
};

/**
 * Assert that the users for an application can be retrieved
 *
 * @param  {RestClient}         client                          The REST client to make the request with
 * @param  {Number}             [app]                           The id of the app to get the users for
 * @param  {String}             [query]                         The string to query users by
 * @param  {Number}             [limit]                         The maximum number of results to retrieve. Defaults to 10
 * @param  {Number}             [offset]                        The number to start paging from. Defaults to 0
 * @param  {Function}           callback                        Standard callback function
 * @param  {User[]}             callback.users                  The users in the application
 * @throws {AssertionError}                                     Error thrown when an assertion failed
 */
var getUsers = module.exports.getUsers = function(client, app, query, limit, offset, callback) {
    client.user.getUsers(app, query, limit, offset, function(err, users) {
        assert.ok(!err);
        assert.ok(users);
        return callback(users);
    });
};

/**
 * Assert that the users for an application can not be retrieved
 *
 * @param  {RestClient}         client                          The REST client to make the request with
 * @param  {Number}             [app]                           The id of the app to get the users for
 * @param  {String}             [query]                         The string to query users by
 * @param  {Number}             [limit]                         The maximum number of results to retrieve. Defaults to 10
 * @param  {Number}             [offset]                        The number to start paging from. Defaults to 0
 * @param  {Number}             code                            The expected HTTP error code
 * @param  {Function}           callback                        Standard callback function
 * @throws {AssertionError}                                     Error thrown when an assertion failed
 */
var getUsersFails = module.exports.getUsersFails = function(client, app, query, limit, offset, code, callback) {
    client.user.getUsers(app, query, limit, offset, function(err, users) {
        assert.ok(err);
        assert.strictEqual(err.code, code);
        assert.ok(!users);
        return callback(users);
    });
};

/**
 * Assert that the calendar token for a user can be reset
 *
 * @param  {RestClient}         client                          The REST client to make the request with
 * @param  {Number}             id                              The id of the user to reset the calendar token for
 * @param  {Function}           callback                        Standard callback function
 * @param  {User}               callback.user                   The updated user
 * @throws {AssertionError}                                     Error thrown when an assertion failed
 */
var assertResetUserCalendarToken = module.exports.assertResetUserCalendarToken = function(client, id, callback) {
    client.user.resetUserCalendarToken(id, function(err, user) {
        assert.ok(!err);
        assert.ok(user);

        // TODO: Once the /api/users/:id endpoint is implemented we should verify that the token has been reset
        return callback(user);
    });
};

/**
 * Assert that the calendar token for a user can not be reset
 *
 * @param  {RestClient}         client                          The REST client to make the request with
 * @param  {Number}             id                              The id of the user to reset the calendar token for
 * @param  {Number}             code                            The expected HTTP error code
 * @param  {Function}           callback                        Standard callback function
 * @throws {AssertionError}                                     Error thrown when an assertion failed
 */
var assertResetUserCalendarTokenFails = module.exports.assertResetUserCalendarTokenFails = function(client, id, code, callback) {
    client.user.resetUserCalendarToken(id, function(err, user) {
        assert.ok(err);
        assert.strictEqual(err.code, code);
        assert.ok(!user);
        return callback();
    });
};

/**
 * Assert that the calendar for a user can be retrieved
 *
 * @param  {RestClient}         client                          The REST client to make the request with
 * @param  {Number}             id                              The id of the user to get the calendar for
 * @param  {String}             start                           The timestamp (ISO 8601) from which to get the calendar for the user
 * @param  {String}             end                             The timestamp (ISO 8601) until which to get the calendar for the user
 * @param  {Event[]}            expectedEvents                  A set of events that should be in the calendar
 * @param  {Function}           callback                        Standard callback function
 * @param  {Event[]}            callback.calendar               The retrieved calendar
 * @throws {AssertionError}                                     Error thrown when an assertion failed
 */
var assertGetUserCalendar = module.exports.assertGetUserCalendar = function(client, id, start, end, expectedEvents, callback) {
    client.user.getUserCalendar(id, start, end, function(err, calendar) {
        assert.ok(!err);
        assert.ok(calendar);
        assert.ok(calendar.results);
        assert.ok(_.isArray(calendar.results));

        // Assert the calendar contains the expected events
        if (expectedEvents) {
            _.each(expectedEvents, function(event) {
                assert.ok(_.find(calendar.results, {'id': event.id}));
            });
        }

        // Assert only the required information is returned
        var allowedEventKeys = ['id', 'displayName', 'location', 'start', 'end', 'organisers', 'updatedAt', 'context'];
        var allowedContextKeys = ['id', 'ParentId', 'displayName', 'type'];
        _.each(calendar.results, function(event) {
            _.each(event, function(val, key) {
                assert.ok(_.contains(allowedEventKeys, key), key + ' is not allowed as an event property in a calendar');
            });
            if (event.context) {
                _.each(event.context, function(val, key) {
                    assert.ok(_.contains(allowedContextKeys, key), key + ' is not allowed as an event\'s context property in a calendar');
                });
            }
        });

        return callback(calendar);
{'fraction_non_alphanumeric': 0.0819277108433735, 'fraction_numerical': 0.0018313253012048193, 'mean_word_length': 1.720372312532774, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 46, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4607868', 'n_tokens_mistral': 4949, 'n_tokens_neox': 4792, 'n_words': 1831}