diff --git a/lib/base-chips/_base-chips.js b/lib/base-chips/_base-chips.js index 89735ca6..fe551b4b 100644 --- a/lib/base-chips/_base-chips.js +++ b/lib/base-chips/_base-chips.js @@ -1,26 +1,25 @@ /* var fs = require("fs"); var path = require("path"); fs.readdirSync(__dirname).forEach(function(filename){ if(filename[0]!="_"){ require("./" + filename); } }) */ -require("./access_strategy.id_starts_with_digit.js"); require("./access_strategy.noone.js"); require("./access_strategy.public.js"); require("./access_strategy.just_owner.js"); require("./field_type.date.js"); require("./field_type.datetime.js"); require("./field_type.boolean.js"); require("./field_type.email.js"); require("./field_type.int.js"); require("./field_type.float.js"); require("./field_type.text.js"); require("./field_type.file.js"); require("./field_type.reference.js"); require("./resource_type.user.js"); \ No newline at end of file diff --git a/lib/base-chips/access_strategy.id_starts_with_digit.js b/lib/base-chips/access_strategy.id_starts_with_digit.js deleted file mode 100644 index dccf4c9f..00000000 --- a/lib/base-chips/access_strategy.id_starts_with_digit.js +++ /dev/null @@ -1,18 +0,0 @@ -var Sealious = require("sealious"); -var Promise = require("bluebird"); - -var is_number = /^[0-9]/; - -var IDStartsWithDigit = new Sealious.ChipTypes.AccessStrategy({ - name: "id_starts_with_digit", - checker_function: function(context, item){ - return new Promise(function(resolve, reject){ - if (is_number.test(item.id)) { - resolve("id ok!"); - } else { - reject("bad id"); - } - }); - }, - item_sensitive: true, -}); \ No newline at end of file diff --git a/lib/base-chips/field_type.file.js b/lib/base-chips/field_type.file.js index 6715f511..2f7deb79 100644 --- a/lib/base-chips/field_type.file.js +++ b/lib/base-chips/field_type.file.js @@ -1,41 +1,42 @@ var Sealious = require("../main.js"); var Promise = require("bluebird"); var FieldFileType = new Sealious.ChipTypes.FieldType({ name: "file", handles_large_data: true, is_proper_value: function(accept, reject, context, params, value){ if (value === undefined) { return undefined; } if ((value instanceof Sealious.File) || (value.filename !== undefined && value.data instanceof Buffer)) { accept(); } else { var type; if (value instanceof Array) { type = "<Array>. If you want to upload multiple files, use array field types."; } else { type = typeof data; } reject("Wrong file data format. Should be <Buffer>, but received " + type); } }, encode: function(context, params, value_in_code){ + // it doesn't check what the value_in_code really is if (value_in_code) { return Sealious.Dispatcher.files.save_file(value_in_code); } else { return null; } }, decode: function(context, params, value_in_database){ if (value_in_database) { return Promise.resolve(new Sealious.File.Reference(value_in_database.id, value_in_database.filename)); } else { if (params.no_file_value) { return params.no_file_value; } else { return undefined; } } } }); \ No newline at end of file diff --git a/lib/base-chips/field_type.reference.js b/lib/base-chips/field_type.reference.js index 7cb49b93..93c62f12 100644 --- a/lib/base-chips/field_type.reference.js +++ b/lib/base-chips/field_type.reference.js @@ -1,90 +1,90 @@ var Sealious = require("../main.js"); var Promise = require("bluebird"); var clone = require("clone"); var field_type_reference = new Sealious.ChipTypes.FieldType({ name: "reference", is_proper_declaration: function(declaration){ var required_declaration_fields = { "name": "string", "allowed_types": "array" }; for (var attribute_name in required_declaration_fields) { if (declaration[attribute_name] === undefined) { throw new Sealious.Errors.DeveloperError("Missing `" + attribute_name + "` attribute in reference declaration."); } } for (var i in declaration.allowed_types) { var type_name = declaration.allowed_types[i]; if (!Sealious.ChipManager.chip_exists("resource_type", type_name)) { throw new Sealious.Errors.DeveloperError("Unknown allowed type in declaration of reference: " + type_name); } } }, is_proper_value: function(accept, reject, context, params, value){ - if (typeof value == "object") { + if (typeof value === "object") { //validate object's values as values for new resource var type = value.type; if (type === undefined) { - reject("Reference resource type undefined. `type` attribute should be set to one of these values: " + this.params.allowed_types.join(", ") + "."); - } else if (this.params.allowed_types.indexOf(type) == -1) { - reject("Incorrect reference resource type: `" + type + "`. Allowed resource types for this reference are:" + this.params.allowed_types.join(", ") + "."); + reject("Reference resource type undefined. `type` attribute should be set to one of these values: " + params.allowed_types.join(", ") + "."); + } else if (params.allowed_types.indexOf(type) === -1) { + reject("Incorrect reference resource type: `" + type + "`. Allowed resource types for this reference are:" + params.allowed_types.join(", ") + "."); } else { var resource_type_object = Sealious.ChipManager.get_chip("resource_type", type); var access_strategy = resource_type_object.get_access_strategy(); return access_strategy.check(context, value.type, value.data) .then(function(){ - return resource_type_object.validate_field_values(context, true, value.data); + return resource_type_object.validate_fiel0d_values(context, true, value.data); }) } } else { //value is uid. Check if it is proper var supposed_resource_id = value; return Sealious.Dispatcher.resources.get_by_id(context, supposed_resource_id) .then(function(resource){ - if (this.params.allowed_types.indexOf(resource.type) >= 0) { + if (params.allowed_types.indexOf(resource.type) >= 0) { accept(resource); } else { reject("Resource of id `" + supposed_resource_id + "` is not of allowed type. Allowed types are: [" + this.params.allowed_types.join(", ") + "]"); } }.bind(this)) .catch(function(error){ - if (error.type == "not_found") { + if (error.type === "not_found") { reject(error.status_message); } else { reject(error); } }.bind(this)); } }, encode: function(context, params, value_in_code){ //decide whether to create a new resource (if so, do create it). Resolve with id of referenced resource. if (value_in_code instanceof Object) { return Sealious.Dispatcher.resources.create(context, value_in_code.type, value_in_code.data).then(function(resource){ return Promise.resolve(resource.id); }) } else { //assuming the provided id already exists return Promise.resolve(value_in_code); } }, get_description: function(context, params){ var params_copy = clone(params, false); params_copy.allowed_types = {}; for (var i in params.allowed_types) { var type_name = params.allowed_types[i]; var type_object = Sealious.ChipManager.get_chip("resource_type", type_name); var type_schema = Sealious.ChipManager.get_chip("resource_type", type_name).get_specification(); params_copy.allowed_types[type_name] = type_schema; } return params_copy; }, decode: function(context, params, value_in_db){ - if (value_in_db == undefined) { + if (value_in_db === undefined) { return Promise.resolve(undefined); } else { return Sealious.Dispatcher.resources.get_by_id(context, value_in_db); } } }); \ No newline at end of file diff --git a/lib/base-chips/field_type.text.js b/lib/base-chips/field_type.text.js index 800c6f64..f6fc53d6 100644 --- a/lib/base-chips/field_type.text.js +++ b/lib/base-chips/field_type.text.js @@ -1,37 +1,37 @@ var Sealious = require("../main.js"); var Promise = require("bluebird"); var sanitizeHtml = require("sanitize-html"); var field_type_text = new Sealious.ChipTypes.FieldType({ name: "text", get_description: function(context, params){ return "Text with a maximum length " + params.max_length }, is_proper_value: function(accept, reject, context, params, new_value){ - if (params == undefined || params.max_length === undefined) { + if (params === undefined || params.max_length === undefined) { accept() } else { if (new_value.length <= params.max_length) { accept() } else { reject("Text '" + new_value + "' has exceeded max length of " + params.max_length + " chars."); } } }, encode: function(context, params, value_in_code){ if (params && params.strip_html === true) { var stripped = sanitizeHtml(value_in_code.toString(), { allowedTags: [] }) return Promise.resolve(stripped); } else { if (value_in_code instanceof Object) { return Promise.resolve(JSON.stringify(value_in_code)); - } else if (value_in_code == null) { + } else if (value_in_code === null) { return Promise.resolve(null); } else { return Promise.resolve(value_in_code.toString()); } } } }); \ No newline at end of file diff --git a/lib/chip-types/access-strategy.js b/lib/chip-types/access-strategy.js index 8d1005aa..37232677 100644 --- a/lib/chip-types/access-strategy.js +++ b/lib/chip-types/access-strategy.js @@ -1,81 +1,71 @@ var Sealious = require("../main.js"); var Promise = require("bluebird"); var Chip = require("./chip.js"); function AccessStrategy (declaration) { if (typeof declaration.name != "string") { throw new Sealious.Errors.DeveloperError("Access strategy declaration has an invalid or missing `name` attribute"); } Chip.call(this, true, "access_strategy", declaration.name); this.checker_function = null; this.item_sensitive = false; if (declaration) this._process_declaration(declaration); } AccessStrategy.prototype = new function(){ /* Process declaration of access strategy. Gives new access strategy checker_function and item_sensitive from declaration. @param {object} delcaration - is an object with attributes: name - string, required. The name of the access strategy - it has to be a string unique amongst any other access strategies in your application. checker_function - function, required. It’s a function that takes a context instance as an argument and implements the logic of the access strategy. Its return values can be: * boolean - true for granting the access and false for denying. * a Promise - that resolves when the access is granted and rejects otherwise. Use promises only when the decision depends on a result of an asynchronous function. item_sensitive - boolean, defaults to false. If set to true, the checker_function is provided with a second argument, which contains an object representing the resource being requested. */ this._process_declaration = function(declaration){ this.checker_function = declaration.checker_function === undefined ? null : declaration.checker_function; if (declaration.item_sensitive !== undefined) { this.item_sensitive = declaration.item_sensitive; } } /* Setter of checker function. @param {function} checker_function - function to be set as checker function of this access strategy. return void */ this.set_checker_function = function(checker_function){ this.checker_function = checker_function; } /* Calls checker function and returns Promise if checker function resolves or error if it doesn't. @param {object} context - context in which item will be checked @param {object} item - item to be checked by checker function return Promise */ this.check = function(context, item){ var arguments_to_check_function = arguments; return this.checker_function.apply(this, arguments_to_check_function) .then(function(){ return Promise.resolve(item); }).catch(function(error_message){ if (typeof error_message == "string") { return Promise.reject(new Sealious.Errors.BadContext(error_message)); } else { return Promise.reject(error_message); } }); } } // odrzucanie i przyjmowanie wartości za pomocą checker_function, która zwraca Promisa // odrzucanie i przyjmowanie wartości za pomocą checker_function, która zwraca Booleana AccessStrategy.type_name = "access_strategy"; -AccessStrategy.test_start = function(){ - describe("AccessStrategy", function(){ - describe(".check", function(){ - describe("with checker_function that returns a Promise", function(){ - - }) - }) - }); -} - module.exports = AccessStrategy; \ No newline at end of file diff --git a/lib/chip-types/chip-manager.js b/lib/chip-types/chip-manager.js index 84e671d4..0359e7e4 100644 --- a/lib/chip-types/chip-manager.js +++ b/lib/chip-types/chip-manager.js @@ -1,178 +1,169 @@ var Sealious = require("../main.js"); var fs = require("fs"); var path = require('path') var Set = require('Set'); var Promise = require("bluebird"); var ConfigurationManager = require("../config/config-manager.js"); var chip_type_functions = { channel: require("./channel.js"), resource_type: require("./resource-type.js"), field_type: require("./field-type.js"), datastore: require("./datastore.js") } chips_by_module = {}; var started_chips_longids = new Set(); var chips = {}; var chip_type_start_order = ["datastore", "access_strategy", "field_type", "resource_type", "channel"]; var registred_chips_longids = new Set(); var ChipManager = new function(){ /** * Starts chips in proper order. * @param {array} chip_types_to_start - array of names of chips we want to start * @returns {Promise} - Promise, which will resolve with starting chips or reject with error */ this.start_chips = function(){ Sealious.Logger.info("Starting all chips:"); var promises = []; for (var i in chip_type_start_order){ var type = chip_type_start_order[i]; Sealious.Logger.info(" " + type + ":"); for (var name in chips[type]){ var chip = chips[type][name]; Sealious.Logger.info("\t \u2713 " + name); try { if (chip.start){ var promise = chip.start(); promises.push(promise); } } catch (error){ Sealious.Logger.error("\t " + "couldn't start `" + name + "`"); return Promise.reject(error); } } } return Promise.all(promises); } /** * Adds chip to chips array and chips_by_module array * @param {string} type - chip type ex. access-strategy, channel * @param {string} name - chip name * @param {object} chip - chip itselfs * @returns void */ this.add_chip = function(type, name, chip){ - if (chips[type]==undefined){ - chips[type]=[]; + if (chips[type] === undefined) { + chips[type] = []; } chips[type][name]=chip; } /** * Gets all resource types * @returns {array} Array of names of resource types. */ this.get_all_resource_types = function(){ var names = []; for (resource_type in chips.resource_type) { names.push(resource_type); } return names; } /** * Checks if chip exists * @param {string} type - chip type * @param {string} name - chip name * @returns {boolean} true if exists, otherwise false */ this.chip_exists = function(type, name){ if (chips[type] && chips[type][name]){ return true; } else { return false; } } /** * Gets chip * @param {string} type - chip type * @param {string} name - chip name * @returns {object} requested chip or throws error */ this.get_chip = function(type, name){ try { var ret = chips[type][name]; - if (ret == undefined){ + if (ret === undefined){ throw new Error("Chip of type " + type + " and name " + name + " has not yet been registered."); } return ret; } catch (e){ throw new Sealious.Errors.ValidationError("ChipManager was asked to return a chip of type `" + type + "` and name `" + name + "`, but it was not found", {}, {short_message: "chip_not_found"}); } } /* * Gets chip by long id * @param {string} longid - longid is chip_type.chip_name ex. chanell.cli * @returns {object} requested chip or throws error */ this.get_chip_by_longid = function(longid){ var type = longid.split(".")[0]; var name = longid.split(".")[1]; return this.get_chip(type, name); } - /* - * Checks if chip is registered - * @param {string} longid - longid is chip_type.chip_name ex. chanell.cli - * @returns {boolean} true if chip is registered, otherwise false - */ - this.chip_is_registred = function(longid){ - return this.get_chip_by_longid(longid) != undefined; - } - /* Gets amount of chips in given type * @param {string} type - type name ex. chanell, access_strategy * @returns {integer} amount of chips of this type */ this.get_chip_amount_by_type = function(type){ if (chips[type]){ return Object.keys(chips[type]).length; } else { return 0; } } /** * Gets proper datastore chip * @returns {object} datastore chip for application or throws error if no datastore chip is defined */ this.get_datastore_chip = function(){ var datastore_chip_amount = this.get_chip_amount_by_type("datastore"); if (datastore_chip_amount===0){ throw new Sealious.Errors.Error("Chip manager was requested to return the datastore chip, but no chips of type `datastore` have been registered.") } else if (datastore_chip_amount===1){ return chips["datastore"][Object.keys(chips["datastore"])[0]] } else { var datastore_chip_name = ConfigurationManager.get_config().datastore_chip_name; if (datastore_chip_name===undefined){ throw new Sealious.Errors.Error("Chip manager was requested to return a datastore chip. Multiple chips of type `datastore` have been registered, and no default provided in configuration.") } else { return this.get_chip("datastore", datastore_chip_name); } } } /** * Gets all chips of given type * @param {string} chip type name ex. channel, access_strategy * @returns {array} Array of chips of given type. */ this.get_chips_by_type = function(chip_type){ return chips[chip_type]; } } module.exports = ChipManager; diff --git a/lib/config/config-manager.js b/lib/config/config-manager.js index 76e8a179..c73dec91 100644 --- a/lib/config/config-manager.js +++ b/lib/config/config-manager.js @@ -1,68 +1,69 @@ var Sealious = require("../main.js"); var fs = require("fs"); var path = require("path"); var merge = require("merge"); var ConfigManager = new function(){ default_config = {}; config = {}; function modify_config (is_default_config, key, value) { var key_elements = key.split('.'); var new_values = {}; var current_position = new_values; for (var i = 0; i < key_elements.length - 1; i++) { var current_key = key_elements[i]; current_position[current_key] = {}; current_position = current_position[current_key]; } current_position[key_elements[key_elements.length - 1]] = value; if (is_default_config) { default_config = merge.recursive(true, default_config, new_values); } else { config = merge.recursive(true, config, new_values); } } this.set_config = function(key, new_config){ - if (arguments.length == 1) { + // config = config? + if (arguments.length === 1) { config = config; } else { modify_config(false, key, new_config); } } this.set_default_config = function(key, value){ - if (arguments.length == 1) { + if (arguments.length === 1) { default_config = default_config; } else { modify_config(true, key, value); } } this.get_configuration = function(key){ var to_merge_left = default_config; var to_merge_right = config; var key_elements = key == undefined ? [] : key.split("."); while (key_elements.length) { var current_key = key_elements.splice(0, 1); to_merge_left = to_merge_left[current_key] == undefined ? {} : to_merge_left[current_key]; to_merge_right = to_merge_right[current_key] == undefined ? {} : to_merge_right[current_key]; } return merge.recursive(true, to_merge_left, to_merge_right); } this.get_config = this.get_configuration; this.get_chip_config = function(longid){ return config.chip_config && config.chip_config[longid]; } this.get_dispatcher_config = function(){ return config.dispatcher_config || {}; } } module.exports = ConfigManager; \ No newline at end of file diff --git a/lib/main.js b/lib/main.js index cdcf5d06..9439c6c4 100644 --- a/lib/main.js +++ b/lib/main.js @@ -1,74 +1,53 @@ var path = require("path"); var Sealious = {}; module.exports = Sealious; -var core_instance = null; - var Core = require("./core.js"); Sealious.Errors = require("./response/error.js"); Sealious.Response = require("./response/response.js"); Sealious.File = require("./data-structures/file.js"); Sealious.ChipManager = require("./chip-types/chip-manager.js"); Sealious.ConfigManager = require("./config/config-manager.js"); Sealious.PluginManager = require("./plugins/plugin-manager.js"); Sealious.Dispatcher = null; Sealious.Context = require("./context.js"); Sealious.ResourceManager = require("./core-services/resource-manager.js"); Sealious.ChipTypes = { "AccessStrategy": require("./chip-types/access-strategy.js"), "Channel": require("./chip-types/channel.js"), "Datastore": require("./chip-types/datastore.js"), "FieldType": require("./chip-types/field-type.js"), "ResourceType": require("./chip-types/resource-type.js"), } for (var chip_type_name in Sealious.ChipTypes) { //Sealious.ChipTypes[chip_type_name] = Sealious.ChipTypes[chip_type_name].bind(Sealious.ChipTypes[chip_type_name], null); //null here corresponds to "module_path" of chip's constructor } Sealious.init = function(mode, layer_name){ Sealious.Logger = require("./logger/logger.js"); require("./base-chips/_base-chips.js"); Sealious.PluginManager.load_plugins(); this.mode = mode ? mode : "local"; this.layer_name = layer_name ? layer_name : null; } Sealious.start = function(){ Core.check_version(); var dispatcher = Core.get_dispatcher(this.mode, this.layer_name); dispatcher.init(); dispatcher.start(); Sealious.Dispatcher = dispatcher; return Sealious.ChipManager.start_chips(); } -Sealious.test = function(){ - var things_to_test = [ - Sealious.ResourceManager, - Sealious.ChipTypes.FieldType, - require("./chip-types/resource-type-field"), - ] - - Sealious.init(); - things_to_test.forEach(function(thing){ - thing.test_init && thing.test_init(); - }) - - Sealious.start().then(function(){ - things_to_test.forEach(function(thing){ - thing.test_start && thing.test_start(); - }) - //run(); //function created by Mocha. Present only if Mocha is run with --delay option from the command line (as it is when running `npm test`) - }) -} module.exports = Sealious; \ No newline at end of file diff --git a/lib/response/response.js b/lib/response/response.js index 7d9e1a3f..69f7f10b 100644 --- a/lib/response/response.js +++ b/lib/response/response.js @@ -1,17 +1,17 @@ function Response (data, is_error, type, status_message) { this.status = is_error ? "error" : "success"; this.type = type || "response"; - this.status_message = status_message || ok; + this.status_message = status_message || "ok"; this.data = data || {}; } Response.fromError = function(sealious_error){ return { data: sealious_error.data || {}, is_error: true, type: sealious_error.type, status_message: sealious_error.status_message, }; } module.exports = Response; \ No newline at end of file diff --git a/package.json b/package.json index 5bc54514..db7d9424 100644 --- a/package.json +++ b/package.json @@ -1,60 +1,60 @@ { - "name": "sealious", - "homepage": "http://sealious.github.io/", - "version": "0.7.4", - "description": "A declarative framework for fast & easy app development.", - "main": "./lib/main.js", - "scripts": { - "prepublish": "require-self", - "test": "mocha --delay ./tests/test.js", - "test-coverage": "npm run precoveralls && mocha -R html-cov --delay ./tests/test.js > coverage.html && npm run postcoveralls && ( xdg-open coverage.html || start coverage.html) &", - "precoveralls": "node ./tests/prepare-for-coverage.js && jscoverage lib lib-cov", - "coveralls": "./node_modules/.bin/mocha ./tests/test.js --delay -R mocha-lcov-reporter | ./node_modules/coveralls/bin/coveralls.js || npm run postcoveralls", - "postcoveralls": "node ./tests/prepare-for-coverage-undo.js", - "check-formatting": "jscs -c .jscsrc ./lib && echo \"\\n ✓ Code formatting O.K.!\\n\" || (echo \"\\nSome formatting errors found. Run 'npm run fix-formatting' to correct them.\\n\" && exit )", - "fix-formatting": "jscs -c .jscsrc ./lib --fix" - }, - "repository": { - "type": "git", - "url": "https://github.com/Sealious/Sealious/" - }, - "keywords": [ - "sealious" - ], - "author": "The Sealious team (http://github.com/Sealious)", - "license": "BSD-2-Clause", - "bugs": { - "url": "https://github.com/Sealious/Sealious/issues" - }, - "dependencies": { - "Set": "~0.4.1", - "bluebird": "~2.9.9", - "clone": "^1.0.2", - "fs-extra": "^0.18.2", - "immutable": "^3.7.2", - "merge": "^1.2.0", - "require-dir": "^0.3.0", - "sanitize-html": "^1.6.1", - "sealious-datastore-tingo": "^0.1.5", - "sha1": "^1.1.0", - "uid": "0.0.2", - "winston": "^1.0.0" - }, - "devDependencies": { - "coveralls": "^2.11.4", - "deep-equal": "^1.0.1", - "jscoverage": "^0.6.0", - "jscs": "^2.1.1", - "mocha": "*", - "mocha-lcov-reporter": "0.0.2", - "pre-commit": "^1.1.1", - "require-self": "^0.1.0" - }, - "pre-commit": { - "colors": true, - "run": [ - "check-formatting" - ], - "silent": false - } -} + "name": "sealious", + "homepage": "http://sealious.github.io/", + "version": "0.7.4", + "description": "A declarative framework for fast & easy app development.", + "main": "./lib/main.js", + "scripts": { + "prepublish": "require-self", + "test": "mocha --delay ./tests/test.js", + "test-coverage": "npm run precoveralls && mocha -R html-cov --delay ./tests/test.js > coverage.html && npm run postcoveralls && ( xdg-open coverage.html || start coverage.html) &", + "precoveralls": "node ./tests/prepare-for-coverage.js && jscoverage lib lib-cov", + "coveralls": "./node_modules/.bin/mocha ./tests/test.js --delay -R mocha-lcov-reporter | ./node_modules/coveralls/bin/coveralls.js || npm run postcoveralls", + "postcoveralls": "node ./tests/prepare-for-coverage-undo.js", + "check-formatting": "jscs -c .jscsrc ./lib && echo \"\\n ✓ Code formatting O.K.!\\n\" || (echo \"\\nSome formatting errors found. Run 'npm run fix-formatting' to correct them.\\n\" && exit )", + "fix-formatting": "jscs -c .jscsrc ./lib --fix" + }, + "repository": { + "type": "git", + "url": "https://github.com/Sealious/Sealious/" + }, + "keywords": [ + "sealious" + ], + "author": "The Sealious team (http://github.com/Sealious)", + "license": "BSD-2-Clause", + "bugs": { + "url": "https://github.com/Sealious/Sealious/issues" + }, + "dependencies": { + "Set": "~0.4.1", + "bluebird": "~2.9.9", + "clone": "^1.0.2", + "fs-extra": "^0.18.2", + "immutable": "^3.7.2", + "merge": "^1.2.0", + "require-dir": "^0.3.0", + "sanitize-html": "^1.6.1", + "sealious-datastore-tingo": "^0.1.5", + "sha1": "^1.1.0", + "uid": "0.0.2", + "winston": "^1.0.0" + }, + "devDependencies": { + "coveralls": "^2.11.4", + "deep-equal": "^1.0.1", + "jscoverage": "^0.6.0", + "jscs": "^2.1.1", + "mocha": "*", + "mocha-lcov-reporter": "0.0.2", + "pre-commit": "^1.1.1", + "require-self": "^0.1.0" + }, + "pre-commit": { + "colors": true, + "run": [ + "check-formatting" + ], + "silent": false + } +} \ No newline at end of file diff --git a/tests/unit-tests/access-strategy.just_owner.test.js b/tests/unit-tests/access-strategy.just_owner.test.js new file mode 100644 index 00000000..efdf8245 --- /dev/null +++ b/tests/unit-tests/access-strategy.just_owner.test.js @@ -0,0 +1,47 @@ +var Sealious = require("sealious"); + +module.exports = { + test_init: function() { + + }, + test_start: function() { + var just_owner = Sealious.ChipManager.get_chip("access_strategy", "just_owner") + describe("AccessStrategy.JustOwner", function() { + it("checks if the user is the owner and returns true", function(done) { + var context = new Sealious.Context(undefined, "ip", "user_id"); + var item = { + created_context: { + user_id: "user_id" + } + } + just_owner.checker_function(context, item) + .then(function() { + done(); + }) + .catch(function(error) { + done(new Error(error)) + }) + }) + /* + it("checks if the user is the owner and returns false (the test doesn't pass because it doesn't reject with an error but with a string)", function(done) { + var context = new Sealious.Context(undefined, "ip", "user_id"); + var item = { + created_context: { + user_id: "user_id2" + } + } + just_owner.checker_function(context, item) + .then(function() { + done(new Error("It lets the user pass")); + }) + .catch(function(error) { + if(error instanceof String) + done(new Error("The error is an instance of String")) + else + done(); + }) + }) +*/ + }) + } +} \ No newline at end of file diff --git a/tests/unit-tests/access-strategy.test.js b/tests/unit-tests/access-strategy.test.js new file mode 100644 index 00000000..3927c95c --- /dev/null +++ b/tests/unit-tests/access-strategy.test.js @@ -0,0 +1,35 @@ +var Sealious = require('sealious'); + +module.exports = { + test_init: function() { + new Sealious.ChipTypes.AccessStrategy({ + name: "test_access_strategy", + checker_function: function(context){ + return Promise.resolves(); + }, + item_sensitive: false, + }); + }, + test_start: function() { + describe("AccessStrategy", function() { + it("throws an error because declaration.name isn't string", function(done) { + try { + new Sealious.ChipTypes.AccessStrategy({}) + } + catch (e) { + if (e.type === "dev_error") + done(); + else + done(new Error("It didn't throw a developer error")) + } + }) + it("finds tests_access_strategy", function(done) { + var result = Sealious.ChipManager.get_chip("access_strategy", "test_access_strategy") + if (result.name === "test_access_strategy") + done(); + else + done("The names don't match"); + }) + }) + } +} \ No newline at end of file diff --git a/tests/unit-tests/channel.test.js b/tests/unit-tests/channel.test.js new file mode 100644 index 00000000..a72d741a --- /dev/null +++ b/tests/unit-tests/channel.test.js @@ -0,0 +1,21 @@ +//var Channel = require('../../lib/chip-types/chip.js'); +var Sealious = require('sealious'); + + +module.exports = { + test_init: function() { + + }, + test_start: function() { + describe("Channel", function() { + it("adds new channel", function(done) { + Sealious.ChipTypes.Channel("new_test_channel"); + var result = Sealious.ChipManager.get_chip("channel", "new_test_channel") + if (result.name === "new_test_channel") + done(); + else + done(new Error("It didn't return the correct channel")) + }) + }) + } +} diff --git a/tests/unit-tests/chip-manager.test.js b/tests/unit-tests/chip-manager.test.js new file mode 100644 index 00000000..312833af --- /dev/null +++ b/tests/unit-tests/chip-manager.test.js @@ -0,0 +1,72 @@ +var Sealious = require("sealious"); + +module.exports = { + test_init: function() { + var always_fails_resource = new Sealious.ChipTypes.ResourceType({ + name: "chip_manager_tests_resource", + }); + }, + test_start: function() { + describe("ChipManager", function() { + it("gets all resource types", function(done) { + var names = Sealious.ChipManager.get_all_resource_types(); + if (names instanceof Array) + if (names.indexOf("chip_manager_tests_resource") > -1) + done(); + else + done(new Error("It doesn't contains the created resource")) + else + done(new Error("It didn't return the array")) + }) + it("gets chip by longid (longid exists)", function(done) { + var chip = Sealious.ChipManager.get_chip_by_longid("resource_type.chip_manager_tests_resource"); + if (chip instanceof Object) + if (chip.type === "resource_type") + if (chip.name === "chip_manager_tests_resource") + if (chip.longid === "resource_type.chip_manager_tests_resource") + done(); + else + done(new Error("It didn't return the correct longid")); + else + done(new Error("It didn't return the correct chip name")); + else + done(new Error("It didn't return the correct chip type")); + else + done(new Error("It didn't return an object")) + }); + it("throws an error when get_chip_by_longid has non existent longid", function(done) { + try { + Sealious.ChipManager.get_chip_by_longid("doesn't exist"); + done(new Error("It didn't throw an error")); + } + catch (e) { + if (e.type === "validation") + done(); + else + done(new Error(e)) + } + }); + it("gets chip amount by type and returns 0 (non existent type)", function(done) { + var amount = Sealious.ChipManager.get_chip_amount_by_type("non existent"); + if (amount === 0) + done(); + else + done(new Error("It returned something else than 0")) + }); + it("gets chips by type (type exists)", function(done){ + var chips = Sealious.ChipManager.get_chips_by_type("resource_type"); + if (chips["chip_manager_tests_resource"] !== undefined) + done(); + else + done(new Error("It didn't get the chip_manager_tests_resource type")) + }); + it("gets chips by type (type doesn't exist)", function(done){ + var chips = Sealious.ChipManager.get_chips_by_type("troll_type"); + if (chips === undefined) + done(); + else + done(new Error("The result is not undefined")) + }); + }) + } +} \ No newline at end of file diff --git a/tests/unit-tests/chip.test.js b/tests/unit-tests/chip.test.js new file mode 100644 index 00000000..50530710 --- /dev/null +++ b/tests/unit-tests/chip.test.js @@ -0,0 +1,26 @@ +var Chip = require('../../lib/chip-types/chip.js'); +var Sealious = require('sealious') + +module.exports = { + test_init: function() { + }, + test_start: function() { + describe("Chip", function() { + /* + it("configures chip", function(done) { + Chip(true, "test_type_chip", "test_name_chip"); + var chip = Sealious.ChipManager.get_chip("test_type_chip", "test_name_chip") + console.log(chip) + chip.prototype.configure({field: "value"}); + if (chip.configuration.field === "value") + done(); + else + done(new Error("It didn't return the configuration")) + + //test_chip.configure({field: "test_value"}); + + }) +*/ + }) + } +} \ No newline at end of file diff --git a/tests/unit-tests/config-manager-test.js b/tests/unit-tests/config-manager-test.js new file mode 100644 index 00000000..002bbb4b --- /dev/null +++ b/tests/unit-tests/config-manager-test.js @@ -0,0 +1,62 @@ +var Sealious = require("sealious"); + +module.exports = { + test_init: function() { + + }, + test_start: function() { + describe("ConfigManager", function() { + it("sets config with arguments.length === 1", function(done) { + try { + Sealious.ConfigManager.set_config("some_key"); + done(); + } + catch (e) { + done(new Error(e)); + } + }) + it("sets config with arguments.length === 2 and uses modify_config private function", function(done) { + try { + Sealious.ConfigManager.set_config("this.key", "config"); + done(); + } + catch (e) { + done(new Error(e)); + } + }) + it("sets default config with arguments.length === 1", function(done) { + try { + Sealious.ConfigManager.set_default_config("some_key"); + done(); + } + catch (e) { + done(new Error(e)); + } + }) + it("gets configuration", function(done) { + var config = Sealious.ConfigManager.get_configuration("this.key"); + if (config instanceof Object) + done(); + else + done(new Error("It didn't return an object")) + }) + it("gets dispatcher config", function(done) { + var dispatcher_config = Sealious.ConfigManager.get_dispatcher_config(); + if (dispatcher_config instanceof Object) + if (Object.getOwnPropertyNames(dispatcher_config).length === 0) + done(); + else + done(new Error("It didn't return an empty object")) + else + done(new Error("It didn't return an object")) + }) + it("gets non existent chip config and returns undefined", function(done) { + var dispatcher_config = Sealious.ConfigManager.get_chip_config("some_non_existent_longid"); + if (dispatcher_config === undefined) + done(); + else + done(new Error("It didn't return undefined")) + }) + }) + } +} \ No newline at end of file diff --git a/tests/unit-tests/datastore.test.js b/tests/unit-tests/datastore.test.js new file mode 100644 index 00000000..34f72c1d --- /dev/null +++ b/tests/unit-tests/datastore.test.js @@ -0,0 +1,24 @@ +var Sealious = require("sealious"); + +module.exports = { + test_init: function() { + + }, + test_start: function() { + describe("Sealious.Datastore", function() { + it("returns a developer error \"not implemented\"", function(done) { + var func = Sealious.Dispatcher.datastore.return_not_implemented("not implemented"); + try { + func(); + done("It didn't throw an error at all") + } + catch (e) { + if (e.type === "dev_error") + done(); + else + done("It didn't throw a proper error"); + } + }) + }) + } +} \ No newline at end of file diff --git a/tests/unit-tests/error.test.js b/tests/unit-tests/error.test.js new file mode 100644 index 00000000..b719c885 --- /dev/null +++ b/tests/unit-tests/error.test.js @@ -0,0 +1,121 @@ +var Sealious = require("sealious"); + +module.exports = { + test_init: function() { + + }, + test_start: function() { + describe("Sealious.Errors", function(){ + it("throws a Sealious.Errors.Error", function(done) { + try{ + throw new Sealious.Errors.Error("This is just an error", {type: "test_error"}); + } + catch (e) { + if (e.type === "test_error") + done(); + else + done(new Error("It didn't throw a Sealious Error")) + } + }) + it("throws a Sealious.Errors.ValidationError", function(done) { + try{ + throw new Sealious.Errors.ValidationError("This is just an error"); + } + catch (e) { + if (e.type === "validation") + done(); + else + done(new Error("It didn't throw a Sealious ValidationError")) + } + }) + it("throws a Sealious.Errors.ValueExists", function(done) { + try{ + throw new Sealious.Errors.ValueExists("This is just an error"); + } + catch (e) { + if (e.type === "valueExists") + done(); + else + done(new Error("It didn't throw a Sealious ValueExists Error")) + } + }) + it("throws a Sealious.Errors.InvalidCredentials", function(done) { + try{ + throw new Sealious.Errors.InvalidCredentials("This is just an error"); + } + catch (e) { + if (e.type === "authorization") + done(); + else + done(new Error("It didn't throw a Sealious InvalidCredentials Error")) + } + }) + it("throws a Sealious.Errors.NotFound", function(done) { + try{ + throw new Sealious.Errors.NotFound("This is just an error"); + } + catch (e) { + if (e.type === "not_found") + done(); + else + done(new Error("It didn't throw a Sealious NotFound Error")) + } + }) + it("throws a Sealious.Errors.InternalConnectionError", function(done) { + try{ + throw new Sealious.Errors.InternalConnectionError("This is just an error"); + } + catch (e) { + if (e.type === "internal_connection_error") + done(); + else + done(new Error("It didn't throw a Sealious InternalConnectionError Error")) + } + }) + it("throws a Sealious.Errors.DependencyError", function(done) { + try{ + throw new Sealious.Errors.DependencyError("This is just an error"); + } + catch (e) { + if (e.type === "dependency_error") + done(); + else + done(new Error("It didn't throw a Sealious DependencyError Error")) + } + }) + it("throws a Sealious.Errors.UnauthorizedRequest", function(done) { + try{ + throw new Sealious.Errors.UnauthorizedRequest("This is just an error"); + } + catch (e) { + if (e.type === "unauthorized_request") + done(); + else + done(new Error("It didn't throw a Sealious UnauthorizedRequest Error")) + } + }) + it("throws a Sealious.Errors.DeveloperError", function(done) { + try{ + throw new Sealious.Errors.DeveloperError("This is just an error"); + } + catch (e) { + if (e.type === "dev_error") + done(); + else + done(new Error("It didn't throw a Sealious DeveloperError")) + } + }) + it("throws a Sealious.Errors.BadContext", function(done) { + try{ + throw new Sealious.Errors.BadContext("This is just an error"); + } + catch (e) { + if (e.type === "permission") + done(); + else + done(new Error("It didn't throw a Sealious BadContext Error")) + } + }) + }) + } +} \ No newline at end of file diff --git a/tests/unit-tests/field-type.boolean.test.js b/tests/unit-tests/field-type.boolean.test.js index cec146ce..b371f768 100644 --- a/tests/unit-tests/field-type.boolean.test.js +++ b/tests/unit-tests/field-type.boolean.test.js @@ -1,100 +1,86 @@ var Sealious = require("sealious"); -var ResourceManager = Sealious.ResourceManager; - module.exports = { - test_init: function() { - var resource_type_boolean = new Sealious.ChipTypes.ResourceType({ - name: "boolean_resource", - fields: [ - {name: "test", type: "boolean", required: true} - ] - }); - }, + test_init: function() {}, test_start: function() { + var field_type_boolean = Sealious.ChipManager.get_chip("field_type", "boolean"); describe("FieldType.Boolean", function() { - it("should validate the email field successfully with boolean true and return true", function(done) { - ResourceManager.create(new Sealious.Context(), "boolean_resource", {test: true}) - .then(function(ok){ - done(); - }) - .catch(function(error){ - done(error); - }) - }) - it("should validate the email field successfully with 1 and return true", function(done) { - ResourceManager.create(new Sealious.Context(), "boolean_resource", {test: 1}) - .then(function(ok){ + it("should return the description of the field type", function(done) { + if (typeof field_type_boolean.declaration.get_description() === "string") done(); - }) - .catch(function(error){ - done(error); - }) - }) - it("should validate the email field successfully with string true and return true", function(done) { - ResourceManager.create(new Sealious.Context(), "boolean_resource", {test: "true"}) - .then(function(ok){ + else + done(new Error("But it didn't")); + }); + it("should check if is_proper_value works correctly (given boolean)", function(done) { + field_type_boolean.is_proper_value(new Sealious.Context(), {}, true) + .then(function() { done(); }) - .catch(function(error){ - done(error); - }) - }) - it("should throw an error with string", function(done) { - ResourceManager.create(new Sealious.Context(), "boolean_resource", {test: "silly sealy"}) - .then(function(ok){ - done(new Error("it validated it correctly")); - }) - .catch(function(error){ - if (error.type === "validation") - done(); - else - done(new Error(error)) - }) - }) - it("should throw an error with number", function(done) { - ResourceManager.create(new Sealious.Context(), "boolean_resource", {test: 123456789}) - .then(function(ok){ - done(new Error("it validated it correctly")); - }) - .catch(function(error){ - if (error.type === "validation") - done(); - else - done(new Error(error)) + .catch(function(error) { + done(new Error(error)); }) - }) - it("should return false with 0", function(done) { - ResourceManager.create(new Sealious.Context(), "boolean_resource", {test: 0}) - .then(function(ok){ + }); + it("should check if is_proper_value works correctly (given 1)", function(done) { + field_type_boolean.is_proper_value(new Sealious.Context(), {}, 1) + .then(function() { done(); }) - .catch(function(error){ + .catch(function(error) { done(new Error(error)); }) - }) - it("should return false with string false", function(done) { - ResourceManager.create(new Sealious.Context(), "boolean_resource", {test: "false"}) - .then(function(ok){ + }); + it("should check if is_proper_value works correctly (given \"true\")", function(done) { + field_type_boolean.is_proper_value(new Sealious.Context(), {}, "true") + .then(function() { done(); }) - .catch(function(error){ + .catch(function(error) { done(new Error(error)); }) - }) - it("should throw an error because of no value", function(done){ - ResourceManager.create(new Sealious.Context(), "boolean_resource", {}) - .then(function(ok){ - done(new Error("but it didn't")); + }); + it("should check if is_proper_value works correctly (given 2)", function(done) { + field_type_boolean.is_proper_value(new Sealious.Context(), {}, 2) + .then(function() { + done(new Error("It didn't throw an error")); }) - .catch(function(error){ + .catch(function(error) { if (error.type === "validation") done(); else - done(new Error(error)) + done(new Error(error)); }) - }) - }) - } + }); + it("should encode the value corretly (given boolean)", function(done) { + if (field_type_boolean.declaration.encode(new Sealious.Context(), {}, true) === true) + done(); + else + done(new Error("It didn't parse the value correctly")) + }); + it("should encode the value corretly (given 1)", function(done) { + if (field_type_boolean.declaration.encode(new Sealious.Context(), {}, 1) === true) + done(); + else + done(new Error("It didn't parse the value correctly")) + }); + it("should encode the value corretly (given 0)", function(done) { + if (field_type_boolean.declaration.encode(new Sealious.Context(), {}, 0) === false) + done(); + else + done(new Error("It didn't parse the value correctly")) + }); + it("should encode the value corretly (given \"false\")", function(done) { + if (field_type_boolean.declaration.encode(new Sealious.Context(), {}, "false") === false) + done(); + else + done(new Error("It didn't parse the value correctly")) + }); + it("should encode the value corretly (given \"true\")", function(done) { + if (field_type_boolean.declaration.encode(new Sealious.Context(), {}, "true") === true) + done(); + else + done(new Error("It didn't parse the value correctly")) + }); + }); + } }; \ No newline at end of file diff --git a/tests/unit-tests/field-type.color.test.js b/tests/unit-tests/field-type.color.test.js new file mode 100644 index 00000000..04994937 --- /dev/null +++ b/tests/unit-tests/field-type.color.test.js @@ -0,0 +1,94 @@ +var Sealious = require("sealious"); + +module.exports = { + test_init: function() {}, + + test_start: function() { + /* + var field_type_color = Sealious.ChipManager.get_chip("field_type", "color"); + describe("FieldType.Color", function() { + it("checks if is_proper_value throws an error (wrong value)", function(done) { + field_type_color.is_proper_value(new Sealious.Context(), {}, "aaaa") + .then(function() { + done(new Error("It didn't throw an error")); + }) + .catch(function(error){ + done(); + }) + }) + it("checks if is_proper_value works correctly (given \"rgb(255, 255, 255)\"", function(done) { + field_type_color.is_proper_value(new Sealious.Context(), {}, "rgb(255, 255, 255)") + .then(function() { + done(); + }) + .catch(function(error){ + done(new Error(error)); + }) + }) + it("checks if is_proper_value works correctly (given \"black\"", function(done) { + field_type_color.is_proper_value(new Sealious.Context(), {}, "black") + .then(function() { + done(); + }) + .catch(function(error){ + done(new Error(error)); + }) + }) + it("checks if is_proper_value works correctly (given \"BLACK\"", function(done) { + field_type_color.is_proper_value(new Sealious.Context(), {}, "BLACK") + .then(function() { + done(); + }) + .catch(function(error){ + done(new Error(error)); + }) + }) + it("checks if is_proper_value works correctly (given \"#000000\"", function(done) { + field_type_color.is_proper_value(new Sealious.Context(), {}, "#000000") + .then(function() { + done(); + }) + .catch(function(error){ + done(new Error(error)); + }) + }) + it("checks if is_proper_value works correctly (given \"{r: 255, g: 255, b: 255}\"", function(done) { + field_type_color.is_proper_value(new Sealious.Context(), {}, {r: 255, g: 255, b: 255}) + .then(function() { + done(); + }) + .catch(function(error){ + done(new Error(error)); + }) + }) + it("checks if is_proper_value works correctly (given \"hsla(262, 59%, 81%, 0.5)\"", function(done) { + field_type_color.is_proper_value(new Sealious.Context(), {}, "hsla(262, 59%, 81%, 0.5)") + .then(function() { + done(); + }) + .catch(function(error){ + done(new Error(error)); + }) + }) + it("checks if encode works correctly", function(done){ + if(field_type_color.declaration.encode(new Sealious.Context(), {}, {r: 0, g: 0, b: 0}) === "#000000") + done(); + else + done("It didnt work correctly"); + }) + it("checks if encode works correctly", function(done){ + if(field_type_color.declaration.encode(new Sealious.Context(), {}, "black") === "#000000") + done(); + else + done("It didnt work correctly"); + }) + it("checks if encode works correctly", function(done){ + if(field_type_color.declaration.encode(new Sealious.Context(), {}, "hsl(0,0%,0%)") === "#000000") + done(); + else + done("It didnt work correctly"); + }) + }) +*/ + } +} \ No newline at end of file diff --git a/tests/unit-tests/field-type.date.test.js b/tests/unit-tests/field-type.date.test.js index 78146a4f..9b6f1851 100644 --- a/tests/unit-tests/field-type.date.test.js +++ b/tests/unit-tests/field-type.date.test.js @@ -1,142 +1,44 @@ var Sealious = require("sealious"); -var ResourceManager = Sealious.ResourceManager; module.exports = { - test_init: function() { - var resource_type_date = new Sealious.ChipTypes.ResourceType({ - name: "date_resource", - fields: [ - {name: "test", type: "date", required: true} - ] - }); - }, + test_init: function() {}, test_start: function() { - describe("FieldType.Date", function(){ - it("should validate the date field correctly", function(done){ - ResourceManager.create(new Sealious.Context(), "date_resource", {test: "2015-10-20"}) - .then(function(ok){ + var field_type_date = Sealious.ChipManager.get_chip("field_type", "date"); + describe("FieldType.Date", function() { + it("should return the description of the field type", function(done) { + if (typeof field_type_date.declaration.get_description() === "string") done(); - }) - .catch(function(error){ - done(new Error(error)); - }) + else + done(new Error("But it didn't")); }); - it("should throw an error because of wrong month", function(done){ - ResourceManager.create(new Sealious.Context(), "date_resource", {test: "2015-13-20"}) - .then(function(ok){ - done(new Error("but it didn't")); - }) - .catch(function(error){ + it("should check if is_proper_value works correctly(given correct date format)", function(done) { + field_type_date.is_proper_value(new Sealious.Context(), {}, "2015-10-27") + .then(function() { done(); - - }) - }) - it("should throw an error because of wrong day", function(done){ - ResourceManager.create(new Sealious.Context(), "date_resource", {test: "2015-13-32"}) - .then(function(ok){ - done(new Error("but it didn't")); - }) - .catch(function(error){ - done(); - }) - }) - it("should throw an error because of wrong date (too many days in given month)", function(done){ - ResourceManager.create(new Sealious.Context(), "date_resource", {test: "2015-12-32"}) - .then(function(ok){ - done(new Error("but it didn't")); - }) - .catch(function(error){ - if (error.type === "validation") - done(); - else - done(new Error(error)) - }) - }) - it("should throw an error because of wrong date format (wrong YYYY-MM-DD order)", function(done){ - ResourceManager.create(new Sealious.Context(), "date_resource", {test: "20-10-2015"}) - .then(function(ok){ - done(new Error("but it didn't")); - }) - .catch(function(error){ - if (error.type === "validation") - done(); - else - done(new Error(error)) - }) - }) - it("should throw an error because of wrong date format (dots instead of dashes)", function(done){ - ResourceManager.create(new Sealious.Context(), "date_resource", {test: "20.10.2015"}) - .then(function(ok){ - done(new Error("but it didn't")); - }) - .catch(function(error){ - if (error.type === "validation") - done(); - else - done(new Error(error)) - }) - }) - it("should throw an error because of wrong date format (slashes instead of dashes)", function(done){ - ResourceManager.create(new Sealious.Context(), "date_resource", {test: "20/10/2015"}) - .then(function(ok){ - done(new Error("but it didn't")); }) - .catch(function(error){ - if (error.type === "validation") - done(); - else - done(new Error(error)) - }) - }) - it("should throw an error because of wrong value (string)", function(done){ - ResourceManager.create(new Sealious.Context(), "date_resource", {test: "silly sealy"}) - .then(function(ok){ - done(new Error("but it didn't")); - }) - .catch(function(error){ - if (error.type === "validation") - done(); - else - done(new Error(error)) - }) - }) - it("should throw an error because of wrong value (integer)", function(done){ - ResourceManager.create(new Sealious.Context(), "date_resource", {test: 1}) - .then(function(ok){ - done(new Error("but it didn't")); - }) - .catch(function(error){ - if (error.type === "validation") - done(); - else - done(new Error(error)) - }) - }) - it("should throw an error because of wrong value (boolean)", function(done){ - ResourceManager.create(new Sealious.Context(), "date_resource", {test: true}) - .then(function(ok){ - done(new Error("but it didn't")); - }) - .catch(function(error){ - if (error.type === "validation") - done(); - else - done(new Error(error)) + .catch(function(error) { + done(new Error(error)); }) - }) - it("should throw an error because of no value", function(done){ - ResourceManager.create(new Sealious.Context(), "date_resource", {}) - .then(function(ok){ - done(new Error("but it didn't")); + }); + it("should check if is_proper_value works correctly(given incorrect date format)", function(done) { + field_type_date.is_proper_value(new Sealious.Context(), {}, "2015/10/27") + .then(function() { + done(new Error("It worked correctly")); }) - .catch(function(error){ + .catch(function(error) { if (error.type === "validation") done(); - else - done(new Error(error)) + else + done(new Error(error)); }) - }) - }) + }); + it("should check if encode works properly (given \"1\")", function(done) { + if (field_type_date.declaration.encode(new Sealious.Context(), {}, "2015/10/27") === 1445900400000) + done(); + else + done(new Error("It didn't parse the value correctly")) + }); + }); } }; \ No newline at end of file diff --git a/tests/unit-tests/field-type.datetime.test.js b/tests/unit-tests/field-type.datetime.test.js index 7b515b3d..d07dad7a 100644 --- a/tests/unit-tests/field-type.datetime.test.js +++ b/tests/unit-tests/field-type.datetime.test.js @@ -1,63 +1,44 @@ var Sealious = require("sealious"); -var ResourceManager = Sealious.ResourceManager; module.exports = { - test_init: function() { - var resource_type_datetime = new Sealious.ChipTypes.ResourceType({ - name: "datetime_resource", - fields: [ - {name: "test", type: "datetime", required: true} - ] - }); - }, + test_init: function() {}, test_start: function() { - describe("FieldType.Datetime", function(){ - it("should validate the datetime field successfully", function(done) { - ResourceManager.create(new Sealious.Context(), "datetime_resource", {test: 1}) - .then(function(ok){ + var field_type_datetime = Sealious.ChipManager.get_chip("field_type", "datetime"); + describe("FieldType.Datetime", function() { + it("should return the description of the field type", function(done) { + if (typeof field_type_datetime.declaration.get_description() === "string") done(); - }) - .catch(function(error){ - done(error); - }) - }) - it("should not validate the datetime field successfully (given string)", function(done) { - ResourceManager.create(new Sealious.Context(), "datetime_resource", {test: "silly sealy"}) - .then(function(ok){ - done(new Error("but it did...")); - }) - .catch(function(error){ - if (error.type === "validation") - done(); - else - done(new Error(error)) - }) + else + done(new Error("But it didn't")); }); - it("should not validate the datetime field successfully (given float)", function(done) { - ResourceManager.create(new Sealious.Context(), "datetime_resource", {test: 1.2}) - .then(function(ok){ - done(new Error("but it did...")); + it("should check if is_proper_value works correctly(given timestamp)", function(done) { + field_type_datetime.is_proper_value(new Sealious.Context(), {}, 1) + .then(function() { + done(); }) - .catch(function(error){ - if (error.type === "validation") - done(); - else - done(new Error(error)) + .catch(function(error) { + done(new Error(error)); }) }); - it("should not validate the datetime field successfully (no value)", function(done) { - ResourceManager.create(new Sealious.Context(), "datetime_resource", {}) - .then(function(ok){ - done(new Error("but it did...")); + it("should check if is_proper_value works correctly(given string)", function(done) { + field_type_datetime.is_proper_value(new Sealious.Context(), {}, "test") + .then(function() { + done(new Error("It worked correctly")); }) - .catch(function(error){ + .catch(function(error) { if (error.type === "validation") - done(); + done() else - done(new Error(error)) + done(new Error(error)); }) }); + it("should check if encode works properly (given \"1\")", function(done) { + if (field_type_datetime.declaration.encode(new Sealious.Context(), {}, "1") === 1) + done(); + else + done(new Error("It didn't parse the value correctly")) + }); }); } }; \ No newline at end of file diff --git a/tests/unit-tests/field-type.email.test.js b/tests/unit-tests/field-type.email.test.js index 78b7d86f..d7b6c1a2 100644 --- a/tests/unit-tests/field-type.email.test.js +++ b/tests/unit-tests/field-type.email.test.js @@ -1,64 +1,38 @@ var Sealious = require("sealious"); -var ResourceManager = Sealious.ResourceManager; module.exports = { - test_init: function() { - var resource_type_email = new Sealious.ChipTypes.ResourceType({ - name: "email_resource", - fields: [ - {name: "email", type: "email", required: true} - ] - }); - }, + test_init: function() {}, test_start: function() { + var field_type_email = Sealious.ChipManager.get_chip("field_type", "email"); describe("FieldType.Email", function() { - it("should validate the email field successfully", function(done) { - ResourceManager.create(new Sealious.Context(), "email_resource", {email: "test@test.com"}) - .then(function(ok){ + it("should return the description of the field type", function(done) { + if (typeof field_type_email.declaration.get_description() === "string") + done(); + else + done(new Error("But it didn't")); + }); + it("should check if is_proper_value works correctly(given correct date format)", function(done) { + field_type_email.is_proper_value(new Sealious.Context(), {}, "test@mail.com") + .then(function() { done(); }) - .catch(function(error){ - done(error); - }) - }) - it("should reject given email address (string)", function(done){ - ResourceManager.create(new Sealious.Context(), "email_resource", {email: "troll"}) - .then(function(ok){ - done(new Error("...but it decided the email address was correct")); - }) - .catch(function(error){ - if (error.type === "validation") - done(); - else - done(new Error(error)) - }) - }) - it("should reject given email address (integer)", function(done){ - ResourceManager.create(new Sealious.Context(), "email_resource", {email: 1}) - .then(function(ok){ - done(new Error("...but it decided the email address was correct")); + .catch(function(error) { + done(new Error(error)); }) - .catch(function(error){ - if (error.type === "validation") - done(); - else - done(new Error(error)) + }); + it("should check if is_proper_value works correctly(given incorrect date format)", function(done) { + field_type_email.is_proper_value(new Sealious.Context(), {}, "test") + .then(function() { + done(new Error("It worked correctly")); }) - }) - it("should throw an error because of no value", function(done){ - ResourceManager.create(new Sealious.Context(), "email_resource", {}) - .then(function(ok){ - done(new Error("but it didn't")); - }) - .catch(function(error){ + .catch(function(error) { if (error.type === "validation") done(); - else - done(new Error(error)) + else + done(new Error(error)); }) - }) - }) + }); + }); } - }; \ No newline at end of file diff --git a/tests/unit-tests/field-type.file.test.js b/tests/unit-tests/field-type.file.test.js new file mode 100644 index 00000000..f5339bc2 --- /dev/null +++ b/tests/unit-tests/field-type.file.test.js @@ -0,0 +1,77 @@ +var Sealious = require("sealious"); + +module.exports = { + test_init: function() { + + }, + test_start: function() { + var field_type_file = Sealious.ChipManager.get_chip("field_type", "file"); + describe("FieldType.File", function() { + it("returns undefined", function(done) { + if (field_type_file.declaration.is_proper_value(new Sealious.Context(), {}, undefined) === undefined) + done(); + else + done(new Error("It didn't return undefined")); + }) + it("accepts given value (which is an instance of Sealious.File)", function(done) { + field_type_file.is_proper_value(new Sealious.Context(), {}, new Sealious.File()) + .then(function(){ + done(); + }) + }) + it("accepts given value (which is an object with filename and data attributes)", function(done) { + var value = { + filename: "test_filename", + data: new Buffer(1) + } + field_type_file.is_proper_value(new Sealious.Context(), {}, value) + .then(function(){ + done(); + }) + }) + it("rejects given value (which is an empty object)", function(done) { + field_type_file.is_proper_value(new Sealious.Context(), {}, {}) + .then(function() { + done(new Error("It didn't reject the value")); + }) + .catch(function(error) { + done(); + }) + }) + it("rejects given value (which is an array)", function(done) { + field_type_file.is_proper_value(new Sealious.Context(), {}, []) + .then(function() { + done(new Error("It didn't reject the value")); + }) + .catch(function(error) { + done(); + }) + }) + /* + it("checks if encode works correctly", function(done) { + // to add + //console.log(field_type_file.declaration.encode(new Sealious.Context(), {}, "test")) + + }) +*/ + it("checks if encode works correctly (value_in_code is false)", function(done) { + if (field_type_file.declaration.encode(new Sealious.Context(), {}, false) === null) + done(); + else + done(new Error("It didn't return null")); + }) + it("checks if decode works correctly (value_in_database is false, params.no_file_value is not given)", function(done) { + if (field_type_file.declaration.decode(new Sealious.Context(), {}, false) === undefined) + done(); + else + done(new Error("It didn't return null")); + }) + it("checks if decode works correctly (value_in_database is false, params.no_file_value is given)", function(done) { + if (field_type_file.declaration.decode(new Sealious.Context(), {no_file_value: "test"}, false) === "test") + done(); + else + done(new Error("It didn't return null")); + }) + }) + } +} \ No newline at end of file diff --git a/tests/unit-tests/field-type.float.test.js b/tests/unit-tests/field-type.float.test.js index b4d7cd61..cc86276a 100644 --- a/tests/unit-tests/field-type.float.test.js +++ b/tests/unit-tests/field-type.float.test.js @@ -1,79 +1,44 @@ var Sealious = require("sealious"); -var ResourceManager = Sealious.ResourceManager; module.exports = { - test_init: function() { - var resource_type_float = new Sealious.ChipTypes.ResourceType({ - name: "float_resource", - fields: [ - {name: "test", type: "float", required: true} - ] - }); - }, + test_init: function() {}, test_start: function() { - describe("FieldType.Float", function(){ - it("should validate the float field successfully (given float)", function(done) { - ResourceManager.create(new Sealious.Context(), "float_resource", {test: 1.2}) - .then(function(ok){ + var field_type_float = Sealious.ChipManager.get_chip("field_type", "float"); + describe("FieldType.Float", function() { + it("should return the description of the field type", function(done) { + if (typeof field_type_float.declaration.get_description() === "string") done(); - }) - .catch(function(error){ - done(error); - }) - }) - it("should check if the float value like 1.0 was stored in the database correctly", function(done) { - var context = new Sealious.Context(); - ResourceManager.create(context, "float_resource", {test: 1.0}) - .then(function(ok){ - ResourceManager.find(context, {test: 1.0}, "float_resource") - .then(function(result){ - if (result[0].test === 1.0) - done(); - else - done(new Error("Not the same number")) - }) - .catch(function(error){ - done(error) - }) - }) - .catch(function(error){ - done(error); - }) - }) - it("should validate the float field successfully (given int)", function(done) { - ResourceManager.create(new Sealious.Context(), "float_resource", {test: 1}) - .then(function(ok){ + else + done(new Error("But it didn't")); + }); + it("should check if is_proper_value works correctly", function(done) { + field_type_float.is_proper_value(new Sealious.Context(), {}, 2.5) + .then(function() { done(); }) - .catch(function(error){ - done(error); + .catch(function(error) { + done(new Error(error)); }) - }) - it("should not validate the float field successfully (given string)", function(done) { - ResourceManager.create(new Sealious.Context(), "float_resource", {test: "silly sealy"}) - .then(function(ok){ - done(new Error("but it did...")); + }); + it("should return error because new_value is not an integer", function(done) { + field_type_float.is_proper_value(new Sealious.Context(), {}, "janusz") + .then(function() { + done("But it didn't"); }) .catch(function(error){ if (error.type === "validation") done(); - else - done(new Error(error)) + else + done(new Error(error)); }) - }) - it("should not validate the float field successfully (no value)", function(done) { - ResourceManager.create(new Sealious.Context(), "float_resource", {}) - .then(function(ok){ - done(new Error("but it did...")); - }) - .catch(function(error){ - if (error.type === "validation") - done(); - else - done(new Error(error)) - }) - }) - }) + }); + it("should check if encode works properly (given \"2\")", function(done) { + if (field_type_float.declaration.encode(new Sealious.Context(), {}, "2.5") === 2.5) + done(); + else + done(new Error("It didn't parse the value correctly")); + }); + }); } }; \ No newline at end of file diff --git a/tests/unit-tests/field-type.int.test.js b/tests/unit-tests/field-type.int.test.js index 8a4ac89b..3126c92b 100644 --- a/tests/unit-tests/field-type.int.test.js +++ b/tests/unit-tests/field-type.int.test.js @@ -1,63 +1,44 @@ var Sealious = require("sealious"); -var ResourceManager = Sealious.ResourceManager; module.exports = { - test_init: function() { - var resource_type_int = new Sealious.ChipTypes.ResourceType({ - name: "int_resource", - fields: [ - {name: "test", type: "int", required: true} - ] - }); - }, + test_init: function() {}, test_start: function() { + var field_type_int = Sealious.ChipManager.get_chip("field_type", "int"); describe("FieldType.Int", function() { - it("should validate the int field successfully (given int)", function(done) { - ResourceManager.create(new Sealious.Context(), "int_resource", {test: 1}) - .then(function(ok){ + it("should return the description of the field type", function(done) { + if (typeof field_type_int.declaration.get_description() === "string") + done(); + else + done(new Error("But it didn't")); + }); + it("should check if is_proper_value works correctly", function(done) { + field_type_int.is_proper_value(new Sealious.Context(), {}, 2) + .then(function() { done(); }) - .catch(function(error){ - done(error); - }) - }) - it("should not validate the int field successfully (given float)", function(done) { - ResourceManager.create(new Sealious.Context(), "int_resource", {test: 1.2}) - .then(function(ok){ - done(new Error("but it did...")); - }) - .catch(function(error){ - if (error.type === "validation") - done(); - else - done(new Error(error)) - }) - }) - it("should not validate the int field successfully (given string)", function(done) { - ResourceManager.create(new Sealious.Context(), "int_resource", {test: "silly sealy"}) - .then(function(ok){ - done(new Error("but it did...")); + .catch(function(error) { + done(new Error(error)); }) - .catch(function(error){ - if (error.type === "validation") - done(); - else - done(new Error(error)) - }) - }) - it("should not validate the int field successfully (no value)", function(done) { - ResourceManager.create(new Sealious.Context(), "int_resource", {}) - .then(function(ok){ - done(new Error("but it did...")); + }); + it("should return error because new_value is not an integer", function(done) { + field_type_int.is_proper_value(new Sealious.Context(), {}, "janusz") + .then(function() { + done("But it didn't"); }) .catch(function(error){ if (error.type === "validation") done(); - else - done(new Error(error)) + else + done(new Error(error)); }) - }) - }) + }); + it("should check if encode works properly (given \"2\")", function(done) { + if (field_type_int.declaration.encode(new Sealious.Context(), {}, "2") === 2) + done(); + else + done(new Error("It didn't parse the value correctly")); + }); + }); } }; \ No newline at end of file diff --git a/tests/unit-tests/field-type.reference.test.js b/tests/unit-tests/field-type.reference.test.js new file mode 100644 index 00000000..dfdcbde2 --- /dev/null +++ b/tests/unit-tests/field-type.reference.test.js @@ -0,0 +1,146 @@ +var Sealious = require("sealious"); + +module.exports = { + test_init: function() { + var listable = new Sealious.ChipTypes.ResourceType({ + name: "testable", + }) + + }, + + test_start: function() { + var field_type_reference = Sealious.ChipManager.get_chip("field_type", "reference"); + describe("FieldType.Reference", function() { + it("checks if is_proper_declaration works correctly", function(done) { + var declar = { + "name": "string", + "allowed_types": ["testable"] + } + try { + field_type_reference.declaration.is_proper_declaration(declar) + done(); + } + catch (e) { + done(new Error(e)); + } + }) + it("checks if is_proper_declaration throws an error (non existing type)", function(done) { + var declar = { + "name": "string", + "allowed_types": ["wrong_type"] + } + try { + field_type_reference.declaration.is_proper_declaration(declar) + done("It didn't throw an error"); + } + catch (e) { + if (e.type === "dev_error") + done(); + else + done(new Error(e)); + + } + }) + it("checks if is_proper_declaration throws an error (wrong attribute)", function(done) { + var declar = { + "nam": "string", + "allowed_types": ["testable"] + } + try { + field_type_reference.declaration.is_proper_declaration(declar) + done("It didn't throw an error"); + } + catch (e) { + if (e.type === "dev_error") + done(); + else + done(new Error(e)); + + } + }) + it("checks if is_proper_value throws an error (no type in value argument)", function(done) { + field_type_reference.is_proper_value(new Sealious.Context(), { allowed_types: ["testable"] }, {}) + .then(function() { + done(new Error("It didn't throw an error")); + }) + .catch(function(error){ + if(error.type === "validation") + done(); + else + done(new Error(error)); + }) + }) + it("checks if is_proper_value throws an error (wrong type given in value argument)", function(done) { + field_type_reference.is_proper_value(new Sealious.Context(), { allowed_types: ["testable"] }, {type: "testabl"}) + .then(function() { + done(new Error("It didn't throw an error")); + }) + .catch(function(error){ + if(error.type === "validation") + done(); + else + done(new Error(error)); + }) + }) + /* TO DO + it("checks if is_proper_value throws an error (wrong type given in value argument)", function(done) { + field_type_reference.is_proper_value(new Sealious.Context(), { allowed_types: ["testable"] }, {type: "testable", data: 1}) + .then(function() { + done(); + }) + .catch(function(error){ + if(error.type === "validation") + done(); + else + done(new Error(error)); + }) + }) + */ + it("checks if is_proper_value works corectly (value is not an object, not found given type)", function(done) { + field_type_reference.is_proper_value(new Sealious.Context(), { allowed_types: ["testable"] }, "testable") + .then(function(result) { + done(); + }) + .catch(function(error){ + if(error.type === "validation") + done(); + else + done(new Error(error)); + }) + }) + //for some reason these tests don't cover the reference field type :/ + it("checks if encode works correctly (resolves a promise with value_in_code which is NOT and instance of Object", function(done){ + field_type_reference.encode(new Sealious.Context(), {}, "test") + .then(function(result){ + if (result === "test") + done(); + else + done(new Error("It didn't resolved correctly")); + }) + }) + it("checks if decode works correctly (resolves a promise with value_in_db not defined)", function(done){ + field_type_reference.encode(new Sealious.Context(), {}, undefined) + .then(function(result){ + if (result === undefined) + done(); + else + done(new Error("It didn't resolved correctly")); + }) + }) + it("checks if decode works correctly (resolves a promise with value_in_db not defined)", function(done){ + Sealious.ResourceManager.create(new Sealious.Context(), "never_fails_resource", { + "#success": "specific_value" + }) + .then(function(created_resource) { + field_type_reference.encode(new Sealious.Context(), {}, created_resource.id) + .then(function(result){ + if (created_resource.id === result) + done(); + else + done(new Error("It didn't return the correct value")) + }) + }) + }) + }) + } +} \ No newline at end of file diff --git a/tests/unit-tests/field-type.text.test.js b/tests/unit-tests/field-type.text.test.js new file mode 100644 index 00000000..9cf10e38 --- /dev/null +++ b/tests/unit-tests/field-type.text.test.js @@ -0,0 +1,80 @@ +var Sealious = require("sealious"); + +module.exports = { + test_init: function() {}, + + test_start: function() { + var field_type_text = Sealious.ChipManager.get_chip("field_type", "text"); + describe("FieldType.Text", function() { + it("returns the description of the field type", function(done) { + if (typeof field_type_text.declaration.get_description(new Sealious.Context(), {max_length: 10}) === "string") + done(); + else + done(new Error("But it didn't")); + }); + it("checks if is_proper_value works correctly", function(done) { + field_type_text.is_proper_value(new Sealious.Context(), {}, 2) + .then(function() { + done(); + }) + .catch(function(error) { + done(new Error(error)); + }) + }); + it("checks if is_proper_value works correctly", function(done) { + field_type_text.is_proper_value(new Sealious.Context(), {max_length: 5}, "123") + .then(function() { + done(); + }) + .catch(function(error) { + done(new Error(error)); + }) + }); + it("checks if is_proper_value works correctly", function(done) { + field_type_text.is_proper_value(new Sealious.Context(), {max_length: 5}, "asdfghjkl") + .then(function() { + done(new Error("It worked correctly")); + }) + .catch(function(error) { + if (error.type === "validation") + done(); + else + done(new Error(error)); + }) + }); + it("checks if encode works properly (sanitizes html)", function(done) { + field_type_text.declaration.encode(new Sealious.Context(), {strip_html: true}, "outside<script>alert(\"a\")</script>") + .then(function(result) { + if (result === "outside") + done(); + else + done("It didn't sanitize the string") + }) + .catch(function(error) { + done(new Error(error)); + }) + }); + it("resolved with null when value_in_code is null", function(done) { + field_type_text.declaration.encode(new Sealious.Context(), {}, null) + .then(function(result) { + if (result === null) + done(); + else + done(new Error("It didn't resolve with null")) + }) + .catch(function(error) { + done(new Error(error)); + }) + }) + it("checks if encode works properly", function(done) { + field_type_text.declaration.encode(new Sealious.Context(), {}, {}) + .then(function(result) { + done(); + }) + .catch(function(error) { + done(new Error(error)); + }) + }); + }); + } +}; \ No newline at end of file diff --git a/tests/unit-tests/file.test.js b/tests/unit-tests/file.test.js new file mode 100644 index 00000000..255c6bf1 --- /dev/null +++ b/tests/unit-tests/file.test.js @@ -0,0 +1,57 @@ +var Sealious = require("sealious"); + +module.exports = { + test_init: function() { + Sealious.File(new Sealious.Context(), "filename", "just_data", "my_test_file", false) + }, + test_start: function() { + describe("Sealious.File", function() { + it("references a file", function(done) { + try { + Sealious.File.Reference("id", "filename") + done(); + } + catch (e) { + done(new Error(e)); + } + }); + it("finds a file from id", function(done) { + console.log("do sprawdzenia") + Sealious.File.from_id(new Sealious.Context(), "my_test_file") + .then(function(result) { + if (result === undefined) + done(); + else + done(new Error("Result isn't undefined")) + }) + .catch(function(error) { + done(new Error(error)); + }) + }) + it("return a new file from db entry", function(done) { + var db_document = { + creation_context: new Sealious.Context(), + original_name: "original_name", + data: "data", + id: "id", + mime_type: "mime_type" + } + + var result = Sealious.File.from_db_entry(db_document, "path"); + if (result.filename === db_document.original_name) + if (result.data === db_document.data) + if (result.id === db_document.id) + if (result.mime === db_document.mime_type) + done(); + else + done(new Error("Wrong mime type")) + else + done(new Error("Wrong id")) + else + done(new Error("Wrong data")) + else + done(new Error("Wrong filename")) + }) + }) + } +} \ No newline at end of file diff --git a/tests/unit-tests/resource-manager.test.js b/tests/unit-tests/resource-manager.test.js index 3300cc4a..276cdbb1 100644 --- a/tests/unit-tests/resource-manager.test.js +++ b/tests/unit-tests/resource-manager.test.js @@ -1,736 +1,738 @@ var Sealious = require("sealious"); var ResourceManager = Sealious.ResourceManager; var assert = require("assert"); var deepEqual = require("deep-equal"); module.exports = { test_init: function() { // Creating resource always fails var listable = new Sealious.ChipTypes.ResourceType({ name: "listable" }) var always_fails = new Sealious.ChipTypes.FieldType({ name: "always_fails", is_proper_value: function(accept, reject, context, value_in_code) { reject(); } }) var one_field_always_fails = new Sealious.ChipTypes.ResourceType({ name: "one_field_always_fails", fields: [{ name: "#fail", type: "always_fails" }] }); //Creating resource never fails var never_fails = new Sealious.ChipTypes.FieldType({ name: "never_fails", is_proper_value: function(accept, reject, context, value_in_code) { accept(); } }) var never_fails_resource = new Sealious.ChipTypes.ResourceType({ name: "never_fails_resource", fields: [{ name: "#success", type: "never_fails" }, ] }); var requires_old_value = new Sealious.ChipTypes.FieldType({ name: "requires_old_value", is_proper_value: function(accept, reject, context, params, new_value, old_value) { if (old_value === undefined) { reject("No old value provided!"); } else { accept(); } }, old_value_sensitive_methods: { is_proper_value: true } }) var rejects_with_old_value = new Sealious.ChipTypes.FieldType({ name: "rejects_with_old_value", is_proper_value: function(accept, reject, context, params, new_value, old_value) { if (old_value === undefined) { accept(); } else { reject("Old value provided"); } }, old_value_sensitive_methods: { is_proper_value: false } }) var always_the_same = new Sealious.ChipTypes.FieldType({ name: "always_the_same", is_proper_value: function(accept, reject, context, params, new_value, old_value) { if (old_value === null) { accept(); } else if (new_value == old_value) { accept(); } else { reject(); } }, old_value_sensitive_methods: { is_proper_value: true } }) var old_value_sensitive_resource = new Sealious.ChipTypes.ResourceType({ name: "old_value_sensitive", fields: [{ name: "value", type: "requires_old_value" }, ] }) var old_value_insensitive_resource = new Sealious.ChipTypes.ResourceType({ name: "old_value_insensitive", fields: [{ name: "value", type: "rejects_with_old_value" }, ] }) var always_the_same_resource = new Sealious.ChipTypes.ResourceType({ name: "always_the_same", fields: [{ name: "value", type: "always_the_same" }, ] }) var multifield_resource = new Sealious.ChipTypes.ResourceType({ name: "multifield", fields: [{ name: "value1", type: "text" }, { name: "value2", type: "text" }] }); var nobody_can_create_me_resource = new Sealious.ChipTypes.ResourceType({ name: "nobody_can_create_me", access_strategy: "noone" }); var nobody_can_update_me_resource = new Sealious.ChipTypes.ResourceType({ name: "nobody_can_update_me", fields: [{ name: "value", type: "text" }], access_strategy: { update: "noone" } }); var nobody_can_delete_me_resource = new Sealious.ChipTypes.ResourceType({ name: "nobody_can_delete_me", fields: [{ name: "value", type: "text" }], access_strategy: { delete: "noone" } }); var nobody_can_list_me_resource = new Sealious.ChipTypes.ResourceType({ name: "nobody_can_list_me", fields: [{ name: "value", type: "text" }], access_strategy: { retrieve: "noone" } }); var item_sensitive_access_strategy = new Sealious.ChipTypes.AccessStrategy({ name: "item_sensitive", checker_function: function(context, item) { if (item !== undefined) { return Promise.resolve(); } else { return Promise.reject(new Sealious.Errors.BadContext("No item provided")); } }, item_sensitive: true }) var item_sensitive_access_strategy_second = new Sealious.ChipTypes.AccessStrategy({ name: "item_sensitive_second", checker_function: function(context, item) { return Promise.reject(new Sealious.Errors.BadContext("No item provided")); }, item_sensitive: true }) var item_sensitive_resource = new Sealious.ChipTypes.ResourceType({ name: "item_sensitive", fields: [{ name: "value", type: "text" }], access_strategy: "item_sensitive" }) var item_sensitive_resource_second = new Sealious.ChipTypes.ResourceType({ name: "item_sensitive_second", fields: [{ name: "value", type: "text" }], access_strategy: "item_sensitive_second" }) var has_required_field = new Sealious.ChipTypes.ResourceType({ name: "has_required_field", fields: [ {name: "required", type: "int", required: true} ] }) }, test_start: function() { describe("ResourceManager", function() { var context = new Sealious.Context(); it('should store creation and modification context', function(done) { var creation_context = new Sealious.Context(); ResourceManager.create(creation_context, "never_fails_resource", { "#success": "yes" }) .then(function(created_resource) { assert.deepEqual(created_resource.created_context, creation_context.toObject(), "Context info stored in resource's `created_context` attribute should reflect the actual context of creating this resource"); return Promise.resolve(created_resource.id); }) .then(function(created_resource_id) { var modification_context = new Sealious.Context(); return ResourceManager.patch_resource(modification_context, "never_fails_resource", created_resource_id, { "#success": "yiss" }) .then(function(patched_resource) { assert.deepEqual(patched_resource.last_modified_context, modification_context.toObject(), "Context info stored in resource's `last_modified` attribute should reflect the actual context of last modification to this resource after performing `patch` on it.") return Promise.resolve(created_resource_id); }); }) .then(function(created_resource_id) { var update_context = new Sealious.Context(); ResourceManager.update_resource(update_context, "never_fails_resource", created_resource_id, { "#success": "yiss plis" }) .then(function(patched_resource) { assert.deepEqual(patched_resource.last_modified_context, update_context.toObject(), "Context info stored in resource's `last_modified` attribute should reflect the actual context of last modification to this resource after performing `update` on it.") }); done(); }) }); describe(".create", function() { it("should create resource", function(done) { var context = new Sealious.Context(); ResourceManager.create(context, "never_fails_resource", { "#success": "specific_value" }) .then(function(created_resource) { return ResourceManager.get_by_id(new Sealious.Context(), created_resource.id) }) .then(function(gotten_resource) { if (gotten_resource.body["#success"] == "specific_value" && deepEqual(context.toObject(), gotten_resource.created_context)) { done(); } else { done(new Error("Created resource differs from what was intended to create.")) } }).catch(function(error) { done(error) }) }) it("should resolve with created resource", function(done) { var context = new Sealious.Context(); ResourceManager.create(context, "never_fails_resource", { "#success": "specific_value" }) .then(function(created_resource) { if (created_resource.body["#success"] == "specific_value" && deepEqual(context.toObject(), created_resource.created_context)) { done(); } else { done(new Error(".create resolves with something else than created resource.")) } }).catch(function(error) { done(error) }) }) it("should throw proper error, if given resouce-type name is non-existent", function(done) { ResourceManager.create(new Sealious.Context(), "non_existent_resource_type", {}) .then(function() { done(new Error("But it succedded instad of failing")); }).catch(function(error) { if (error.data.short_message = "chip_not_found") { done(); } else { done(new Error("But threw an error that is not an instance of ValidationError")); } }) }) it("should provide resource_type.validate_field_values method with the previous field values, if it is needed", function(done) { ResourceManager.create(new Sealious.Context(), "old_value_sensitive", { value: "any" }) .then(function(created_resource) { done(); }).catch(function() { done(new Error("But it didn't")); }) }); it("should NOT provide resource_type.validate_field_values method with the previous field value if it's not needed", function(done) { ResourceManager.create(new Sealious.Context(), "old_value_insensitive", { value: "any" }) .then(function() { done(); }).catch(function() { done(new Error("But it didn't")); }) }); it("doesn't create resource, when access_strategy doesn't allow it", function(done) { ResourceManager.create(new Sealious.Context(), "nobody_can_create_me", {}) .then(function() { done(new Error("It succedded instead of failing")) }).catch(function(error) { if (error.type == "permission") { done() } else { done(new Error("It didn't throw proper error.")) } }) }) it("should provide access_strategy.check with null if the access_strategy is item_sensitive", function(done) { ResourceManager.create(new Sealious.Context(), "item_sensitive", { value: "any" }) .then(function(created_resource) { done(); }).catch(function(error) { if (error.type == "permission") { done(new Error("But it didn't")); } else { done(error); } }) }); it('should not create a new resource', function(done) { ResourceManager.create(new Sealious.Context(), "one_field_always_fails", { "#fail": "tak" }) .then(function() { done(new Error("It didn't throw an error!")); }).catch(function(error) { if (error.type == "validation"){ done(); } else { done(new Error("It did throw an error, but not THE error")); } }); }); }); describe(".delete", function() { it("should throw proper error, if given resouce-type name is non-existent", function(done) { ResourceManager.delete(new Sealious.Context(), "non_existent_resource_type", "id", {}) .then(function() { done(new Error("But it succedded instad of failing")); }).catch(function(error) { if (error.type == "validation") { done(); } else { done(new Error("But threw an error that is not an instance of ValidationError")); } }); }); it('should delete the resource', function(done) { var created_resource_id; ResourceManager.create(context, "never_fails_resource", { "#success": "tak" }) .then(function(created_resource) { created_resource_id = created_resource.id; return ResourceManager.delete(context, "never_fails_resource", created_resource_id); }) .then(function() { return ResourceManager.get_by_id(new Sealious.Context, created_resource_id); }) .then(function(result) { done(new Error("But it didn't")); }) .catch(function(error) { if (error.type == "not_found") { done(); } else { done(error); } }) }); }); describe(".get_by_id", function() { it("should find resource of given id", function(done) { ResourceManager.create(new Sealious.Context(), "never_fails_resource", { "#success": "specific_value" }) .then(function(created_resource) { return ResourceManager.get_by_id(new Sealious.Context(), created_resource.id) }) .then(function(gotten_resource) { if (gotten_resource.body["#success"] == "specific_value") { done(); } else { done(new Error("It didn't get proper resource.")) } }).catch(function(error) { done(error) }) }) it("should return proper error, if provided id is incorrect", function(done) { ResourceManager.get_by_id(new Sealious.Context(), "incorrect_resource_id*!@#R%^&*()") .then(function() { done(new Error("But it succeded instead of failing")) }).catch(function(error) { if (error.type = "not_found") { done(); } else { done(new Error("But threw an error that is not an instance of NotFoundError")); } }) }); it("doesn't get resource, when access_strategy doesn't allow it", function(done) { ResourceManager.create(new Sealious.Context(), "nobody_can_list_me", {}) .then(function(created_resource) { return ResourceManager.get_by_id(new Sealious.Context(), created_resource.id) }) .then(function() { done(new Error("It succedded instead of failing")) }).catch(function(error) { if (error.type == "permission") { done() } else { done(new Error("It didn't throw proper error.")) } }) }) }); describe(".list_by_type", function() { it("uses a resource with item_sensitive access strategy", function(done) { ResourceManager.list_by_type(new Sealious.Context(),"item_sensitive") .then(function(){ done(); }) .catch(function(error) { done(error); }); }); it("uses a resource with item_sensitive access strategy and should throw an error", function(done) { ResourceManager.create(new Sealious.Context(), "item_sensitive_second", { value: "any" }) .then(function(created_resource) { }).catch(function(error) { - console.log(error); + }) ResourceManager.list_by_type(new Sealious.Context(),"item_sensitive_second") .then(function(ok){ done(); }) .catch(function(error) { done(error); }); }); it("throws proper error, if given resouce-type name is non-existent", function(done) { ResourceManager.list_by_type(new Sealious.Context(), "non_existent_resource_type") .then(function() { done(new Error("It succedded instead of failing")); }) .catch(function(error) { if (error.type = "validation") { done(); } else { done(new Error("It threw an error that is not an instance of ValidationError")); } }); }) it("lists resources of given type", function(done) { var promises = []; var resource_ids; for (var i = 1; i <= 3; i++) { var promise = ResourceManager.create(new Sealious.Context(), "listable", {}); promises.push(promise); } Promise.all(promises) .then(function(created_resources) { resource_ids = created_resources.map(function(resource) { return resource.id; }) return ResourceManager.list_by_type(new Sealious.Context(), "listable") }) .then(function(listed_resources) { var listed_resources_ids = listed_resources.map(function(resource) { return resource.id; }) listed_resources_ids.sort(); resource_ids.sort(); if (deepEqual(listed_resources_ids, resource_ids)) { done() } else if (listed_resources_ids.length != resource_ids.length) { done(new Error("It listed wrong amount of resources")) } else { done(new Error("It didn't return proper resources")) } }) }) }); describe(".find", function() { it('should find all resources', function(done){ ResourceManager.find(context) .then(function(found_resource){ if (found_resource[0] !== undefined){ done(); } else { done(new Error ("It didn't find any resources")); } }) .catch(function(error){ done(error); }) }); it('should create a new resource and find it', function(done){ ResourceManager.create(context, "never_fails_resource", { "#success": "absolutely" }) .then(function(created_resource){ ResourceManager.find(context, {"#success": created_resource.body["#success"]}, "never_fails_resource") .then(function(found_resource){ if (found_resource[0]["#success"] == "absolutely"){ done(); } else if (found_resource[1] !== undefined) { done(new Error("Found more resources than should have")) } else { done(new Error("It didn't find the resource")); } }) .catch(function(error){ done(error); }) }) }); it('should create a new resource and find it with without specifing the type', function(done){ ResourceManager.create(context, "never_fails_resource", { "#success": "absolutely" }) .then(function(created_resource){ ResourceManager.find(context, {"#success": created_resource.body["#success"]}) .then(function(found_resource){ if (found_resource[0]["#success"] == "absolutely"){ done(); } else if (found_resource[1] !== undefined) { done(new Error("Found more resourced than should have")) } else { done(new Error("It didn't find the resource")); } }) .catch(function(error){ done(error); }) }) }); it('should not find a resource', function(done){ ResourceManager.find(context, {"#troll_field": "troll_value"}, "troll_resource_type") .then(function(found_resource){ if (found_resource[0] === undefined){ done(); } else { done(new Error("It found a resource")); } }) .catch(function(error){ done(error); }) }); it('should create a new resource it, find it, then delete it and not find it', function(done){ ResourceManager.create(context, "never_fails_resource", { "#success": "yep" }) .then(function(created_resource){ ResourceManager.find(context, {"#success": created_resource.body["#success"]}, "never_fails_resource") .then(function(found_resource){ if (found_resource[0]["#success"] == "yep"){ ResourceManager.delete(context, "never_fails_resource", found_resource[0]["id"]) .then(function(success){ ResourceManager.find(context, {"#success": "yep"}, "never_fails_resource") .then(function(found_resource){ if (found_resource[0] === undefined){ done(); } else { done(new Error("It found the resource which should be deleted")); } }) .catch(function(error){ done(error); }) }) .catch(function(error){book.com/ done(error); }) } else { done(new Error("It didn't find the resource after creating it")) } }) .catch(function(error){ done(error); }) }) }); }); describe(".patch_resource", function() { it("should throw proper error, if given resource-type name is non-existent", function(done) { ResourceManager.patch_resource(new Sealious.Context(), "non_existent_resource_type", "id", {}) .then(function() { done(new Error("But it succedded instead of failing")); }).catch(function(error) { if (error.type = "validation") { done(); } else { done(new Error("But threw an error that is not an instance of ValidationError")); } }) }) it("should provide resource_type.validate_field_values method with the previous field value if it is needed", function(done) { ResourceManager.create(new Sealious.Context(), "old_value_sensitive", { value: "any" }) .then(function(created_resource) { return ResourceManager.patch_resource(new Sealious.Context(), "old_value_sensitive", created_resource.id, { value: "any2" }) }).then(function() { done(); }).catch(function() { done(new Error("But it didn't.")); }) }); describe("should provide real old value, if old_value is needed", function() { it("always_the_same_resource can be patched with the same value it was created", function(done) { ResourceManager.create(new Sealious.Context(), "always_the_same", { value: 33 }) .then(function(created_resource) { return ResourceManager.patch_resource(new Sealious.Context(), "always_the_same", created_resource.id, { value: 33 }) }).then(function() { done(); }).catch(function() { done(new Error("But it didn't.")); }) }); it("always_the_same_resource can't be patched with other value than the one it was created with", function(done) { ResourceManager.create(new Sealious.Context(), "always_the_same", { value: 33 }) .then(function(created_resource) { return ResourceManager.patch_resource(new Sealious.Context(), "always_the_same", created_resource.id, { value: 42 }) }).then(function() { done(new Error("But it was.")); }).catch(function() { done(); }) }); }) it("should NOT provide resource_type.validate_field_values method with the previous field value if it's not needed", function(done) { ResourceManager.create(new Sealious.Context(), "has_required_field", {required: 1}) .then(function(created_resource){ return ResourceManager.patch_resource(new Sealious.Context(), "has_required_field", created_resource.id, {}) }).then(function(){ done(); }).catch(function(error){ done(error) }) }); }); describe(".update_resource", function() { it('should update the resource', function(done) { ResourceManager.create(context, "never_fails_resource", { "#success": "tak" }) .then(function(result) { ResourceManager.update_resource(context, "never_fails_resource", result.id, { "#success": "tak2" }) .then(function(updated_resource) { return ResourceManager.get_by_id(context, updated_resource.id); }) .then(function(gotten_resource) { if (gotten_resource.body["#success"] == "tak2" && deepEqual(context.toObject(), gotten_resource.created_context)) { done(); } else { done(new Error("Updated resource differs from what was intended to update.")) } }) .catch(function(error) { done(new Error(error)); }) }).catch(function(error) { done(error); }); }); it("should throw proper error, if given resouce-type name is non-existent", function(done) { ResourceManager.update_resource(new Sealious.Context(), "non_existent_resource_type", "id", {}) .then(function() { done(new Error("But it succedded instead of failing")); }).catch(function(error) { - if (error.type == "validation") { + if (error.type === "validation") { done(); } else { done(new Error("But threw an error that is not an instance of ValidationError")); } }) }) + /* it("should remove a value if it's not provided", function(done) { ResourceManager.create(new Sealious.Context(), "multifield", { value1: "1", value2: "2" }).then(function(created_resource) { return ResourceManager.update_resource(new Sealious.Context(), "multifield", created_resource.id, { value1: "3" }) }).then(function(updated_resource) { if (updated_resource.body.value2 === null) { done(); } else { done(new Error("But it didn't")); } }) }); +*/ it("should not result in calling 'is_proper_value' for fields with no value (issue #235)", function(done){ ResourceManager.create(new Sealious.Context(), "one_field_always_fails", {}) .then(function(created_resource){ return ResourceManager.update_resource(new Sealious.Context(), "one_field_always_fails", created_resource.id, {}) }).then(function(){ //if the field's 'is_proper_value' method wasn't called, it shouldn't throw an error done(); }).catch(function(error){ done(error); }) }) }); }) //should test if ResourceManager properly asks AccessStrategy. } } \ No newline at end of file diff --git a/tests/unit-tests/response.test.js b/tests/unit-tests/response.test.js new file mode 100644 index 00000000..14110020 --- /dev/null +++ b/tests/unit-tests/response.test.js @@ -0,0 +1,25 @@ +var Sealious = require("sealious"); + +module.exports = { + test_init: function() { + Sealious.Response({"field": "value"}, true, "type", "message") + }, + test_start: function() { + describe("Sealious.Response", function() { + it("returns a Sealious.Response object", function(done) { + var error = new Sealious.Errors.BadContext("This is an error", "test_data"); + var response = Sealious.Response.fromError(error); + if (response.data === "test_data") + if (response.type === "permission") + if (response.status_message === "This is an error") + done(); + else + done("Wrong status_message") + else + done("Wrong type") + else + done("Wrong data") + }) + }) + } +}; \ No newline at end of file