diff --git a/CHANGELOG.md b/CHANGELOG.md index 02ef9ff0357..9b84961e07f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,19 @@ 🚀 IN PRODUCTION 🚀 (https://www.npmjs.com/package/@tiledesk/tiledesk-server/v/2.3.77) +# 2.10.40 +- bug fix: empty string for slug and index issue + +# 2.10.39 +- updated whatsapp-connector to 0.1.76 +- updated tybot-connector to 0.2.139 +- added chatbot slug in faq_kb model +- added /replace endpoint in request + +# 2.10.38 +- updated whatsapp-worker to 0.1.11 +- added index to request model + # 2.10.36 - updated tybot-connector to 0.2.138 diff --git a/errorCodes.js b/errorCodes.js index 4f9da4454d7..9632b3681e0 100644 --- a/errorCodes.js +++ b/errorCodes.js @@ -26,6 +26,12 @@ const errorCodes = { ACCESS_DENIED: 11003, }, }, + CHATBOT: { + BASE_CODE: 12000, + ERRORS: { + DUPLICATE_SLUG: 12001 + } + } // Aggiungi altre route }; diff --git a/middleware/passport.js b/middleware/passport.js index d7fb44fcc58..ab0e61e9561 100644 --- a/middleware/passport.js +++ b/middleware/passport.js @@ -72,6 +72,14 @@ if (process.env.GOOGLE_SIGNIN_ENABLED=="true" || process.env.GOOGLE_SIGNIN_ENABL winston.info('Authentication Google Signin enabled : ' + enableGoogleSignin); + +var enableOauth2Signin = false; +if (process.env.OAUTH2_SIGNIN_ENABLED=="true" || process.env.OAUTH2_SIGNIN_ENABLED == true) { + enableOauth2Signin = true; +} +winston.info('Authentication Oauth2 Signin enabled : ' + enableOauth2Signin); + + var jwthistory = undefined; try { jwthistory = require('@tiledesk-ent/tiledesk-server-jwthistory'); @@ -573,6 +581,248 @@ if (enableGoogleSignin==true) { } +if (enableOauth2Signin==true) { + + const OAuth2Strategy = require('passport-oauth2'); + OAuth2Strategy.prototype.userProfile = function(accessToken, done) { + + winston.debug("accessToken " + accessToken) + + + /* + https://stackoverflow.com/questions/66452108/keycloak-get-users-returns-403-forbidden + The service account associated with your client needs to be allowed to view the realm users. + Go to http://localhost:8080/auth/admin/{realm_name}/console/#/realms/{realm_name}/clients + Select your client (which must be a confidential client) + In the settings tab, switch Service Account Enabled to ON + Click on save, the Service Account Roles tab will appear + In Client Roles, select realm_management + Scroll through available roles until you can select view_users + Click on Add selected + You should have something like this : + */ + + + // ATTENTION You have to add a client scope after as described here: https://keycloak.discourse.group/t/issue-on-userinfo-endpoint-at-keycloak-20/18461/4 + + // console.log("this._oauth2", this._oauth2) + this._oauth2._useAuthorizationHeaderForGET = true; + this._oauth2.get( process.env.OAUTH2_USER_INFO_URL, accessToken, (err, body) => { + if (err) { + return done(err); + } + + try { + winston.debug("body", body); + + const json = JSON.parse(body); + const userInfo = { + keycloakId: json.sub, + fullName: json.name, + firstName: json.given_name, + lastName: json.family_name, + username: json.preferred_username, + email: json.email, + // avatar: json.avatar, + // realm: this.options.realm, + }; + winston.debug("userInfo", userInfo); + + done(null, userInfo); + } catch (e) { + done(e); + } + }); + }; + + + passport.use(new OAuth2Strategy({ + authorizationURL: process.env.OAUTH2_AUTH_URL, + tokenURL: process.env.OAUTH2_TOKEN_URL, + clientID: process.env.OAUTH2_CLIENT_ID, + clientSecret: process.env.OAUTH2_CLIENT_SECRET, + callbackURL: process.env.OAUTH2_CALLBACK_URL || "http://localhost:3000/auth/oauth2/callback" + }, + function(accessToken, refreshToken, params, profile, cb) { + winston.debug("params", params); + + + const token = jwt.decode(accessToken); // user id lives in here + winston.debug("token", token); + + const profileInfo = jwt.decode(params.id_token); // user email lives in here + winston.debug("profileInfo", profileInfo); + + winston.debug("profile", profile); + + winston.debug("accessToken", accessToken); + + winston.debug("refreshToken", refreshToken); + + var issuer = token.iss; + var email = profile.email; + + var query = {providerId : issuer, subject: profile.keycloakId}; + winston.debug("query", query) + + Auth.findOne(query, function(err, cred){ + winston.debug("cred", cred, err); + if (err) { return cb(err); } + if (!cred) { + // The oauth account has not logged in to this app before. Create a + // new user record and link it to the oauth account. + var password = uniqid() + // signup ( email, password, firstname, lastname, emailverified) { + userService.signup(email, password, profile.displayName, "", true) + .then(function (savedUser) { + + winston.debug("savedUser", savedUser) + + var auth = new Auth({ + providerId: issuer, + email: email, + subject: profile.keycloakId, + }); + auth.save(function (err, authSaved) { + if (err) { return cb(err); } + winston.debug("authSaved", authSaved); + + return cb(null, savedUser); + }); + }).catch(function(err) { + winston.error("Error signup oauth ", err); + return cb(err); + }); + } else { + // The Oauth account has previously logged in to the app. Get the + // user record linked to the Oauth account and log the user in. + + User.findOne({ + email: email, status: 100 + }, 'email firstname lastname emailverified id', function (err, user) { + + winston.debug("user",user, err); + // winston.debug("usertoJSON()",user.toJSON()); + + if (err) { + winston.error("Error getting user",user, err); + return cb(err); + } + + if (!user) { + winston.info("User not found",user, err); + return cb(null, false); + } + + return cb(null, user); + }); + } + }); + } + )); +} + + + +// const KeycloakStrategy = require('@exlinc/keycloak-passport') + + +// // Register the strategy with passport +// passport.use( +// "keycloak", +// new KeycloakStrategy( +// { +// host: process.env.KEYCLOAK_HOST, +// realm: process.env.KEYCLOAK_REALM, +// clientID: process.env.KEYCLOAK_CLIENT_ID, +// clientSecret: process.env.KEYCLOAK_CLIENT_SECRET, +// callbackURL: `${process.env.AUTH_KEYCLOAK_CALLBACK}`, +// authorizationURL : `${process.env.KEYCLOAK_HOST}/realms/${process.env.KEYCLOAK_REALM}/protocol/openid-connect/auth`, +// tokenURL : `${process.env.KEYCLOAK_HOST}/realms/${process.env.KEYCLOAK_REALM}/protocol/openid-connect/token`, +// userInfoURL : `${process.env.KEYCLOAK_HOST}/realms/${process.env.KEYCLOAK_REALM}/protocol/openid-connect/userinfo` +// // authorizationURL: '123', +// // tokenURL : '123', +// // userInfoURL: '123' +// }, +// (accessToken, refreshToken, profile, done) => { + + +// const token = jwt.decode(accessToken); // user id lives in here +// console.log("token", token); + +// console.log("profile", profile); + +// console.log("accessToken", accessToken); + +// console.log("refreshToken", refreshToken); + +// var issuer = token.iss; +// var email = profile.email; + +// var query = {providerId : issuer, subject: profile.keycloakId}; +// winston.info("query", query) + +// Auth.findOne(query, function(err, cred){ +// winston.info("cred", cred, err); +// if (err) { return cb(err); } +// if (!cred) { +// // The oauth account has not logged in to this app before. Create a +// // new user record and link it to the oauth account. +// var password = uniqid() +// // signup ( email, password, firstname, lastname, emailverified) { +// userService.signup(email, password, profile.displayName, "", true) +// .then(function (savedUser) { + +// winston.info("savedUser", savedUser) + +// var auth = new Auth({ +// providerId: issuer, +// email: email, +// subject: profile.keycloakId, +// }); +// auth.save(function (err, authSaved) { +// if (err) { return cb(err); } +// winston.info("authSaved", authSaved); + +// return cb(null, savedUser); +// }); +// }).catch(function(err) { +// winston.error("Error signup oauth ", err); +// return cb(err); +// }); +// } else { +// // The Oauth account has previously logged in to the app. Get the +// // user record linked to the Oauth account and log the user in. + +// User.findOne({ +// email: email, status: 100 +// }, 'email firstname lastname emailverified id', function (err, user) { + +// winston.info("user",user, err); +// winston.info("usertoJSON()",user.toJSON()); + +// if (err) { +// winston.error("Error getting user",user, err); +// return cb(err); +// } + +// if (!user) { +// winston.info("User not found",user, err); +// return cb(null, false); +// } + +// return done(null, user); +// }); +// } +// }); +// } +// )); + + + + + + // var OidcStrategy = require('passport-openidconnect').Strategy; diff --git a/models/faq.js b/models/faq.js index 671bcb6d5dc..e2fc9ba188d 100644 --- a/models/faq.js +++ b/models/faq.js @@ -7,12 +7,12 @@ const uuidv4 = require('uuid/v4'); var defaultFullTextLanguage = process.env.DEFAULT_FULLTEXT_INDEX_LANGUAGE || "none"; var FaqSchema = new Schema({ - _id: { - type: mongoose.Schema.Types.ObjectId, - index: true, - required: true, - auto: true, - }, + // _id: { + // type: mongoose.Schema.Types.ObjectId, + // index: true, + // required: true, + // auto: true, + // }, id_faq_kb: { type: String, index: true diff --git a/models/faq_kb.js b/models/faq_kb.js index 64b219f163a..0cce03397db 100644 --- a/models/faq_kb.js +++ b/models/faq_kb.js @@ -128,12 +128,38 @@ var Faq_kbSchema = new Schema({ default: function () { return this.isNew ? false : undefined; }, + }, + slug: { + type: String, + required: false, + index: true } -}, { +},{ timestamps: true }); +Faq_kbSchema.pre("save", async function (next) { + // Check if the document is new and if the slug has not been set manually + if (this.isNew && !this.slug) { + const baseSlug = generateSlug(this.name); + let uniqueSlug = baseSlug; + + const existingCount = await mongoose.model("faq_kb").countDocuments({ + id_project: this.id_project, + slug: { $regex: `^${baseSlug}(?:-\\d+)?$` } + }); + + if (existingCount > 0) { + uniqueSlug = `${baseSlug}-${existingCount}`; + } + + this.slug = uniqueSlug; + } + + next(); +}); + Faq_kbSchema.virtual('fullName').get(function () { // winston.debug("faq_kb fullName virtual called"); return (this.name); @@ -142,8 +168,14 @@ Faq_kbSchema.virtual('fullName').get(function () { Faq_kbSchema.index({certified: 1, public: 1}); //suggested by atlas -Faq_kbSchema.index({name: 'text', description: 'text', "tags": 'text'}, - {"name":"faqkb_fulltext","default_language": defaultFullTextLanguage,"language_override": "language"}); // schema level +Faq_kbSchema.index( + {name: 'text', description: 'text', "tags": 'text'}, + {"name":"faqkb_fulltext","default_language": defaultFullTextLanguage,"language_override": "language"}); // schema level + +Faq_kbSchema.index( + { id_project: 1, slug: 1 }, + { unique: true, partialFilterExpression: { slug: { $exists: true } } } +); var faq_kb = mongoose.model('faq_kb', Faq_kbSchema); @@ -153,6 +185,16 @@ if (process.env.MONGOOSE_SYNCINDEX) { winston.info("faq_kb syncIndexes") } - +function generateSlug(name) { + return name + .toLowerCase() + .trim() + .normalize("NFD") // Normalize characters with accents + .replace(/[\u0300-\u036f]/g, "") // Removes diacritics (e.g. à becomes a) + .replace(/[^a-z0-9\s-_]/g, "") // Remove special characters + .replace(/\s+/g, "-") // Replaces spaces with dashes + .replace(/_/g, "-") + .replace(/-+/g, "-"); // Removes consecutive hyphens +} module.exports = faq_kb diff --git a/models/request.js b/models/request.js index c771f918d3a..88725e1e435 100644 --- a/models/request.js +++ b/models/request.js @@ -504,6 +504,7 @@ RequestSchema.index({ id_project: 1, preflight: 1, createdAt: 1 }) RequestSchema.index({ participants: 1, id_project: 1, createdAt: -1, status: 1 }) RequestSchema.index({ id_project: 1, "snapshot.lead.email": 1, createdAt: -1, status: 1 }) RequestSchema.index({ id_project: 1, createdAt: -1, status: 1 }) +RequestSchema.index({ id_project: 1, preflight: 1, smartAssignment: 1, "snapshot.department.routing": 1, createdAt: 1, status: 1 }) // ERROR DURING DEPLOY OF 2.10.27 //RequestSchema.index({ id_project: 1, participants: 1, "snapshot.agents.id_user": 1, createdAt: -1, status: 1 }) diff --git a/package-lock.json b/package-lock.json index 6b69478c41d..e1aa9dff376 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@tiledesk/tiledesk-server", - "version": "2.10.36", + "version": "2.10.40", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@tiledesk/tiledesk-server", - "version": "2.10.36", + "version": "2.10.40", "hasInstallScript": true, "license": "AGPL-3.0", "dependencies": { @@ -24,11 +24,11 @@ "@tiledesk/tiledesk-sms-connector": "^0.1.11", "@tiledesk/tiledesk-telegram-connector": "^0.1.14", "@tiledesk/tiledesk-train-jobworker": "^0.0.11", - "@tiledesk/tiledesk-tybot-connector": "^0.2.138", + "@tiledesk/tiledesk-tybot-connector": "^0.2.139", "@tiledesk/tiledesk-voice-twilio-connector": "^0.1.12", "@tiledesk/tiledesk-vxml-connector": "^0.1.67", - "@tiledesk/tiledesk-whatsapp-connector": "^0.1.75", - "@tiledesk/tiledesk-whatsapp-jobworker": "^0.0.10", + "@tiledesk/tiledesk-whatsapp-connector": "^0.1.76", + "@tiledesk/tiledesk-whatsapp-jobworker": "^0.0.11", "amqplib": "^0.5.5", "app-root-path": "^3.0.0", "bcrypt-nodejs": "0.0.3", @@ -78,6 +78,7 @@ "passport-google-oidc": "^0.1.0", "passport-http": "^0.3.0", "passport-jwt": "^4.0.0", + "passport-oauth2": "^1.8.0", "pdfmake": "^0.2.5", "promise-events": "^0.2.4", "request": "^2.88.2", @@ -4124,9 +4125,9 @@ } }, "node_modules/@tiledesk/tiledesk-tybot-connector": { - "version": "0.2.138", - "resolved": "https://registry.npmjs.org/@tiledesk/tiledesk-tybot-connector/-/tiledesk-tybot-connector-0.2.138.tgz", - "integrity": "sha512-VDIFqCKTI/5WUSEbnv+Y+jcMBi8dza7FsWv0eXv6ZoYjmm3gkTIkC2q5JvSZILbZcra9+X7K6OkpQViL9t8PiQ==", + "version": "0.2.139", + "resolved": "https://registry.npmjs.org/@tiledesk/tiledesk-tybot-connector/-/tiledesk-tybot-connector-0.2.139.tgz", + "integrity": "sha512-ux4+AZFjKRu9OgmEEfF8F9RgeEYxLo9Jzj/71ENY3R2Sj1t2Fz6tbYXZX0f14ZOl4u920FKQvbWQmYGuuOjM5Q==", "dependencies": { "@tiledesk/helpcenter-query-client": "^0.1.8", "@tiledesk/tiledesk-chatbot-client": "^0.5.30", @@ -4146,11 +4147,48 @@ "mongoose": "^6.3.5", "multer": "^1.4.5-lts.1", "nanoid": "^3.1.25", - "redis": "^3.1.2", + "redis": "^4.7.0", "uuid": "^3.3.3", "vm2": "^3.9.13" } }, + "node_modules/@tiledesk/tiledesk-tybot-connector/node_modules/@redis/client": { + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/@redis/client/-/client-1.6.0.tgz", + "integrity": "sha512-aR0uffYI700OEEH4gYnitAnv3vzVGXCFvYfdpu/CJKvk4pHfLPEy/JSZyrpQ+15WhXe1yJRXLtfQ84s4mEXnPg==", + "dependencies": { + "cluster-key-slot": "1.1.2", + "generic-pool": "3.9.0", + "yallist": "4.0.0" + }, + "engines": { + "node": ">=14" + } + }, + "node_modules/@tiledesk/tiledesk-tybot-connector/node_modules/@redis/json": { + "version": "1.0.7", + "resolved": "https://registry.npmjs.org/@redis/json/-/json-1.0.7.tgz", + "integrity": "sha512-6UyXfjVaTBTJtKNG4/9Z8PSpKE6XgSyEb8iwaqDcy+uKrd/DGYHTWkUdnQDyzm727V7p21WUMhsqz5oy65kPcQ==", + "peerDependencies": { + "@redis/client": "^1.0.0" + } + }, + "node_modules/@tiledesk/tiledesk-tybot-connector/node_modules/@redis/search": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/@redis/search/-/search-1.2.0.tgz", + "integrity": "sha512-tYoDBbtqOVigEDMAcTGsRlMycIIjwMCgD8eR2t0NANeQmgK/lvxNAvYyb6bZDD4frHRhIHkJu2TBRvB0ERkOmw==", + "peerDependencies": { + "@redis/client": "^1.0.0" + } + }, + "node_modules/@tiledesk/tiledesk-tybot-connector/node_modules/@redis/time-series": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/@redis/time-series/-/time-series-1.1.0.tgz", + "integrity": "sha512-c1Q99M5ljsIuc4YdaCwfUEXsofakb9c8+Zse2qxTadu8TalLXuAESzLvFAvNVbkmSlvlzIQOLpBCmWI9wTOt+g==", + "peerDependencies": { + "@redis/client": "^1.0.0" + } + }, "node_modules/@tiledesk/tiledesk-tybot-connector/node_modules/axios": { "version": "1.7.7", "resolved": "https://registry.npmjs.org/axios/-/axios-1.7.7.tgz", @@ -4286,6 +4324,19 @@ "node": ">= 6.0.0" } }, + "node_modules/@tiledesk/tiledesk-tybot-connector/node_modules/redis": { + "version": "4.7.0", + "resolved": "https://registry.npmjs.org/redis/-/redis-4.7.0.tgz", + "integrity": "sha512-zvmkHEAdGMn+hMRXuMBtu4Vo5P6rHQjLoHftu+lBqq8ZTA3RCVC/WzD790bkKKiNFp7d5/9PcSD19fJyyRvOdQ==", + "dependencies": { + "@redis/bloom": "1.2.0", + "@redis/client": "1.6.0", + "@redis/graph": "1.1.1", + "@redis/json": "1.0.7", + "@redis/search": "1.2.0", + "@redis/time-series": "1.1.0" + } + }, "node_modules/@tiledesk/tiledesk-tybot-connector/node_modules/sift": { "version": "16.0.1", "resolved": "https://registry.npmjs.org/sift/-/sift-16.0.1.tgz", @@ -4299,6 +4350,11 @@ "node": ">=10.0.0" } }, + "node_modules/@tiledesk/tiledesk-tybot-connector/node_modules/yallist": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz", + "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==" + }, "node_modules/@tiledesk/tiledesk-voice-twilio-connector": { "version": "0.1.12", "resolved": "https://registry.npmjs.org/@tiledesk/tiledesk-voice-twilio-connector/-/tiledesk-voice-twilio-connector-0.1.12.tgz", @@ -4930,9 +4986,9 @@ } }, "node_modules/@tiledesk/tiledesk-whatsapp-connector": { - "version": "0.1.75", - "resolved": "https://registry.npmjs.org/@tiledesk/tiledesk-whatsapp-connector/-/tiledesk-whatsapp-connector-0.1.75.tgz", - "integrity": "sha512-cBLPcJvUDhhnXi6qKQBDtHewVAOQd8yTp0Js4BZMajv1lmy710GeMMc3EUfLLbqIvmmMdJVc1Iu5Rs6OuFIDWQ==", + "version": "0.1.76", + "resolved": "https://registry.npmjs.org/@tiledesk/tiledesk-whatsapp-connector/-/tiledesk-whatsapp-connector-0.1.76.tgz", + "integrity": "sha512-i86fSodEp5+In3EQADVGxk7y3XJYmH14ogGaNUZMkMnFUhdZ9E01JiKNO5dvHAARgX2IBfeQtLUlgEfvj045Xw==", "dependencies": { "@tiledesk/tiledesk-client": "^0.10.4", "app-root-path": "^3.0.0", @@ -5112,9 +5168,9 @@ } }, "node_modules/@tiledesk/tiledesk-whatsapp-jobworker": { - "version": "0.0.10", - "resolved": "https://registry.npmjs.org/@tiledesk/tiledesk-whatsapp-jobworker/-/tiledesk-whatsapp-jobworker-0.0.10.tgz", - "integrity": "sha512-9Z+JqJND6jaSRMxO1hle4lL1vOD9iOA3lNPYVEmciLLv5xZGQwtxUn2gOD2qeUkncptmejgb4QsT5yu988HwdQ==", + "version": "0.0.11", + "resolved": "https://registry.npmjs.org/@tiledesk/tiledesk-whatsapp-jobworker/-/tiledesk-whatsapp-jobworker-0.0.11.tgz", + "integrity": "sha512-d3XKokv90U/UfzIB3f7MYEQhzysDlSvDkpOlEXakq16vd3TXVCfIniSG+uLd7bKE8ej8TEjiwgo3aW8Rg/0N/w==", "dependencies": { "@tiledesk/tiledesk-client": "^0.10.4", "@types/node": "^18.0.6", @@ -6464,6 +6520,14 @@ } ] }, + "node_modules/base64url": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/base64url/-/base64url-3.0.1.tgz", + "integrity": "sha512-ir1UPr3dkwexU7FdV8qBBbNDRUhMmIekYMFZfi+C/sLNnRESKPl23nB9b2pltqfOQNnGzsDdId90AEtG5tCx4A==", + "engines": { + "node": ">=6.0.0" + } + }, "node_modules/basic-auth": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/basic-auth/-/basic-auth-2.0.1.tgz", @@ -15209,6 +15273,30 @@ "resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz", "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==" }, + "node_modules/passport-oauth2": { + "version": "1.8.0", + "resolved": "https://registry.npmjs.org/passport-oauth2/-/passport-oauth2-1.8.0.tgz", + "integrity": "sha512-cjsQbOrXIDE4P8nNb3FQRCCmJJ/utnFKEz2NX209f7KOHPoX18gF7gBzBbLLsj2/je4KrgiwLLGjf0lm9rtTBA==", + "dependencies": { + "base64url": "3.x.x", + "oauth": "0.10.x", + "passport-strategy": "1.x.x", + "uid2": "0.0.x", + "utils-merge": "1.x.x" + }, + "engines": { + "node": ">= 0.4.0" + }, + "funding": { + "type": "github", + "url": "https://github.com/sponsors/jaredhanson" + } + }, + "node_modules/passport-oauth2/node_modules/oauth": { + "version": "0.10.0", + "resolved": "https://registry.npmjs.org/oauth/-/oauth-0.10.0.tgz", + "integrity": "sha512-1orQ9MT1vHFGQxhuy7E/0gECD3fd2fCC+PIX+/jgmU/gI3EpRocXtmtvxCO5x3WZ443FLTLFWNDjl5MPJf9u+Q==" + }, "node_modules/passport-openidconnect": { "version": "0.1.1", "resolved": "https://registry.npmjs.org/passport-openidconnect/-/passport-openidconnect-0.1.1.tgz", @@ -17872,6 +17960,11 @@ "node": ">= 0.8" } }, + "node_modules/uid2": { + "version": "0.0.4", + "resolved": "https://registry.npmjs.org/uid2/-/uid2-0.0.4.tgz", + "integrity": "sha512-IevTus0SbGwQzYh3+fRsAMTVVPOoIVufzacXcHPmdlle1jUpq7BRL+mw3dgeLanvGZdwwbWhRV6XrcFNdBmjWA==" + }, "node_modules/unbzip2-stream": { "version": "1.4.3", "resolved": "https://registry.npmjs.org/unbzip2-stream/-/unbzip2-stream-1.4.3.tgz", @@ -21960,9 +22053,9 @@ } }, "@tiledesk/tiledesk-tybot-connector": { - "version": "0.2.138", - "resolved": "https://registry.npmjs.org/@tiledesk/tiledesk-tybot-connector/-/tiledesk-tybot-connector-0.2.138.tgz", - "integrity": "sha512-VDIFqCKTI/5WUSEbnv+Y+jcMBi8dza7FsWv0eXv6ZoYjmm3gkTIkC2q5JvSZILbZcra9+X7K6OkpQViL9t8PiQ==", + "version": "0.2.139", + "resolved": "https://registry.npmjs.org/@tiledesk/tiledesk-tybot-connector/-/tiledesk-tybot-connector-0.2.139.tgz", + "integrity": "sha512-ux4+AZFjKRu9OgmEEfF8F9RgeEYxLo9Jzj/71ENY3R2Sj1t2Fz6tbYXZX0f14ZOl4u920FKQvbWQmYGuuOjM5Q==", "requires": { "@tiledesk/helpcenter-query-client": "^0.1.8", "@tiledesk/tiledesk-chatbot-client": "^0.5.30", @@ -21982,11 +22075,39 @@ "mongoose": "^6.3.5", "multer": "^1.4.5-lts.1", "nanoid": "^3.1.25", - "redis": "^3.1.2", + "redis": "^4.7.0", "uuid": "^3.3.3", "vm2": "^3.9.13" }, "dependencies": { + "@redis/client": { + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/@redis/client/-/client-1.6.0.tgz", + "integrity": "sha512-aR0uffYI700OEEH4gYnitAnv3vzVGXCFvYfdpu/CJKvk4pHfLPEy/JSZyrpQ+15WhXe1yJRXLtfQ84s4mEXnPg==", + "requires": { + "cluster-key-slot": "1.1.2", + "generic-pool": "3.9.0", + "yallist": "4.0.0" + } + }, + "@redis/json": { + "version": "1.0.7", + "resolved": "https://registry.npmjs.org/@redis/json/-/json-1.0.7.tgz", + "integrity": "sha512-6UyXfjVaTBTJtKNG4/9Z8PSpKE6XgSyEb8iwaqDcy+uKrd/DGYHTWkUdnQDyzm727V7p21WUMhsqz5oy65kPcQ==", + "requires": {} + }, + "@redis/search": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/@redis/search/-/search-1.2.0.tgz", + "integrity": "sha512-tYoDBbtqOVigEDMAcTGsRlMycIIjwMCgD8eR2t0NANeQmgK/lvxNAvYyb6bZDD4frHRhIHkJu2TBRvB0ERkOmw==", + "requires": {} + }, + "@redis/time-series": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/@redis/time-series/-/time-series-1.1.0.tgz", + "integrity": "sha512-c1Q99M5ljsIuc4YdaCwfUEXsofakb9c8+Zse2qxTadu8TalLXuAESzLvFAvNVbkmSlvlzIQOLpBCmWI9wTOt+g==", + "requires": {} + }, "axios": { "version": "1.7.7", "resolved": "https://registry.npmjs.org/axios/-/axios-1.7.7.tgz", @@ -22086,6 +22207,19 @@ "xtend": "^4.0.0" } }, + "redis": { + "version": "4.7.0", + "resolved": "https://registry.npmjs.org/redis/-/redis-4.7.0.tgz", + "integrity": "sha512-zvmkHEAdGMn+hMRXuMBtu4Vo5P6rHQjLoHftu+lBqq8ZTA3RCVC/WzD790bkKKiNFp7d5/9PcSD19fJyyRvOdQ==", + "requires": { + "@redis/bloom": "1.2.0", + "@redis/client": "1.6.0", + "@redis/graph": "1.1.1", + "@redis/json": "1.0.7", + "@redis/search": "1.2.0", + "@redis/time-series": "1.1.0" + } + }, "sift": { "version": "16.0.1", "resolved": "https://registry.npmjs.org/sift/-/sift-16.0.1.tgz", @@ -22095,6 +22229,11 @@ "version": "1.1.0", "resolved": "https://registry.npmjs.org/streamsearch/-/streamsearch-1.1.0.tgz", "integrity": "sha512-Mcc5wHehp9aXz1ax6bZUyY5afg9u2rv5cqQI3mRrYkGC8rW2hM02jWuwjtL++LS5qinSyhj2QfLyNsuc+VsExg==" + }, + "yallist": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz", + "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==" } } }, @@ -22591,9 +22730,9 @@ } }, "@tiledesk/tiledesk-whatsapp-connector": { - "version": "0.1.75", - "resolved": "https://registry.npmjs.org/@tiledesk/tiledesk-whatsapp-connector/-/tiledesk-whatsapp-connector-0.1.75.tgz", - "integrity": "sha512-cBLPcJvUDhhnXi6qKQBDtHewVAOQd8yTp0Js4BZMajv1lmy710GeMMc3EUfLLbqIvmmMdJVc1Iu5Rs6OuFIDWQ==", + "version": "0.1.76", + "resolved": "https://registry.npmjs.org/@tiledesk/tiledesk-whatsapp-connector/-/tiledesk-whatsapp-connector-0.1.76.tgz", + "integrity": "sha512-i86fSodEp5+In3EQADVGxk7y3XJYmH14ogGaNUZMkMnFUhdZ9E01JiKNO5dvHAARgX2IBfeQtLUlgEfvj045Xw==", "requires": { "@tiledesk/tiledesk-client": "^0.10.4", "app-root-path": "^3.0.0", @@ -22731,9 +22870,9 @@ } }, "@tiledesk/tiledesk-whatsapp-jobworker": { - "version": "0.0.10", - "resolved": "https://registry.npmjs.org/@tiledesk/tiledesk-whatsapp-jobworker/-/tiledesk-whatsapp-jobworker-0.0.10.tgz", - "integrity": "sha512-9Z+JqJND6jaSRMxO1hle4lL1vOD9iOA3lNPYVEmciLLv5xZGQwtxUn2gOD2qeUkncptmejgb4QsT5yu988HwdQ==", + "version": "0.0.11", + "resolved": "https://registry.npmjs.org/@tiledesk/tiledesk-whatsapp-jobworker/-/tiledesk-whatsapp-jobworker-0.0.11.tgz", + "integrity": "sha512-d3XKokv90U/UfzIB3f7MYEQhzysDlSvDkpOlEXakq16vd3TXVCfIniSG+uLd7bKE8ej8TEjiwgo3aW8Rg/0N/w==", "requires": { "@tiledesk/tiledesk-client": "^0.10.4", "@types/node": "^18.0.6", @@ -23802,6 +23941,11 @@ "resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.5.1.tgz", "integrity": "sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==" }, + "base64url": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/base64url/-/base64url-3.0.1.tgz", + "integrity": "sha512-ir1UPr3dkwexU7FdV8qBBbNDRUhMmIekYMFZfi+C/sLNnRESKPl23nB9b2pltqfOQNnGzsDdId90AEtG5tCx4A==" + }, "basic-auth": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/basic-auth/-/basic-auth-2.0.1.tgz", @@ -30852,6 +30996,25 @@ } } }, + "passport-oauth2": { + "version": "1.8.0", + "resolved": "https://registry.npmjs.org/passport-oauth2/-/passport-oauth2-1.8.0.tgz", + "integrity": "sha512-cjsQbOrXIDE4P8nNb3FQRCCmJJ/utnFKEz2NX209f7KOHPoX18gF7gBzBbLLsj2/je4KrgiwLLGjf0lm9rtTBA==", + "requires": { + "base64url": "3.x.x", + "oauth": "0.10.x", + "passport-strategy": "1.x.x", + "uid2": "0.0.x", + "utils-merge": "1.x.x" + }, + "dependencies": { + "oauth": { + "version": "0.10.0", + "resolved": "https://registry.npmjs.org/oauth/-/oauth-0.10.0.tgz", + "integrity": "sha512-1orQ9MT1vHFGQxhuy7E/0gECD3fd2fCC+PIX+/jgmU/gI3EpRocXtmtvxCO5x3WZ443FLTLFWNDjl5MPJf9u+Q==" + } + } + }, "passport-openidconnect": { "version": "0.1.1", "resolved": "https://registry.npmjs.org/passport-openidconnect/-/passport-openidconnect-0.1.1.tgz", @@ -33013,6 +33176,11 @@ "random-bytes": "~1.0.0" } }, + "uid2": { + "version": "0.0.4", + "resolved": "https://registry.npmjs.org/uid2/-/uid2-0.0.4.tgz", + "integrity": "sha512-IevTus0SbGwQzYh3+fRsAMTVVPOoIVufzacXcHPmdlle1jUpq7BRL+mw3dgeLanvGZdwwbWhRV6XrcFNdBmjWA==" + }, "unbzip2-stream": { "version": "1.4.3", "resolved": "https://registry.npmjs.org/unbzip2-stream/-/unbzip2-stream-1.4.3.tgz", diff --git a/package.json b/package.json index f9ec4e44a99..36d9ea94c85 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@tiledesk/tiledesk-server", "description": "The Tiledesk server module", - "version": "2.10.36", + "version": "2.10.40", "scripts": { "start": "node ./bin/www", "pretest": "mongodb-runner start", @@ -48,13 +48,14 @@ "@tiledesk/tiledesk-rasa-connector": "^1.0.10", "@tiledesk/tiledesk-telegram-connector": "^0.1.14", "@tiledesk/tiledesk-train-jobworker": "^0.0.11", - "@tiledesk/tiledesk-tybot-connector": "^0.2.138", - "@tiledesk/tiledesk-whatsapp-connector": "^0.1.75", - "@tiledesk/tiledesk-whatsapp-jobworker": "^0.0.10", + "@tiledesk/tiledesk-tybot-connector": "^0.2.139", + "@tiledesk/tiledesk-whatsapp-connector": "^0.1.76", + "@tiledesk/tiledesk-whatsapp-jobworker": "^0.0.11", "@tiledesk/tiledesk-sms-connector": "^0.1.11", "@tiledesk/tiledesk-vxml-connector": "^0.1.67", "@tiledesk/tiledesk-voice-twilio-connector": "^0.1.12", "@tiledesk/tiledesk-multi-worker": "^0.1.6", + "passport-oauth2": "^1.8.0", "amqplib": "^0.5.5", "app-root-path": "^3.0.0", "bcrypt-nodejs": "0.0.3", diff --git a/routes/auth.js b/routes/auth.js index 7249b9f129c..4d4895808e2 100644 --- a/routes/auth.js +++ b/routes/auth.js @@ -770,6 +770,97 @@ router.get("/google/callback", passport.authenticate("google", { session: false } ); + + + +router.get("/oauth2", function(req,res,next){ + winston.debug("redirect_url: "+ req.query.redirect_url ); + req.session.redirect_url = req.query.redirect_url; + + winston.debug("forced_redirect_url: "+ req.query.forced_redirect_url ); + req.session.forced_redirect_url = req.query.forced_redirect_url; + + passport.authenticate( + 'oauth2' + )(req,res,next); +}); + +// router.get('/oauth2', +// passport.authenticate('oauth2')); + + router.get('/oauth2/callback', + passport.authenticate('oauth2', { session: false}), + function(req, res) { + winston.debug("'/oauth2/callback: "); + + var user = req.user; + winston.debug("user", user); + winston.debug("req.session.redirect_url: "+ req.session.redirect_url); + + + var userJson = user.toObject(); + + delete userJson.password; + + + var signOptions = { + issuer: 'https://tiledesk.com', + subject: 'user', + audience: 'https://tiledesk.com', + jwtid: uuidv4() + + }; + + var alg = process.env.GLOBAL_SECRET_ALGORITHM; + if (alg) { + signOptions.algorithm = alg; + } + + + var token = jwt.sign(userJson, configSecret, signOptions); //priv_jwt pp_jwt + + + // return the information including token as JSON + // res.json(returnObject); + + let dashboard_base_url = process.env.EMAIL_BASEURL || config.baseUrl; + winston.debug("Google Redirect dashboard_base_url: ", dashboard_base_url); + + let homeurl = "/#/"; + + if (req.session.redirect_url) { + homeurl = req.session.redirect_url; + } + + var url = dashboard_base_url+homeurl+"?token=JWT "+token; + + if (req.session.forced_redirect_url) { + url = req.session.forced_redirect_url+"?jwt=JWT "+token; //attention we use jwt= (ionic) instead token=(dashboard) for ionic + } + + winston.debug("Google Redirect: "+ url); + + res.redirect(url); + + + + }); + +router.get( + "/keycloak", + passport.authenticate("keycloak") +); +router.get( + "/keycloak/callback", + passport.authenticate("keycloak"), + function(req, res) { + winston.info("'/keycloak/callback: "); + // Successful authentication, redirect home. + res.redirect('/'); + } +); + + // profile route after successful sign in // router.get("/profile", (req, res) => { // console.log(req); diff --git a/routes/faq_kb.js b/routes/faq_kb.js index 7d995f8b52c..376455f8a26 100644 --- a/routes/faq_kb.js +++ b/routes/faq_kb.js @@ -18,6 +18,7 @@ const uuidv4 = require('uuid/v4'); const trainingService = require('../services/trainingService'); var roleChecker = require('../middleware/has-role'); const roleConstants = require('../models/roleConstants'); +const errorCodes = require('../errorCodes'); let chatbot_templates_api_url = process.env.CHATBOT_TEMPLATES_API_URL @@ -244,7 +245,7 @@ router.put('/:faq_kbid/publish', roleChecker.hasRole('admin'), async (req, res) let forkedChatBotId = forked.bot_id; winston.debug("forkedChatBotId: "+forkedChatBotId); - let updatedForkedChabot = await Faq_kb.findByIdAndUpdate(forkedChatBotId, {trashed: true, publishedBy: req.user.id, publishedAt: new Date().getTime()}, { new: true, upsert: true }).exec(); + let updatedForkedChabot = await Faq_kb.findByIdAndUpdate(forkedChatBotId, { $unset: { slug: 1 }, trashed: true, publishedBy: req.user.id, publishedAt: new Date().getTime()}, { new: true, upsert: true }).exec(); winston.debug("updatedForkedChabot: ",updatedForkedChabot); botEvent.emit('faqbot.update', updatedForkedChabot); @@ -327,12 +328,20 @@ router.put('/:faq_kbid', roleChecker.hasRoleOrTypes('admin', ['bot','subscriptio if (req.body.agents_available != undefined) { update.agents_available = req.body.agents_available } + + if (req.body.slug != undefined) { + update.slug = req.body.slug; + } winston.debug("update", update); Faq_kb.findByIdAndUpdate(req.params.faq_kbid, update, { new: true, upsert: true }, function (err, updatedFaq_kb) { //TODO add cache_bot_here if (err) { - return res.status(500).send({ success: false, msg: 'Error updating object.' }); + if (err.code === 11000 && err.keyValue.slug) { + return res.status(500).send({ success: false, msg: 'Error updating object.', error: 'Slug already exists: ' + err.keyValue.slug, error_code: errorCodes.CHATBOT.ERRORS.DUPLICATE_SLUG }) + } else { + return res.status(500).send({ success: false, msg: 'Error updating object.' }); + } } botEvent.emit('faqbot.update', updatedFaq_kb); @@ -859,7 +868,6 @@ router.post('/importjson/:id_faq_kb', roleChecker.hasRole('admin'), upload.singl } else { winston.error("Error creating new intent: ", err); } - winston.error("Error creating new intent: ", err); }) if (faq) { diff --git a/routes/request.js b/routes/request.js index 5457434ac28..dde28382da9 100644 --- a/routes/request.js +++ b/routes/request.js @@ -32,6 +32,7 @@ const { check, validationResult } = require('express-validator'); const RoleConstants = require('../models/roleConstants'); const eventService = require('../pubmodules/events/eventService'); const { Scheduler } = require('../services/Scheduler'); +const faq_kb = require('../models/faq_kb'); //const JobManager = require('../utils/jobs-worker-queue-manager-v2/JobManagerV2'); // var messageService = require('../services/messageService'); @@ -436,6 +437,63 @@ router.put('/:requestid/participants', function (req, res) { }); +router.put('/:requestid/replace', async (req, res) => { + + let id; + let name; + let slug; + + if (req.body.id) { + id = "bot_" + req.body.id; + } else if (req.body.name) { + name = req.body.name; + } else if (req.body.slug) { + slug = req.body.slug; + } else { + return res.status(400).send({ success: false, error: "Missing field 'id' or 'name' in body" }) + } + + if (name) { + let chatbot = await faq_kb.findOne({ id_project: req.projectid, name: name }).catch((err) => { + winston.error("Error finding bot ", err); + return res.status(500).send({ success: false, error: "An error occurred getting chatbot with name " + name }) + }) + + if (!chatbot) { + return res.status(404).send({ success: false, error: "Chatbot with name '" + name + "' not found" }) + } + + id = "bot_" + chatbot._id; + winston.info("Chatbot found: ", id); + } + + if (slug) { + let chatbot = await faq_kb.findOne({ id_project: req.projectid, slug: slug}).catch((err) => { + winston.error("Error finding bot ", err); + return res.status(500).send({ success: false, error: "An error occurred getting chatbot with slug " + slug }) + }) + + if (!chatbot) { + return res.status(404).send({ success: false, error: "Chatbot with slug '" + slug + "' not found" }) + } + + id = "bot_" + chatbot._id; + winston.info("Chatbot found: ", id); + } + + let participants = []; + participants.push(id); + winston.info("participants to be set: ", participants); + + requestService.setParticipantsByRequestId(req.params.requestid, req.projectid, participants).then((updatedRequest) => { + winston.info("SetParticipant response: ", updatedRequest); + res.status(200).send(updatedRequest); + }).catch((err) => { + winston.error("Error setting participants ", err); + res.status(500).send({ success: false, error: "Error setting participants to request"}) + }) +}) + // TODO make a synchronous chat21 version (with query parameter?) with request.support_group.created router.delete('/:requestid/participants/:participantid', function (req, res) { winston.debug(req.body); diff --git a/test/faqkbRoute.js b/test/faqkbRoute.js index 5e235f04696..30292350464 100644 --- a/test/faqkbRoute.js +++ b/test/faqkbRoute.js @@ -1,6 +1,6 @@ //During the test the env variable is set to test process.env.NODE_ENV = 'test'; -process.env.LOG_LEVEL = 'critical'; +process.env.LOG_LEVEL = 'error'; var Faq = require('../models/faq'); var projectService = require('../services/projectService'); @@ -30,80 +30,7 @@ chai.use(chaiHttp); describe('FaqKBRoute', () => { - describe('/create', () => { - - it('create-new-chatbot', (done) => { - - var email = "test-signup-" + Date.now() + "@email.com"; - var pwd = "pwd"; - - userService.signup(email, pwd, "Test Firstname", "Test lastname").then(function (savedUser) { - projectService.create("test-faqkb-create", savedUser._id).then(function (savedProject) { - - chai.request(server) - .post('/' + savedProject._id + '/faq_kb') - .auth(email, pwd) - .send({ "name": "testbot", type: "external", language: 'fr' }) - .end((err, res) => { - - if (err) { console.error("err: ", err); } - if (log) { console.log("res.body", res.body); } - - res.should.have.status(200); - res.body.should.be.a('object'); - expect(res.body.name).to.equal("testbot"); - expect(res.body.language).to.equal("fr"); - - chai.request(server) - .get('/' + savedProject._id + '/faq_kb/' + res.body._id) - .auth(email, pwd) - .end((err, res) => { - - if (err) { console.error("err: ", err); } - if (log) { console.log("res.body", res.body); } - - res.should.have.status(200); - - done(); - - }); - }); - }); - }); - - }) - - it('create-new-chatbot-agent-role', (done) => { - - var email = "test-signup-" + Date.now() + "@email.com"; - var pwd = "pwd"; - - userService.signup(email, pwd, "Test Firstname", "Test lastname").then(function (savedUser) { - projectService.create("test-faqkb-create", savedUser._id).then(function (savedProject) { - Project_user.findOneAndUpdate({ id_project: savedProject._id, id_user: savedUser._id }, { role: roleConstants.AGENT }, (err, savedProject_user) => { - - chai.request(server) - .post('/' + savedProject._id + '/faq_kb') - .auth(email, pwd) - .send({ "name": "testbot", type: "external", language: 'fr' }) - .end((err, res) => { - - if (err) { console.error("err: ", err); } - if (log) { console.log("res.body", res.body); } - - res.should.have.status(403); - expect(res.body.success).to.equal(false); - expect(res.body.msg).to.equal("you dont have the required role."); - - done(); - - }); - }) - }); - }); - - }) - + describe('Get', () => { it('get-all-chatbot-with-role-admin-or-owner', (done) => { @@ -172,12 +99,12 @@ describe('FaqKBRoute', () => { .get('/' + savedProject._id + '/faq_kb') .auth(email, pwd) .end((err, res) => { - + if (err) { console.error("err: ", err); } if (log) { console.log("res.body", res.body); } res.should.have.status(200); - + done(); }); }) @@ -190,123 +117,171 @@ describe('FaqKBRoute', () => { }).timeout(20000); - /** - * This test will be no longer available after merge with master because - * the profile section can no longer be modified via api. - */ - // it('createMaximumNumberExceeded', (done) => { - - // var email = "test-signup-" + Date.now() + "@email.com"; - // var pwd = "pwd"; - - // userService.signup(email, pwd, "Test Firstname", "Test lastname").then(function (savedUser) { - // projectService.create("test-faqkb-create", savedUser._id).then(function (savedProject) { - - // chai.request(server) - // .put('/projects/' + savedProject._id) - // .auth(email, pwd) - // .send({ profile: { quotes: { chatbots: 2 } } }) - // .end((err, res) => { - - // if (log) { console.log("res.body", res.body); } - - // chai.request(server) - // .post('/' + savedProject._id + '/faq_kb') - // .auth(email, pwd) - // .send({ "name": "testbot1", type: "external", language: 'en' }) - // .end((err, res) => { - // if (log) { console.log("res.body", res.body); } - // res.should.have.status(200); - // res.body.should.be.a('object'); - // expect(res.body.name).to.equal("testbot1"); - // expect(res.body.language).to.equal("en"); - - // chai.request(server) - // .post('/' + savedProject._id + '/faq_kb') - // .auth(email, pwd) - // .send({ "name": "testbot2", type: "external", language: 'en' }) - // .end((err, res) => { - // if (log) { console.log("res.body", res.body); } - // res.should.have.status(200); - // res.body.should.be.a('object'); - // expect(res.body.name).to.equal("testbot2"); - // expect(res.body.language).to.equal("en"); - - // chai.request(server) - // .post('/' + savedProject._id + '/faq_kb') - // .auth(email, pwd) - // .send({ "name": "testbot3", type: "external", language: 'en' }) - // .end((err, res) => { - - // if (log) { console.log("res.body", res.body); } - - // res.should.have.status(403); - // res.body.should.be.a('object'); - // expect(res.body.success).to.equal(false); - // expect(res.body.error).to.equal("Maximum number of chatbots reached for the current plan"); - // expect(res.body.plan_limit).to.equal(2); - - // done() - - // }); - // }); - // }); - // }) - // }); - // }); - - // }).timeout(20000); - - - // it('train with tiledesk-ai', (done) => { - // var email = "test-signup-" + Date.now() + "@email.com"; - // var pwd = "pwd"; - - // userService.signup(email, pwd, "Test Firstname", "Test Lastname").then((savedUser) => { - // projectService.create("test-faqkb-create", savedUser._id).then((savedProject) => { - - // chai.request(server) - // .post('/' + savedProject._id + '/faq_kb') - // .auth(email, pwd) - // .send({ "name": "testbot", type: "internal", template: "example", intentsEngine: "tiledesk-ai" }) - // .end((err, res) => { - // if (log) { - // console.log("res.body", res.body); - // } - // res.should.have.status(200); - // res.body.should.be.a('object'); - // expect(res.body.name).to.equal("testbot"); - // var id_faq_kb = res.body._id; - - // chai.request(server) - // .get('/' + savedProject._id + '/faq?id_faq_kb=' + id_faq_kb) - // .auth(email, pwd) - // .end((err, res) => { - // if (log) { console.log("faq_list: ", res.body); } - // res.should.have.status(200); - // res.body.should.be.an('array').that.is.not.empty; - - // chai.request(server) - // .post('/' + savedProject._id + '/faq_kb/aitrain') - // .auth(email, pwd) - // .send({ id_faq_kb: id_faq_kb, webhook_enabled: false }) - // .end((err, res) => { - // if (log) { console.log("train res.body: ", res.body); } - - // done(); - // }) - - // }) - - - - // }); - // }) - // }) - // }) - - - it('create with template example', (done) => { + }) + + describe('Create', () => { + + it('create-new-chatbot', (done) => { + + var email = "test-signup-" + Date.now() + "@email.com"; + var pwd = "pwd"; + + userService.signup(email, pwd, "Test Firstname", "Test lastname").then(function (savedUser) { + projectService.create("test-faqkb-create", savedUser._id).then(function (savedProject) { + + chai.request(server) + .post('/' + savedProject._id + '/faq_kb') + .auth(email, pwd) + .send({ "name": "testbot", type: "external", language: 'en' }) + .end((err, res) => { + + if (err) { console.error("err: ", err); } + if (log) { console.log("res.body", res.body); } + + res.should.have.status(200); + res.body.should.be.a('object'); + expect(res.body.name).to.equal("testbot"); + expect(res.body.language).to.equal("en"); + + chai.request(server) + .get('/' + savedProject._id + '/faq_kb/' + res.body._id) + .auth(email, pwd) + .end((err, res) => { + + if (err) { console.error("err: ", err); } + if (log) { console.log("res.body", res.body); } + + res.should.have.status(200); + + done(); + + }); + }); + }); + }); + + }) + + it('create-new-chatbot-auto-slug', (done) => { + + var email = "test-signup-" + Date.now() + "@email.com"; + var pwd = "pwd"; + + userService.signup(email, pwd, "Test Firstname", "Test lastname").then(function (savedUser) { + projectService.create("test-faqkb-create", savedUser._id).then(function (savedProject) { + + chai.request(server) + .post('/' + savedProject._id + '/faq_kb') + .auth(email, pwd) + .send({ "name": "My Awesome Bot", type: "internal", language: 'en', template: "blank" }) + .end((err, res) => { + + if (err) { console.error("err: ", err); } + if (log) { console.log("res.body", res.body); } + + res.should.have.status(200); + res.body.should.be.a('object'); + expect(res.body.name).to.equal("My Awesome Bot"); + expect(res.body.slug).to.equal("my-awesome-bot") + expect(res.body.language).to.equal("en"); + + chai.request(server) + .post('/' + savedProject._id + '/faq_kb') + .auth(email, pwd) + .send({ "name": "My Awesome Bot", type: "internal", language: 'en', template: "blank" }) + .end((err, res) => { + + if (err) { console.error("err: ", err); } + if (log) { console.log("res.body", res.body); } + + res.should.have.status(200); + res.body.should.be.a('object'); + expect(res.body.name).to.equal("My Awesome Bot"); + expect(res.body.slug).to.equal("my-awesome-bot-1") + expect(res.body.language).to.equal("en"); + + chai.request(server) + .post('/' + savedProject._id + '/faq_kb') + .auth(email, pwd) + .send({ "name": "My Awesome Bot", type: "internal", language: 'en', template: "blank" }) + .end((err, res) => { + if (err) { console.error("err: ", err); } + if (log) { console.log("res.body", res.body); } + + res.should.have.status(200); + res.body.should.be.a('object'); + expect(res.body.name).to.equal("My Awesome Bot"); + expect(res.body.slug).to.equal("my-awesome-bot-2") + expect(res.body.language).to.equal("en"); + + chai.request(server) + .post('/' + savedProject._id + '/faq_kb') + .auth(email, pwd) + .send({ "name": "My Awesome Bot 1", type: "internal", language: 'en', template: "blank" }) + .end((err, res) => { + + if (err) { console.error("err: ", err); } + if (log) { console.log("res.body", res.body); } + + res.should.have.status(200); + res.body.should.be.a('object'); + expect(res.body.name).to.equal("My Awesome Bot 1"); + expect(res.body.slug).to.equal("my-awesome-bot-1-1") + expect(res.body.language).to.equal("en"); + + chai.request(server) + .get('/' + savedProject._id + '/faq_kb/') + .auth(email, pwd) + .end((err, res) => { + + if (err) { console.error("err: ", err); } + if (log) { console.log("res.body", res.body); } + + res.should.have.status(200); + + done(); + + }); + }) + }) + }) + }); + }); + }); + }) + + it('create-new-chatbot-agent-role', (done) => { + + var email = "test-signup-" + Date.now() + "@email.com"; + var pwd = "pwd"; + + userService.signup(email, pwd, "Test Firstname", "Test lastname").then(function (savedUser) { + projectService.create("test-faqkb-create", savedUser._id).then(function (savedProject) { + Project_user.findOneAndUpdate({ id_project: savedProject._id, id_user: savedUser._id }, { role: roleConstants.AGENT }, (err, savedProject_user) => { + + chai.request(server) + .post('/' + savedProject._id + '/faq_kb') + .auth(email, pwd) + .send({ "name": "testbot", type: "external", language: 'fr' }) + .end((err, res) => { + + if (err) { console.error("err: ", err); } + if (log) { console.log("res.body", res.body); } + + res.should.have.status(403); + expect(res.body.success).to.equal(false); + expect(res.body.msg).to.equal("you dont have the required role."); + + done(); + + }); + }) + }); + }); + + }) + + it('create-with-template-example', (done) => { var email = "test-signup-" + Date.now() + "@email.com"; var pwd = "pwd"; @@ -319,10 +294,10 @@ describe('FaqKBRoute', () => { .auth(email, pwd) .send({ "name": "testbot", type: "internal", template: "example" }) .end((err, res) => { - + if (err) { console.error("err: ", err); } if (log) { console.log("res.body", res.body); } - + res.should.have.status(200); res.body.should.be.a('object'); expect(res.body.name).to.equal("testbot"); @@ -332,7 +307,7 @@ describe('FaqKBRoute', () => { .get('/' + savedProject._id + '/faq?id_faq_kb=' + id_faq_kb) .auth(email, pwd) .end((err, res) => { - + if (err) { console.error("err: ", err); } if (log) { console.log("res.body", res.body); } @@ -350,7 +325,7 @@ describe('FaqKBRoute', () => { }) }) - it('create with template blank', (done) => { + it('create-with-template-blank', (done) => { var email = "test-signup-" + Date.now() + "@email.com"; var pwd = "pwd"; @@ -363,7 +338,7 @@ describe('FaqKBRoute', () => { .auth(email, pwd) .send({ "name": "testbot", type: "internal", template: "blank" }) .end((err, res) => { - + if (err) { console.error("err: ", err); } if (log) { console.log("res.body", res.body); } @@ -376,10 +351,10 @@ describe('FaqKBRoute', () => { .get('/' + savedProject._id + '/faq?id_faq_kb=' + id_faq_kb) .auth(email, pwd) .end((err, res) => { - + if (err) { console.error("err: ", err); } if (log) { console.log("faq_list: ", JSON.stringify(res.body, null, 2)); } - + res.should.have.status(200); res.body.should.be.an('array').that.is.not.empty; @@ -394,8 +369,125 @@ describe('FaqKBRoute', () => { }) }) - // mocha test/faqkbRoute.js --grep 'language' - it('update chatbot and intents language', (done) => { + }); + + + describe('Update', () => { + + it('update-chatbot-no-slug', (done) => { + + var email = "test-signup-" + Date.now() + "@email.com"; + var pwd = "pwd"; + + userService.signup(email, pwd, "Test Firstname", "Test lastname").then(function (savedUser) { + projectService.create("test-faqkb-create", savedUser._id).then(function (savedProject) { + + chai.request(server) + .post('/' + savedProject._id + '/faq_kb') + .auth(email, pwd) + .send({ "name": "My Awesome Bot", type: "internal", language: 'en', template: "blank" }) + .end((err, res) => { + + if (err) { console.error("err: ", err); } + if (log) { console.log("res.body", res.body); } + + res.should.have.status(200); + res.body.should.be.a('object'); + expect(res.body.name).to.equal("My Awesome Bot"); + expect(res.body.slug).to.equal("my-awesome-bot") + expect(res.body.language).to.equal("en"); + + let id_faq_kb = res.body._id; + + chai.request(server) + .put('/' + savedProject._id + '/faq_kb/' + id_faq_kb) + .auth(email, pwd) + .send({ "name": "My Magician Bot" }) + .end((err, res) => { + + if (err) { console.error("err: ", err); } + if (log) { console.log("res.body", res.body); } + + res.should.have.status(200); + res.body.should.be.a('object'); + expect(res.body.name).to.equal("My Magician Bot"); + expect(res.body.slug).to.equal("my-awesome-bot"); + + done() + }) + + }); + }); + }); + }) + + it('update-chatbot-slug-with-existing-one', (done) => { + + var email = "test-signup-" + Date.now() + "@email.com"; + var pwd = "pwd"; + + userService.signup(email, pwd, "Test Firstname", "Test lastname").then(function (savedUser) { + projectService.create("test-faqkb-create", savedUser._id).then(function (savedProject) { + + chai.request(server) + .post('/' + savedProject._id + '/faq_kb') + .auth(email, pwd) + .send({ "name": "My Awesome Bot", type: "internal", language: 'en', template: "blank" }) + .end((err, res) => { + + if (err) { console.error("err: ", err); } + if (log) { console.log("res.body", res.body); } + + res.should.have.status(200); + res.body.should.be.a('object'); + expect(res.body.name).to.equal("My Awesome Bot"); + expect(res.body.slug).to.equal("my-awesome-bot") + expect(res.body.language).to.equal("en"); + + chai.request(server) + .post('/' + savedProject._id + '/faq_kb') + .auth(email, pwd) + .send({ "name": "My Awesome Bot", type: "internal", language: 'en', template: "blank" }) + .end((err, res) => { + + if (err) { console.error("err: ", err); } + if (log) { console.log("res.body", res.body); } + + res.should.have.status(200); + res.body.should.be.a('object'); + expect(res.body.name).to.equal("My Awesome Bot"); + expect(res.body.slug).to.equal("my-awesome-bot-1") + expect(res.body.language).to.equal("en"); + + let id_faq_kb = res.body._id; + + chai.request(server) + .put('/' + savedProject._id + '/faq_kb/' + id_faq_kb) + .auth(email, pwd) + .send({ "slug": "my-awesome-bot" }) + .end((err, res) => { + + if (err) { console.error("err: ", err); } + if (log) { console.log("res.body", res.body); } + + res.should.have.status(500); + res.body.should.be.a('object'); + expect(res.body.success).to.equal(false); + expect(res.body.error).to.equal("Slug already exists: my-awesome-bot"); + expect(res.body.error_code).to.equal(12001); + + done() + + }) + + }); + + }); + }); + }); + }) + + it('update-chatbot-and-intents-language', (done) => { var email = "test-signup-" + Date.now() + "@email.com"; var pwd = "pwd"; @@ -408,7 +500,7 @@ describe('FaqKBRoute', () => { .auth(email, pwd) .send({ "name": "testbot", type: "internal", template: "example", language: "en" }) .end((err, res) => { - + if (err) { console.error("err: ", err); } if (log) { console.log("res.body", res.body); } @@ -421,10 +513,10 @@ describe('FaqKBRoute', () => { .get('/' + savedProject._id + '/faq?id_faq_kb=' + id_faq_kb) .auth(email, pwd) .end((err, res) => { - + if (err) { console.error("err: ", err); } if (log) { console.log("res.body", res.body); } - + res.should.have.status(200); res.body.should.be.an('array').that.is.not.empty; @@ -433,7 +525,7 @@ describe('FaqKBRoute', () => { .put('/' + savedProject._id + '/faq_kb/' + id_faq_kb + '/language/it') .auth(email, pwd) .end((err, res) => { - + if (err) { console.error("err: ", err); } if (log) { console.log("res.body", res.body); } @@ -446,16 +538,16 @@ describe('FaqKBRoute', () => { .get('/' + savedProject._id + '/faq?id_faq_kb=' + id_faq_kb) .auth(email, pwd) .end((err, res) => { - + if (err) { console.error("err: ", err); } if (log) { console.log("res.body", res.body); } res.should.have.status(200); res.body.should.be.an('array').that.is.not.empty; - + done(); }) - + }) }) @@ -467,8 +559,11 @@ describe('FaqKBRoute', () => { }) }) + }) + + describe('Import/Export and Fork', () => { - it('fork chatbot (private)', (done) => { + it('fork-chatbot-private', (done) => { var email = "test-signup-" + Date.now() + "@email.com"; var pwd = "pwd"; @@ -499,7 +594,7 @@ describe('FaqKBRoute', () => { .auth(email, pwd) .send({ "name": "privateBot", type: "internal", language: 'en', public: "false", template: "blank" }) .end((err, res) => { - + if (err) { console.error("err: ", err); } if (log) { console.log("res.body", res.body); } @@ -514,7 +609,7 @@ describe('FaqKBRoute', () => { .auth(email, pwd) .set('Content-Type', 'application/json') .end((err, res) => { - + if (err) { console.error("err: ", err); } if (log) { console.log("res.body", res.body); } res.should.have.status(200); @@ -532,8 +627,7 @@ describe('FaqKBRoute', () => { }) - - it('fork chatbot (public)', (done) => { + it('fork-chatbot-public', (done) => { var email_user1 = "user1-signup-" + Date.now() + "@email.com"; var email_user2 = "user2-signup-" + Date.now() + "@email.com"; var pwd = "pwd"; @@ -566,7 +660,7 @@ describe('FaqKBRoute', () => { .auth(email_user1, pwd) .send({ "name": "publicBot", type: "internal", language: 'en', public: "true", template: "blank" }) .end((err, res) => { - + if (err) { console.error("err: ", err); } if (log) { console.log("res.body", res.body); } @@ -581,10 +675,10 @@ describe('FaqKBRoute', () => { .auth(email_user2, pwd) .set('Content-Type', 'application/json') .end((err, res) => { - + if (err) { console.error("err: ", err); } if (log) { console.log("res.body", res.body); } - + res.should.have.status(200); done(); @@ -596,7 +690,7 @@ describe('FaqKBRoute', () => { }) }) - it('create bot and import json', (done) => { + it('create-bot-and-import-json', (done) => { var email = "test-signup-" + Date.now() + "@email.com"; var pwd = "pwd"; @@ -610,7 +704,7 @@ describe('FaqKBRoute', () => { .set('Content-Type', 'text/plain') .attach('uploadFile', fs.readFileSync(path.resolve(__dirname, './example-json-rules.txt')), 'example-json-rules') .end((err, res) => { - + if (err) { console.error("err: ", err); } if (log) { console.log("res.body", res.body); } @@ -625,7 +719,7 @@ describe('FaqKBRoute', () => { .get('/' + savedProject._id + '/faq?id_faq_kb=' + id_faq_kb) .auth(email, pwd) .end((err, res) => { - + if (err) { console.error("err: ", err); } if (log) { console.log("res.body", res.body); } @@ -642,7 +736,7 @@ describe('FaqKBRoute', () => { }) - it('import json in an existing bot', (done) => { + it('import-json-in-an-existing-bot', (done) => { var email = "test-signup-" + Date.now() + "@email.com"; var pwd = "pwd"; @@ -655,7 +749,7 @@ describe('FaqKBRoute', () => { .auth(email, pwd) .send({ "name": "testbot", type: "tilebot", language: "en", template: "blank " }) .end((err, res) => { - + if (err) { console.error("err: ", err); } if (log) { console.log("res.body", res.body); } @@ -674,7 +768,7 @@ describe('FaqKBRoute', () => { if (err) { console.error("err: ", err); } if (log) { console.log("import json res: ", JSON.stringify(res.body, null, 2)); } - + res.should.have.status(200); //res.should.be.a('object'); //expect(res.body.name).to.equal("examplebot"); @@ -687,8 +781,7 @@ describe('FaqKBRoute', () => { })) }) - - it('import json in an existing bot and replace all intents', (done) => { + it('import-json-in-an-existing-bot-and-replace-all-intents', (done) => { var email = "test-signup-" + Date.now() + "@email.com"; var pwd = "pwd"; @@ -701,7 +794,7 @@ describe('FaqKBRoute', () => { .auth(email, pwd) .send({ "name": "testbot", type: "tilebot", language: "en", template: "blank " }) .end((err, res) => { - + if (err) { console.error("err: ", err); } if (log) { console.log("res.body", res.body); } @@ -716,8 +809,8 @@ describe('FaqKBRoute', () => { .auth(email, pwd) .end((err, res) => { - if (err) { console.error("err: ", err )}; - if (log) { console.log("res.body: ", res.body )}; + if (err) { console.error("err: ", err) }; + if (log) { console.log("res.body: ", res.body) }; res.should.have.status(200) res.body.should.be.a('array'); @@ -737,7 +830,7 @@ describe('FaqKBRoute', () => { //res.should.be.a('object'); //expect(res.body.name).to.equal("examplebot"); //expect(res.body.language).to.equal("en"); - + done(); }) }) @@ -747,8 +840,7 @@ describe('FaqKBRoute', () => { })) }) - // DEPRECATED - it('import-json-overwrite-true', (done) => { + it('import-json', (done) => { var email = "test-signup-" + Date.now() + "@email.com"; var pwd = "pwd"; @@ -756,15 +848,14 @@ describe('FaqKBRoute', () => { userService.signup(email, pwd, "Test Firstname", "Test lastname").then(function (savedUser) { projectService.create("test-faqkb-create", savedUser._id).then(function (savedProject) { - chai.request(server) .post('/' + savedProject._id + '/faq_kb') .auth(email, pwd) .send({ "name": "testbot", type: "internal", language: 'fr', template: "blank" }) .end((err, res) => { - - if (err) { console.error("err: ", err )}; - if (log) { console.log("res.body: ", res.body )}; + + if (err) { console.error("err: ", err) }; + if (log) { console.log("res.body: ", res.body) }; res.should.have.status(200); res.body.should.be.a('object'); @@ -773,14 +864,14 @@ describe('FaqKBRoute', () => { let id_faq_kb = res.body._id; chai.request(server) - .post('/' + savedProject._id + '/faq_kb/importjson/' + id_faq_kb + "?overwrite=true") + .post('/' + savedProject._id + '/faq_kb/importjson/' + id_faq_kb) .auth(email, pwd) .set('Content-Type', 'text/plain') - .attach('uploadFile', fs.readFileSync(path.resolve(__dirname, './example.json')), 'example.json') + .attach('uploadFile', fs.readFileSync(path.resolve(__dirname, './example-json.txt')), 'example-json.txt') .end((err, res) => { - - if (err) { console.error("err: ", err )}; - if (log) { console.log("res.body: ", res.body )}; + + if (err) { console.error("err: ", err) }; + if (log) { console.log("res.body: ", res.body) }; res.should.have.status(200); res.should.be.a('object'); @@ -792,8 +883,8 @@ describe('FaqKBRoute', () => { .auth(email, pwd) .end((err, res) => { - if (err) { console.error("err: ", err )}; - if (log) { console.log("res.body: ", res.body )}; + if (err) { console.error("err: ", err) }; + if (log) { console.log("res.body: ", res.body) }; res.should.have.status(200); res.body.should.be.an('array').that.is.not.empty; @@ -807,8 +898,7 @@ describe('FaqKBRoute', () => { }) }) - - it('import json', (done) => { + it('export-json', (done) => { var email = "test-signup-" + Date.now() + "@email.com"; var pwd = "pwd"; @@ -819,274 +909,45 @@ describe('FaqKBRoute', () => { chai.request(server) .post('/' + savedProject._id + '/faq_kb') .auth(email, pwd) - .send({ "name": "testbot", type: "internal", language: 'fr', template: "blank" }) + .send({ "name": "testbot", type: "internal", template: "example", language: 'fr' }) .end((err, res) => { - - if (err) { console.error("err: ", err )}; - if (log) { console.log("res.body: ", res.body )}; - + + if (err) { console.error("err: ", err) }; + if (log) { console.log("res.body: ", res.body) }; + res.should.have.status(200); res.body.should.be.a('object'); expect(res.body.name).to.equal("testbot"); expect(res.body.language).to.equal("fr"); let id_faq_kb = res.body._id; + if (log) { console.log("id_faq_kb: ", id_faq_kb); } + chai.request(server) - .post('/' + savedProject._id + '/faq_kb/importjson/' + id_faq_kb) + .get('/' + savedProject._id + '/faq?id_faq_kb=' + id_faq_kb) .auth(email, pwd) - .set('Content-Type', 'text/plain') - .attach('uploadFile', fs.readFileSync(path.resolve(__dirname, './example-json.txt')), 'example-json.txt') .end((err, res) => { - - if (err) { console.error("err: ", err )}; - if (log) { console.log("res.body: ", res.body )}; + + if (err) { console.error("err: ", err) }; + if (log) { console.log("res.body: ", res.body) }; res.should.have.status(200); - res.should.be.a('object'); - expect(res.body.name).to.equal("examplebot"); - expect(res.body.language).to.equal("en"); + res.body.should.be.an('array').that.is.not.empty; chai.request(server) - .get('/' + savedProject._id + '/faq?id_faq_kb=' + id_faq_kb) + .get('/' + savedProject._id + '/faq_kb/exportjson/' + id_faq_kb) .auth(email, pwd) .end((err, res) => { - - if (err) { console.error("err: ", err )}; - if (log) { console.log("res.body: ", res.body )}; + + if (err) { console.error("err: ", err) }; + if (log) { console.log("res.body: ", res.body) }; res.should.have.status(200); - res.body.should.be.an('array').that.is.not.empty; + //res.body.should.be.a('string'); done(); - }) }) - }) - }) - }) - }) - - // DEPRECATED - // it('import json (simple)', (done) => { - - // var email = "test-signup-" + Date.now() + "@email.com"; - // var pwd = "pwd"; - - // userService.signup(email, pwd, "Test Firstname", "Test lastname").then(function (savedUser) { - // projectService.create("test-faqkb-create", savedUser._id).then(function (savedProject) { - - // chai.request(server) - // .post('/' + savedProject._id + '/faq_kb') - // .auth(email, pwd) - // .send({ "name": "testbot", type: "internal", language: 'fr' }) - // .end((err, res) => { - // if (log) { - // console.log("res.body: ", res.body); - // } - // res.should.have.status(200); - // res.body.should.be.a('object'); - // expect(res.body.name).to.equal("testbot"); - // expect(res.body.language).to.equal("fr"); - // let id_faq_kb = res.body._id; - - // chai.request(server) - // .post('/' + savedProject._id + '/faq_kb/importjson/' + id_faq_kb + '?intentsOnly=true&overwrite=true') - // .auth(email, pwd) - // .set('Content-Type', 'text/plain') - // .attach('uploadFile', fs.readFileSync(path.resolve(__dirname, './example-json-intents.txt')), 'example-json-intents.txt') - // .end((err, res) => { - // if (log) { - // console.log("import (intents only) json res: ", res.body); - // } - // res.should.have.status(200); - // //res.should.be.a('object'); - // //expect(res.body.success).to.equal(true); - - // chai.request(server) - // .get('/' + savedProject._id + '/faq?id_faq_kb=' + id_faq_kb) - // .auth(email, pwd) - // .end((err, res) => { - // if (log) { - // console.log("faq_list: ", res.body); - // } - // res.should.have.status(200); - // res.body.should.be.an('array').that.is.not.empty; - - // done(); - - // }) - // }) - // }) - // }) - // }) - // }) - - - // DEPRECATED - // it('import json (intents only) (overwrite true)', (done) => { - - // var email = "test-signup-" + Date.now() + "@email.com"; - // var pwd = "pwd"; - - // userService.signup(email, pwd, "Test Firstname", "Test lastname").then(function (savedUser) { - // projectService.create("test-faqkb-create", savedUser._id).then(function (savedProject) { - - // chai.request(server) - // .post('/' + savedProject._id + '/faq_kb') - // .auth(email, pwd) - // .send({ "name": "testbot", type: "internal", language: 'fr', template: 'blank' }) - // .end((err, res) => { - // if (log) { - // console.log("res.body: ", res.body); - // } - // res.should.have.status(200); - // res.body.should.be.a('object'); - // expect(res.body.name).to.equal("testbot"); - // expect(res.body.language).to.equal("fr"); - // let id_faq_kb = res.body._id; - - // chai.request(server) - // .post('/' + savedProject._id + '/faq_kb/importjson/' + id_faq_kb + '?intentsOnly=true&overwrite=true') - // .auth(email, pwd) - // .set('Content-Type', 'text/plain') - // .attach('uploadFile', fs.readFileSync(path.resolve(__dirname, './example-json-intents.txt')), 'example-json-intents.txt') - // .end((err, res) => { - // if (log) { - // console.log("import (intents only) json res: ", res.body); - // } - // res.should.have.status(200); - // //res.should.be.a('object'); - // //expect(res.body.success).to.equal(true); - - // chai.request(server) - // .get('/' + savedProject._id + '/faq?id_faq_kb=' + id_faq_kb) - // .auth(email, pwd) - // .end((err, res) => { - // if (log) { - // console.log("faq_list: ", res.body); - // } - // res.should.have.status(200); - // res.body.should.be.an('array').that.is.not.empty; - - // done(); - - // }) - // }) - // }) - // }) - // }) - // }) - - - // DEPRECATED - // it('import json (intents only) (overwrite false)', (done) => { - - // var email = "test-signup-" + Date.now() + "@email.com"; - // var pwd = "pwd"; - - // userService.signup(email, pwd, "Test Firstname", "Test lastname").then(function (savedUser) { - // projectService.create("test-faqkb-create", savedUser._id).then(function (savedProject) { - - // chai.request(server) - // .post('/' + savedProject._id + '/faq_kb') - // .auth(email, pwd) - // .send({ "name": "testbot", type: "internal", language: 'fr', template: 'blank' }) - // .end((err, res) => { - // if (log) { - // console.log("res.body: ", res.body); - // } - // res.should.have.status(200); - // res.body.should.be.a('object'); - // expect(res.body.name).to.equal("testbot"); - // expect(res.body.language).to.equal("fr"); - // let id_faq_kb = res.body._id; - - // chai.request(server) - // .post('/' + savedProject._id + '/faq_kb/importjson/' + id_faq_kb + '?intentsOnly=true') - // .auth(email, pwd) - // .set('Content-Type', 'text/plain') - // .attach('uploadFile', fs.readFileSync(path.resolve(__dirname, './example-json-intents.txt')), 'example-json-intents.txt') - // .end((err, res) => { - // if (log) { - // console.log("import (intents only) json res: ", res.body); - // } - // res.should.have.status(200); - // //res.should.be.a('object'); - // //expect(res.body.success).to.equal(true); - - // chai.request(server) - // .get('/' + savedProject._id + '/faq?id_faq_kb=' + id_faq_kb) - // .auth(email, pwd) - // .end((err, res) => { - // if (log) { - // console.log("faq_list: ", res.body); - // } - // res.should.have.status(200); - // res.body.should.be.an('array').that.is.not.empty; - - // done(); - - // }) - // }) - // }) - // }) - // }) - // }) - - - it('exportjson', (done) => { - - - // this.timeout(); - - var email = "test-signup-" + Date.now() + "@email.com"; - var pwd = "pwd"; - - userService.signup(email, pwd, "Test Firstname", "Test lastname").then(function (savedUser) { - projectService.create("test-faqkb-create", savedUser._id).then(function (savedProject) { - - chai.request(server) - .post('/' + savedProject._id + '/faq_kb') - .auth(email, pwd) - .send({ "name": "testbot", type: "internal", template: "example", language: 'fr' }) - .end((err, res) => { - - if (err) { console.error("err: ", err )}; - if (log) { console.log("res.body: ", res.body )}; - - res.should.have.status(200); - res.body.should.be.a('object'); - expect(res.body.name).to.equal("testbot"); - expect(res.body.language).to.equal("fr"); - let id_faq_kb = res.body._id; - - if (log) { console.log("id_faq_kb: ", id_faq_kb); } - - chai.request(server) - .get('/' + savedProject._id + '/faq?id_faq_kb=' + id_faq_kb) - .auth(email, pwd) - .end((err, res) => { - - if (err) { console.error("err: ", err )}; - if (log) { console.log("res.body: ", res.body )}; - - res.should.have.status(200); - res.body.should.be.an('array').that.is.not.empty; - - chai.request(server) - .get('/' + savedProject._id + '/faq_kb/exportjson/' + id_faq_kb) - .auth(email, pwd) - .end((err, res) => { - - if (err) { console.error("err: ", err )}; - if (log) { console.log("res.body: ", res.body )}; - - res.should.have.status(200); - //res.body.should.be.a('string'); - - done(); - }) - }) // chai.request(server) // .patch('/' + savedProject._id + '/faq_kb/' + id_faq_kb + '/attributes') @@ -1095,7 +956,7 @@ describe('FaqKBRoute', () => { // .end((err, res) => { // console.log("res.body: ", res.body) - + // }) }); @@ -1104,11 +965,7 @@ describe('FaqKBRoute', () => { }).timeout(20000); - - it('export json (intents only)', (done) => { - - - // this.timeout(); + it('export-json-intents-only)', (done) => { var email = "test-signup-" + Date.now() + "@email.com"; var pwd = "pwd"; @@ -1122,8 +979,8 @@ describe('FaqKBRoute', () => { .send({ "name": "testbot", type: "internal", template: "example", language: 'fr' }) .end((err, res) => { - if (err) { console.error("err: ", err )}; - if (log) { console.log("res.body: ", res.body )}; + if (err) { console.error("err: ", err) }; + if (log) { console.log("res.body: ", res.body) }; res.should.have.status(200); res.body.should.be.a('object'); @@ -1136,9 +993,9 @@ describe('FaqKBRoute', () => { .get('/' + savedProject._id + '/faq?id_faq_kb=' + id_faq_kb) .auth(email, pwd) .end((err, res) => { - - if (err) { console.error("err: ", err )}; - if (log) { console.log("res.body: ", res.body )}; + + if (err) { console.error("err: ", err) }; + if (log) { console.log("res.body: ", res.body) }; res.should.have.status(200); res.body.should.be.an('array').that.is.not.empty; @@ -1147,9 +1004,9 @@ describe('FaqKBRoute', () => { .get('/' + savedProject._id + '/faq_kb/exportjson/' + id_faq_kb + "?intentsOnly=true") .auth(email, pwd) .end((err, res) => { - - if (err) { console.error("err: ", err )}; - if (log) { console.log("res.body: ", res.body )}; + + if (err) { console.error("err: ", err) }; + if (log) { console.log("res.body: ", res.body) }; res.should.have.status(200); //res.body.should.be.a('string'); @@ -1163,126 +1020,466 @@ describe('FaqKBRoute', () => { }).timeout(20000); + }) - // mocha test/faqkbRoute.js --grep 'train' - it('train', (done) => { + // describe('Train', () => { + // it('train', (done) => { - // this.timeout(); + // var email = "test-signup-" + Date.now() + "@email.com"; + // var pwd = "pwd"; - var email = "test-signup-" + Date.now() + "@email.com"; - var pwd = "pwd"; + // userService.signup(email, pwd, "Test Firstname", "Test lastname").then(function (savedUser) { + // projectService.create("test-faqkb-train", savedUser._id).then(function (savedProject) { + // faqService.create("testbot", "http://54.228.177.1644", savedProject._id, savedUser._id).then(function (savedBot) { - userService.signup(email, pwd, "Test Firstname", "Test lastname").then(function (savedUser) { - projectService.create("test-faqkb-train", savedUser._id).then(function (savedProject) { - faqService.create("testbot", "http://54.228.177.1644", savedProject._id, savedUser._id).then(function (savedBot) { - - var newFaq = new Faq({ - id_faq_kb: savedBot._id, - question: "question1\nquestion2", - answer: "answer", - id_project: savedProject._id, - topic: "default", - createdBy: savedUser._id, - updatedBy: savedUser._id - }); + // var newFaq = new Faq({ + // id_faq_kb: savedBot._id, + // question: "question1\nquestion2", + // answer: "answer", + // id_project: savedProject._id, + // topic: "default", + // createdBy: savedUser._id, + // updatedBy: savedUser._id + // }); - newFaq.save(function (err, savedFaq) { - expect(savedBot.name).to.equal("testbot"); - expect(savedBot.secret).to.not.equal(null); + // newFaq.save(function (err, savedFaq) { + // expect(savedBot.name).to.equal("testbot"); + // expect(savedBot.secret).to.not.equal(null); - chai.request(server) - .post('/' + savedProject._id + '/faq_kb/train') - .auth(email, pwd) - .send({ "id_faq_kb": savedBot._id }) - .end((err, res) => { - - if (err) { console.error("err: ", err )}; - if (log) { console.log("res.body: ", res.body )}; - - res.should.have.status(200); - res.body.should.be.a('object'); - expect(res.body.train.nlu.intent).to.equal(savedBot.intent_display_name); - // expect(res.body.text).to.equal("addestramento avviato"); + // chai.request(server) + // .post('/' + savedProject._id + '/faq_kb/train') + // .auth(email, pwd) + // .send({ "id_faq_kb": savedBot._id }) + // .end((err, res) => { + // if (err) { console.error("err: ", err) }; + // if (log) { console.log("res.body: ", res.body) }; - done(); - }); + // res.should.have.status(200); + // res.body.should.be.a('object'); + // expect(res.body.train.nlu.intent).to.equal(savedBot.intent_display_name); + // // expect(res.body.text).to.equal("addestramento avviato"); - }); - }); - }); - }); - }).timeout(20000); + // done(); + // }); - // it('publishChatbot', (done) => { - - // var email = "test-signup-" + Date.now() + "@email.com"; - // var pwd = "pwd"; - - // userService.signup(email, pwd, "Test Firstname", "Test Lastname").then(function (savedUser) { - // projectService.create("current-project", savedUser._id).then(function (currentProject) { - - // console.log("declare chatbot_service functions...") - - // class chatbot_service { - // async fork(id, api_url, token, project_id) { - // console.log("chatbot_service test fork called") - // return { message: "Chatbot forked successfully", bot_id: savedChatbot._id } - // //return chatbot_mock.existing_chatbot_mock; - // } - - // async getBotById(id, published, api_url, chatbot_templates_api_url, token, project_id, globals) { - // return chatbot_mock.existing_chatbot_mock; - // } - - // async createBot(api_url, token, chatbot, project_id) { - // return chatbot_mock.empty_chatbot_mock - // } - - // async importFaqs(api_url, id_faq_kb, token, chatbot, project_id) { - // return chatbot_mock.import_faqs_res_mock - // } - // } - - // server.set('chatbot_service', new chatbot_service()); - // console.log("chatbot_service functions declared") - - // chai.request(server) - // .post('/' + currentProject._id + '/faq_kb') - // .auth(email, pwd) - // .send({ "name": "privateBot", type: "internal", language: 'en', public: "false", template: "blank" }) - // .end((err, res) => { - // console.log("res.body: ", res.body); - // if (log) { - // } - // res.should.have.status(200); - // res.body.should.be.a('object'); - // expect(res.body.name).to.equal("privateBot"); - // expect(res.body.language).to.equal("en"); - // let id_faq_kb = res.body._id; - - // chai.request(server) - // .put('/' + currentProject._id + '/faq_kb/' + id_faq_kb + '/publish') - // .auth(email, pwd) - // .set('Content-Type', 'application/json') - // .end((err, res) => { - // console.log("publish bot res.body: ", res.body); - // res.should.have.status(200); - - // done(); - // }) - // }) - - - // }) - // }) - // }) + // }); + // }); + // }); + // }); + // }).timeout(20000); - }); + // }) }); +/** +* This test will be no longer available after merge with master because +* the profile section can no longer be modified via api. +*/ +// it('createMaximumNumberExceeded', (done) => { + +// var email = "test-signup-" + Date.now() + "@email.com"; +// var pwd = "pwd"; + +// userService.signup(email, pwd, "Test Firstname", "Test lastname").then(function (savedUser) { +// projectService.create("test-faqkb-create", savedUser._id).then(function (savedProject) { + +// chai.request(server) +// .put('/projects/' + savedProject._id) +// .auth(email, pwd) +// .send({ profile: { quotes: { chatbots: 2 } } }) +// .end((err, res) => { + +// if (log) { console.log("res.body", res.body); } + +// chai.request(server) +// .post('/' + savedProject._id + '/faq_kb') +// .auth(email, pwd) +// .send({ "name": "testbot1", type: "external", language: 'en' }) +// .end((err, res) => { +// if (log) { console.log("res.body", res.body); } +// res.should.have.status(200); +// res.body.should.be.a('object'); +// expect(res.body.name).to.equal("testbot1"); +// expect(res.body.language).to.equal("en"); + +// chai.request(server) +// .post('/' + savedProject._id + '/faq_kb') +// .auth(email, pwd) +// .send({ "name": "testbot2", type: "external", language: 'en' }) +// .end((err, res) => { +// if (log) { console.log("res.body", res.body); } +// res.should.have.status(200); +// res.body.should.be.a('object'); +// expect(res.body.name).to.equal("testbot2"); +// expect(res.body.language).to.equal("en"); + +// chai.request(server) +// .post('/' + savedProject._id + '/faq_kb') +// .auth(email, pwd) +// .send({ "name": "testbot3", type: "external", language: 'en' }) +// .end((err, res) => { + +// if (log) { console.log("res.body", res.body); } + +// res.should.have.status(403); +// res.body.should.be.a('object'); +// expect(res.body.success).to.equal(false); +// expect(res.body.error).to.equal("Maximum number of chatbots reached for the current plan"); +// expect(res.body.plan_limit).to.equal(2); + +// done() + +// }); +// }); +// }); +// }) +// }); +// }); + +// }).timeout(20000); + + +// it('train with tiledesk-ai', (done) => { +// var email = "test-signup-" + Date.now() + "@email.com"; +// var pwd = "pwd"; + +// userService.signup(email, pwd, "Test Firstname", "Test Lastname").then((savedUser) => { +// projectService.create("test-faqkb-create", savedUser._id).then((savedProject) => { + +// chai.request(server) +// .post('/' + savedProject._id + '/faq_kb') +// .auth(email, pwd) +// .send({ "name": "testbot", type: "internal", template: "example", intentsEngine: "tiledesk-ai" }) +// .end((err, res) => { +// if (log) { +// console.log("res.body", res.body); +// } +// res.should.have.status(200); +// res.body.should.be.a('object'); +// expect(res.body.name).to.equal("testbot"); +// var id_faq_kb = res.body._id; + +// chai.request(server) +// .get('/' + savedProject._id + '/faq?id_faq_kb=' + id_faq_kb) +// .auth(email, pwd) +// .end((err, res) => { +// if (log) { console.log("faq_list: ", res.body); } +// res.should.have.status(200); +// res.body.should.be.an('array').that.is.not.empty; + +// chai.request(server) +// .post('/' + savedProject._id + '/faq_kb/aitrain') +// .auth(email, pwd) +// .send({ id_faq_kb: id_faq_kb, webhook_enabled: false }) +// .end((err, res) => { +// if (log) { console.log("train res.body: ", res.body); } + +// done(); +// }) + +// }) + + + +// }); +// }) +// }) +// }) + + +// DEPRECATED +// it('import json (simple)', (done) => { + +// var email = "test-signup-" + Date.now() + "@email.com"; +// var pwd = "pwd"; + +// userService.signup(email, pwd, "Test Firstname", "Test lastname").then(function (savedUser) { +// projectService.create("test-faqkb-create", savedUser._id).then(function (savedProject) { + +// chai.request(server) +// .post('/' + savedProject._id + '/faq_kb') +// .auth(email, pwd) +// .send({ "name": "testbot", type: "internal", language: 'fr' }) +// .end((err, res) => { +// if (log) { +// console.log("res.body: ", res.body); +// } +// res.should.have.status(200); +// res.body.should.be.a('object'); +// expect(res.body.name).to.equal("testbot"); +// expect(res.body.language).to.equal("fr"); +// let id_faq_kb = res.body._id; + +// chai.request(server) +// .post('/' + savedProject._id + '/faq_kb/importjson/' + id_faq_kb + '?intentsOnly=true&overwrite=true') +// .auth(email, pwd) +// .set('Content-Type', 'text/plain') +// .attach('uploadFile', fs.readFileSync(path.resolve(__dirname, './example-json-intents.txt')), 'example-json-intents.txt') +// .end((err, res) => { +// if (log) { +// console.log("import (intents only) json res: ", res.body); +// } +// res.should.have.status(200); +// //res.should.be.a('object'); +// //expect(res.body.success).to.equal(true); + +// chai.request(server) +// .get('/' + savedProject._id + '/faq?id_faq_kb=' + id_faq_kb) +// .auth(email, pwd) +// .end((err, res) => { +// if (log) { +// console.log("faq_list: ", res.body); +// } +// res.should.have.status(200); +// res.body.should.be.an('array').that.is.not.empty; + +// done(); + +// }) +// }) +// }) +// }) +// }) +// }) + + +// DEPRECATED +// it('import json (intents only) (overwrite true)', (done) => { + +// var email = "test-signup-" + Date.now() + "@email.com"; +// var pwd = "pwd"; + +// userService.signup(email, pwd, "Test Firstname", "Test lastname").then(function (savedUser) { +// projectService.create("test-faqkb-create", savedUser._id).then(function (savedProject) { + +// chai.request(server) +// .post('/' + savedProject._id + '/faq_kb') +// .auth(email, pwd) +// .send({ "name": "testbot", type: "internal", language: 'fr', template: 'blank' }) +// .end((err, res) => { +// if (log) { +// console.log("res.body: ", res.body); +// } +// res.should.have.status(200); +// res.body.should.be.a('object'); +// expect(res.body.name).to.equal("testbot"); +// expect(res.body.language).to.equal("fr"); +// let id_faq_kb = res.body._id; + +// chai.request(server) +// .post('/' + savedProject._id + '/faq_kb/importjson/' + id_faq_kb + '?intentsOnly=true&overwrite=true') +// .auth(email, pwd) +// .set('Content-Type', 'text/plain') +// .attach('uploadFile', fs.readFileSync(path.resolve(__dirname, './example-json-intents.txt')), 'example-json-intents.txt') +// .end((err, res) => { +// if (log) { +// console.log("import (intents only) json res: ", res.body); +// } +// res.should.have.status(200); +// //res.should.be.a('object'); +// //expect(res.body.success).to.equal(true); + +// chai.request(server) +// .get('/' + savedProject._id + '/faq?id_faq_kb=' + id_faq_kb) +// .auth(email, pwd) +// .end((err, res) => { +// if (log) { +// console.log("faq_list: ", res.body); +// } +// res.should.have.status(200); +// res.body.should.be.an('array').that.is.not.empty; + +// done(); + +// }) +// }) +// }) +// }) +// }) +// }) + + +// DEPRECATED +// it('import json (intents only) (overwrite false)', (done) => { + +// var email = "test-signup-" + Date.now() + "@email.com"; +// var pwd = "pwd"; + +// userService.signup(email, pwd, "Test Firstname", "Test lastname").then(function (savedUser) { +// projectService.create("test-faqkb-create", savedUser._id).then(function (savedProject) { + +// chai.request(server) +// .post('/' + savedProject._id + '/faq_kb') +// .auth(email, pwd) +// .send({ "name": "testbot", type: "internal", language: 'fr', template: 'blank' }) +// .end((err, res) => { +// if (log) { +// console.log("res.body: ", res.body); +// } +// res.should.have.status(200); +// res.body.should.be.a('object'); +// expect(res.body.name).to.equal("testbot"); +// expect(res.body.language).to.equal("fr"); +// let id_faq_kb = res.body._id; + +// chai.request(server) +// .post('/' + savedProject._id + '/faq_kb/importjson/' + id_faq_kb + '?intentsOnly=true') +// .auth(email, pwd) +// .set('Content-Type', 'text/plain') +// .attach('uploadFile', fs.readFileSync(path.resolve(__dirname, './example-json-intents.txt')), 'example-json-intents.txt') +// .end((err, res) => { +// if (log) { +// console.log("import (intents only) json res: ", res.body); +// } +// res.should.have.status(200); +// //res.should.be.a('object'); +// //expect(res.body.success).to.equal(true); + +// chai.request(server) +// .get('/' + savedProject._id + '/faq?id_faq_kb=' + id_faq_kb) +// .auth(email, pwd) +// .end((err, res) => { +// if (log) { +// console.log("faq_list: ", res.body); +// } +// res.should.have.status(200); +// res.body.should.be.an('array').that.is.not.empty; + +// done(); + +// }) +// }) +// }) +// }) +// }) +// }) + +// it('publishChatbot', (done) => { + +// var email = "test-signup-" + Date.now() + "@email.com"; +// var pwd = "pwd"; + +// userService.signup(email, pwd, "Test Firstname", "Test Lastname").then(function (savedUser) { +// projectService.create("current-project", savedUser._id).then(function (currentProject) { + +// console.log("declare chatbot_service functions...") + +// class chatbot_service { +// async fork(id, api_url, token, project_id) { +// console.log("chatbot_service test fork called") +// return { message: "Chatbot forked successfully", bot_id: savedChatbot._id } +// //return chatbot_mock.existing_chatbot_mock; +// } + +// async getBotById(id, published, api_url, chatbot_templates_api_url, token, project_id, globals) { +// return chatbot_mock.existing_chatbot_mock; +// } + +// async createBot(api_url, token, chatbot, project_id) { +// return chatbot_mock.empty_chatbot_mock +// } + +// async importFaqs(api_url, id_faq_kb, token, chatbot, project_id) { +// return chatbot_mock.import_faqs_res_mock +// } +// } + +// server.set('chatbot_service', new chatbot_service()); +// console.log("chatbot_service functions declared") + +// chai.request(server) +// .post('/' + currentProject._id + '/faq_kb') +// .auth(email, pwd) +// .send({ "name": "privateBot", type: "internal", language: 'en', public: "false", template: "blank" }) +// .end((err, res) => { +// console.log("res.body: ", res.body); +// if (log) { +// } +// res.should.have.status(200); +// res.body.should.be.a('object'); +// expect(res.body.name).to.equal("privateBot"); +// expect(res.body.language).to.equal("en"); +// let id_faq_kb = res.body._id; + +// chai.request(server) +// .put('/' + currentProject._id + '/faq_kb/' + id_faq_kb + '/publish') +// .auth(email, pwd) +// .set('Content-Type', 'application/json') +// .end((err, res) => { +// console.log("publish bot res.body: ", res.body); +// res.should.have.status(200); + +// done(); +// }) +// }) + + +// }) +// }) +// }) + +// DEPRECATED +// it('import-json-overwrite-true', (done) => { + +// var email = "test-signup-" + Date.now() + "@email.com"; +// var pwd = "pwd"; + +// userService.signup(email, pwd, "Test Firstname", "Test lastname").then(function (savedUser) { +// projectService.create("test-faqkb-create", savedUser._id).then(function (savedProject) { + + +// chai.request(server) +// .post('/' + savedProject._id + '/faq_kb') +// .auth(email, pwd) +// .send({ "name": "testbot", type: "internal", language: 'fr', template: "blank" }) +// .end((err, res) => { + +// if (err) { console.error("err: ", err) }; +// if (log) { console.log("res.body: ", res.body) }; + +// res.should.have.status(200); +// res.body.should.be.a('object'); +// expect(res.body.name).to.equal("testbot"); +// expect(res.body.language).to.equal("fr"); +// let id_faq_kb = res.body._id; + +// chai.request(server) +// .post('/' + savedProject._id + '/faq_kb/importjson/' + id_faq_kb + "?overwrite=true") +// .auth(email, pwd) +// .set('Content-Type', 'text/plain') +// .attach('uploadFile', fs.readFileSync(path.resolve(__dirname, './example.json')), 'example.json') +// .end((err, res) => { + +// if (err) { console.error("err: ", err) }; +// if (log) { console.log("res.body: ", res.body) }; + +// res.should.have.status(200); +// res.should.be.a('object'); +// expect(res.body.name).to.equal("examplebot"); +// expect(res.body.language).to.equal("en"); + +// chai.request(server) +// .get('/' + savedProject._id + '/faq?id_faq_kb=' + id_faq_kb) +// .auth(email, pwd) +// .end((err, res) => { + +// if (err) { console.error("err: ", err) }; +// if (log) { console.log("res.body: ", res.body) }; + +// res.should.have.status(200); +// res.body.should.be.an('array').that.is.not.empty; + +// done(); + +// }) +// }) +// }) +// }) +// }) +// }) \ No newline at end of file