2018-06-15 11:03:24 -04:00
|
|
|
import userSearch from "discourse/lib/user-search";
|
2019-10-21 09:52:54 -04:00
|
|
|
import { CANCELLED_STATUS } from "discourse/lib/autocomplete";
|
2016-10-23 21:43:38 -04:00
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
QUnit.module("lib:user-search", {
|
|
|
|
beforeEach() {
|
2018-06-15 11:03:24 -04:00
|
|
|
const response = object => {
|
|
|
|
return [200, { "Content-Type": "application/json" }, object];
|
2016-10-24 00:17:17 -04:00
|
|
|
};
|
|
|
|
|
2018-06-15 12:18:45 -04:00
|
|
|
// prettier-ignore
|
2019-08-06 03:57:45 -04:00
|
|
|
server.get("/u/search/users", request => { //eslint-disable-line
|
|
|
|
|
|
|
|
// special responder for per category search
|
|
|
|
const categoryMatch = request.url.match(/category_id=([0-9]+)/);
|
|
|
|
if (categoryMatch) {
|
2019-10-21 09:52:54 -04:00
|
|
|
if(categoryMatch[1] === "3"){
|
|
|
|
return response({});
|
|
|
|
}
|
2019-08-06 03:57:45 -04:00
|
|
|
return response({
|
|
|
|
users: [
|
|
|
|
{
|
|
|
|
username: `category_${categoryMatch[1]}`,
|
|
|
|
name: "category user",
|
|
|
|
avatar_template:
|
|
|
|
"https://avatars.discourse.org/v3/letter/t/41988e/{size}.png"
|
|
|
|
}
|
|
|
|
]});
|
|
|
|
}
|
|
|
|
|
2019-10-28 14:32:13 -04:00
|
|
|
if(request.url.match(/no-results/)){
|
|
|
|
return response({users: []});
|
|
|
|
}
|
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
return response({
|
|
|
|
users: [
|
|
|
|
{
|
|
|
|
username: "TeaMoe",
|
|
|
|
name: "TeaMoe",
|
|
|
|
avatar_template:
|
|
|
|
"https://avatars.discourse.org/v3/letter/t/41988e/{size}.png"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
username: "TeamOneJ",
|
|
|
|
name: "J Cobb",
|
|
|
|
avatar_template:
|
2016-10-24 00:17:17 -04:00
|
|
|
"https://avatars.discourse.org/v3/letter/t/3d9bf3/{size}.png"
|
2018-06-15 11:03:24 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
username: "kudos",
|
|
|
|
name: "Team Blogeto.com",
|
|
|
|
avatar_template:
|
|
|
|
"/user_avatar/meta.discourse.org/kudos/{size}/62185_1.png"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
username: "RosieLinda",
|
|
|
|
name: "Linda Teaman",
|
|
|
|
avatar_template:
|
|
|
|
"https://avatars.discourse.org/v3/letter/r/bc8723/{size}.png"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
username: "legalatom",
|
|
|
|
name: "Team LegalAtom",
|
|
|
|
avatar_template:
|
|
|
|
"https://avatars.discourse.org/v3/letter/l/a9a28c/{size}.png"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
username: "dzsat_team",
|
|
|
|
name: "Dz Sat Dz Sat",
|
|
|
|
avatar_template:
|
|
|
|
"https://avatars.discourse.org/v3/letter/d/eb9ed0/{size}.png"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
groups: [
|
|
|
|
{
|
|
|
|
name: "bob",
|
|
|
|
usernames: []
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "team",
|
|
|
|
usernames: []
|
|
|
|
}
|
|
|
|
]
|
|
|
|
});
|
2016-10-24 00:17:17 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2016-10-23 21:43:38 -04:00
|
|
|
|
2019-08-06 03:57:45 -04:00
|
|
|
QUnit.test("it flushes cache when switching categories", async assert => {
|
|
|
|
let results = await userSearch({ term: "hello", categoryId: 1 });
|
|
|
|
assert.equal(results[0].username, "category_1");
|
|
|
|
assert.equal(results.length, 1);
|
|
|
|
|
|
|
|
// this is cached ... so let's check the cache is good
|
|
|
|
results = await userSearch({ term: "hello", categoryId: 1 });
|
|
|
|
assert.equal(results[0].username, "category_1");
|
|
|
|
assert.equal(results.length, 1);
|
|
|
|
|
|
|
|
results = await userSearch({ term: "hello", categoryId: 2 });
|
|
|
|
assert.equal(results[0].username, "category_2");
|
|
|
|
assert.equal(results.length, 1);
|
|
|
|
});
|
|
|
|
|
2019-10-21 09:52:54 -04:00
|
|
|
QUnit.test(
|
|
|
|
"it returns cancel when eager completing with no results",
|
|
|
|
async assert => {
|
|
|
|
// Do everything twice, to check the cache works correctly
|
|
|
|
|
|
|
|
for (let i = 0; i < 2; i++) {
|
|
|
|
// No topic or category, will always cancel
|
|
|
|
let result = await userSearch({ term: "" });
|
|
|
|
assert.equal(result, CANCELLED_STATUS);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let i = 0; i < 2; i++) {
|
|
|
|
// Unsecured category, so has no recommendations
|
|
|
|
let result = await userSearch({ term: "", categoryId: 3 });
|
|
|
|
assert.equal(result, CANCELLED_STATUS);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let i = 0; i < 2; i++) {
|
|
|
|
// Secured category, will have 1 recommendation
|
|
|
|
let results = await userSearch({ term: "", categoryId: 1 });
|
|
|
|
assert.equal(results[0].username, "category_1");
|
|
|
|
assert.equal(results.length, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2018-05-17 03:17:58 -04:00
|
|
|
QUnit.test("it places groups unconditionally for exact match", async assert => {
|
2018-06-15 11:03:24 -04:00
|
|
|
let results = await userSearch({ term: "Team" });
|
|
|
|
assert.equal(results[results.length - 1]["name"], "team");
|
2018-02-12 21:28:29 -05:00
|
|
|
});
|
2018-10-18 20:56:10 -04:00
|
|
|
|
|
|
|
QUnit.test("it strips @ from the beginning", async assert => {
|
|
|
|
let results = await userSearch({ term: "@Team" });
|
|
|
|
assert.equal(results[results.length - 1]["name"], "team");
|
|
|
|
});
|
2019-02-14 19:29:26 -05:00
|
|
|
|
2019-02-19 21:33:56 -05:00
|
|
|
QUnit.test("it skips a search depending on punctuations", async assert => {
|
2019-02-21 00:23:11 -05:00
|
|
|
let results;
|
2019-02-19 21:33:56 -05:00
|
|
|
let skippedTerms = [
|
|
|
|
"@sam s", // double space is not allowed
|
|
|
|
"@sam;",
|
|
|
|
"@sam,",
|
|
|
|
"@sam:"
|
|
|
|
];
|
|
|
|
|
2019-02-20 00:15:10 -05:00
|
|
|
for (let term of skippedTerms) {
|
2019-02-21 00:23:11 -05:00
|
|
|
results = await userSearch({ term });
|
2019-02-19 21:33:56 -05:00
|
|
|
assert.equal(results.length, 0);
|
2019-02-20 00:15:10 -05:00
|
|
|
}
|
2019-02-19 21:33:56 -05:00
|
|
|
|
|
|
|
let allowedTerms = [
|
|
|
|
"@sam sam", // double space is not allowed
|
|
|
|
"@sam.sam",
|
2019-02-28 23:23:45 -05:00
|
|
|
"@sam_sam",
|
|
|
|
"@sam-sam",
|
2019-02-19 21:33:56 -05:00
|
|
|
"@"
|
|
|
|
];
|
|
|
|
|
|
|
|
let topicId = 100;
|
|
|
|
|
2019-02-20 00:15:10 -05:00
|
|
|
for (let term of allowedTerms) {
|
2019-02-21 00:23:11 -05:00
|
|
|
results = await userSearch({ term, topicId });
|
2019-02-19 21:33:56 -05:00
|
|
|
assert.equal(results.length, 6);
|
2019-02-20 00:15:10 -05:00
|
|
|
}
|
2019-02-21 00:23:11 -05:00
|
|
|
|
|
|
|
results = await userSearch({ term: "sam@sam.com", allowEmails: true });
|
|
|
|
// 6 + email
|
|
|
|
assert.equal(results.length, 7);
|
|
|
|
|
2019-11-11 11:42:45 -05:00
|
|
|
results = await userSearch({ term: "sam+test@sam.com", allowEmails: true });
|
|
|
|
assert.equal(results.length, 7);
|
|
|
|
|
2019-02-21 00:23:11 -05:00
|
|
|
results = await userSearch({ term: "sam@sam.com" });
|
|
|
|
assert.equal(results.length, 0);
|
2019-10-28 14:32:13 -04:00
|
|
|
|
|
|
|
results = await userSearch({
|
|
|
|
term: "no-results@example.com",
|
|
|
|
allowEmails: true
|
|
|
|
});
|
|
|
|
assert.equal(results.length, 1);
|
2019-02-14 19:29:26 -05:00
|
|
|
});
|