2022-12-28 10:50:55 -05:00
import {
acceptance ,
exists ,
query ,
} from "discourse/tests/helpers/qunit-helpers" ;
2022-12-27 13:10:29 -05:00
import { click , currentURL , fillIn , visit } from "@ember/test-helpers" ;
import { test } from "qunit" ;
acceptance ( "Data Explorer Plugin | Param Input" , function ( needs ) {
needs . user ( ) ;
needs . settings ( { data _explorer _enabled : true } ) ;
needs . pretender ( ( server , helper ) => {
server . get ( "/admin/plugins/explorer/groups.json" , ( ) => {
return helper . response ( [
{
id : 1 ,
name : "admins" ,
} ,
{
id : 2 ,
name : "moderators" ,
} ,
{
id : 3 ,
name : "staff" ,
} ,
{
id : 0 ,
name : "everyone" ,
} ,
{
id : 10 ,
name : "trust_level_0" ,
} ,
{
id : 11 ,
name : "trust_level_1" ,
} ,
{
id : 12 ,
name : "trust_level_2" ,
} ,
{
id : 13 ,
name : "trust_level_3" ,
} ,
{
id : 14 ,
name : "trust_level_4" ,
} ,
2022-12-28 10:50:55 -05:00
{
id : 41 ,
name : "discourse" ,
} ,
2022-12-27 13:10:29 -05:00
] ) ;
} ) ;
server . get ( "/admin/plugins/explorer/schema.json" , ( ) => {
return helper . response ( {
anonymous _users : [
{
column _name : "id" ,
data _type : "serial" ,
primary : true ,
} ,
{
column _name : "user_id" ,
data _type : "integer" ,
fkey _info : "users" ,
} ,
{
column _name : "master_user_id" ,
data _type : "integer" ,
fkey _info : "users" ,
} ,
{
column _name : "active" ,
data _type : "boolean" ,
} ,
{
column _name : "created_at" ,
data _type : "timestamp" ,
} ,
{
column _name : "updated_at" ,
data _type : "timestamp" ,
} ,
] ,
} ) ;
} ) ;
server . get ( "/admin/plugins/explorer/queries" , ( ) => {
return helper . response ( {
queries : [
{
id : - 6 ,
sql : "-- [params]\n-- int :months_ago = 1\n\nWITH query_period AS (\n SELECT\n date_trunc('month', CURRENT_DATE) - INTERVAL ':months_ago months' as period_start,\n date_trunc('month', CURRENT_DATE) - INTERVAL ':months_ago months' + INTERVAL '1 month' - INTERVAL '1 second' as period_end\n )\n\n SELECT\n ua.user_id,\n count(1) AS like_count\n FROM user_actions ua\n INNER JOIN query_period qp\n ON ua.created_at >= qp.period_start\n AND ua.created_at <= qp.period_end\n WHERE ua.action_type = 1\n GROUP BY ua.user_id\n ORDER BY like_count DESC\n LIMIT 100\n" ,
name : "Top 100 Likers" ,
description :
"returns the top 100 likers for a given monthly period ordered by like_count. It accepts a ‘ months_ago’ parameter, defaults to 1 to give results for the last calendar month." ,
param _info : [
{
identifier : "months_ago" ,
type : "int" ,
default : "1" ,
nullable : false ,
} ,
] ,
created _at : "2021-02-02T12:21:11.449Z" ,
username : "system" ,
group _ids : [ ] ,
last _run _at : "2021-02-11T08:29:59.337Z" ,
hidden : false ,
user _id : - 1 ,
} ,
{
id : - 7 ,
sql : "-- [params]\n-- user_id :user\n\nSELECT :user_id\n\n" ,
name : "Invalid Query" ,
description : "" ,
param _info : [
{
identifier : "user" ,
type : "user_id" ,
default : null ,
nullable : false ,
} ,
] ,
created _at : "2022-01-14T16:40:05.458Z" ,
username : "bianca" ,
group _ids : [ ] ,
last _run _at : "2022-01-14T16:47:34.244Z" ,
hidden : false ,
user _id : 1 ,
} ,
] ,
} ) ;
} ) ;
server . put ( "/admin/plugins/explorer/queries/-6" , ( ) => {
return helper . response ( {
success : true ,
errors : [ ] ,
duration : 27.5 ,
result _count : 2 ,
columns : [ "user_id" , "like_count" ] ,
default _limit : 1000 ,
relations : {
user : [
{
id : - 2 ,
username : "discobot" ,
name : null ,
avatar _template : "/user_avatar/localhost/discobot/{size}/2_2.png" ,
} ,
{
id : 2 ,
username : "andrey1" ,
name : null ,
avatar _template :
"/letter_avatar_proxy/v4/letter/a/c0e974/{size}.png" ,
} ,
] ,
} ,
colrender : {
0 : "user" ,
} ,
rows : [
[ - 2 , 2 ] ,
[ 2 , 2 ] ,
] ,
} ) ;
} ) ;
server . post ( "/admin/plugins/explorer/queries/-6/run" , ( ) => {
return helper . response ( {
success : true ,
errors : [ ] ,
duration : 27.5 ,
result _count : 2 ,
params : { months _ago : "1" } ,
columns : [ "user_id" , "like_count" ] ,
default _limit : 1000 ,
relations : {
user : [
{
id : - 2 ,
username : "discobot" ,
name : null ,
avatar _template : "/user_avatar/localhost/discobot/{size}/2_2.png" ,
} ,
{
id : 2 ,
username : "andrey1" ,
name : null ,
avatar _template :
"/letter_avatar_proxy/v4/letter/a/c0e974/{size}.png" ,
} ,
] ,
} ,
colrender : {
0 : "user" ,
} ,
rows : [
[ - 2 , 2 ] ,
[ 2 , 2 ] ,
] ,
} ) ;
} ) ;
2022-12-28 10:50:55 -05:00
server . get ( "/g/discourse/reports/-8" , ( ) => {
return helper . response ( {
query : {
id : - 8 ,
sql : "-- [params]\n-- int :months_ago = 1\n\nWITH query_period AS (\n SELECT\n date_trunc('month', CURRENT_DATE) - INTERVAL ':months_ago months' as period_start,\n date_trunc('month', CURRENT_DATE) - INTERVAL ':months_ago months' + INTERVAL '1 month' - INTERVAL '1 second' as period_end\n )\n\n SELECT\n ua.user_id,\n count(1) AS like_count\n FROM user_actions ua\n INNER JOIN query_period qp\n ON ua.created_at >= qp.period_start\n AND ua.created_at <= qp.period_end\n WHERE ua.action_type = 1\n GROUP BY ua.user_id\n ORDER BY like_count DESC\n LIMIT 100\n" ,
name : "Top 100 Likers Report" ,
description :
"returns the top 100 likers for a given monthly period ordered by like_count. It accepts a ‘ months_ago’ parameter, defaults to 1 to give results for the last calendar month." ,
param _info : [
{
identifier : "months_ago" ,
type : "int" ,
default : "1" ,
nullable : false ,
} ,
] ,
created _at : "2021-02-02T12:21:11.449Z" ,
username : "system" ,
group _ids : [ 41 ] ,
last _run _at : "2021-02-11T08:29:59.337Z" ,
hidden : false ,
user _id : - 1 ,
} ,
} ) ;
} ) ;
2022-12-27 13:10:29 -05:00
server . post ( "/admin/plugins/explorer/queries/-7/run" , ( ) => {
return helper . response ( {
success : true ,
errors : [ ] ,
duration : 27.5 ,
params : { user _id : "null" } ,
columns : [ "user_id" ] ,
default _limit : 1000 ,
relations : {
user : [
{
id : 2 ,
username : "andrey1" ,
name : null ,
avatar _template :
"/letter_avatar_proxy/v4/letter/a/c0e974/{size}.png" ,
} ,
] ,
} ,
colrender : {
0 : "user" ,
} ,
rows : [ ] ,
} ) ;
} ) ;
2022-12-28 10:50:55 -05:00
server . post ( "/g/discourse/reports/-8/run" , ( ) => {
return helper . response ( {
success : true ,
errors : [ ] ,
duration : 27.5 ,
result _count : 2 ,
params : { months _ago : "1" } ,
columns : [ "user_id" , "like_count" ] ,
default _limit : 1000 ,
relations : {
user : [
{
id : - 2 ,
username : "discobot" ,
name : null ,
avatar _template : "/user_avatar/localhost/discobot/{size}/2_2.png" ,
} ,
{
id : 2 ,
username : "andrey1" ,
name : null ,
avatar _template :
"/letter_avatar_proxy/v4/letter/a/c0e974/{size}.png" ,
} ,
] ,
} ,
colrender : {
0 : "user" ,
} ,
rows : [
[ - 2 , 2 ] ,
[ 2 , 2 ] ,
] ,
} ) ;
} ) ;
2022-12-27 13:10:29 -05:00
} ) ;
test ( "it puts params for the query into the url" , async function ( assert ) {
2023-11-21 16:23:35 -05:00
await visit ( "/admin/plugins/explorer?id=-6" ) ;
2022-12-27 13:10:29 -05:00
const monthsAgoValue = "2" ;
await fillIn ( ".query-params input" , monthsAgoValue ) ;
await click ( "form.query-run button" ) ;
const searchParams = new URLSearchParams ( currentURL ( ) ) ;
const monthsAgoParam = JSON . parse ( searchParams . get ( "params" ) ) . months _ago ;
assert . equal ( monthsAgoParam , monthsAgoValue ) ;
} ) ;
test ( "it loads the page if one of the parameter is null" , async function ( assert ) {
2023-11-23 14:34:09 -05:00
await visit ( '/admin/plugins/explorer?id=-7¶ms={"user":null}' ) ;
2022-12-27 13:10:29 -05:00
assert . ok ( exists ( ".query-params .user-chooser" ) ) ;
assert . ok ( exists ( ".query-run .btn.btn-primary" ) ) ;
} ) ;
2022-12-28 10:50:55 -05:00
test ( "it applies params when running a report" , async function ( assert ) {
await visit ( "/g/discourse/reports/-8" ) ;
const monthsAgoValue = "2" ;
await fillIn ( ".query-params input" , monthsAgoValue ) ;
await click ( "form.query-run button" ) ;
assert . equal ( query ( ".query-params input" ) . value , monthsAgoValue ) ;
} ) ;
2022-12-27 13:10:29 -05:00
} ) ;