Added the Social Groups plugin. This is still under construction.
Made a few improvements to the ForumStore, including bringing it's API closer in line with the other datastores, adding stubs for future subforum functionality, and improving efficiency in a few places.
The auth interface now handles all the authentication stuff.
Renamed the debug config variable to debug_mode.
Added the PluginPerms API.
Internal Errors will now dump the stack trace in the console.
Added support for installable plugins.
Refactored the routing logic so that the router now handles the common PreRoute logic(exc. /static/)
Added the CreateTable method to the query generator. It might need some tweaking to better support other database systems.
Added the same CreateTable method to the query builder.
Began work on PostgreSQL support.
Added the string-string hook type
Added the pre_render hook type.
Added the ParentID and ParentType fields to forums.
Added the get_forum_url_prefix function.
Added a more generic build_slug function.
Added the get_topic_url_prefix function.
Added the override_perms and override_forum_perms functions for bulk setting and unsetting permissions.
Added more ExtData fields in a few structs and removed them on the Perms struct as the PluginPerms API supersedes them there.
Plugins can now see the router instance.
The plugin initialisation handlers can now throw errors.
Plugins are now initialised after all the forum's subsystems are.
Refactored the unit test logic. For instance, we now use the proper .Log method rather than fmt.Println in many cases.
Sorry, we'll have to break Github's generated file detection, as the build instructions aren't working, unless I put them at the top, and they're far, far more important than getting Github to recognise the generated code as generated code.
Fixed an issue with mysql.go's _init_database() overwriting the dbpassword variable. Not a huge issue, but it is a "gotcha" for those not expecting a ':' at the start.
Fixed an issue with forum creation where the forum permissions didn't get cached.
Fixed a bug in plugin_bbcode where negative numbers in rand would crash Gosora.
Made the outputs of plugin_markdown and plugin_bbcode more compliant with the tests.
Revamped the phrase system to make it easier for us to add language pack related features in the future.
Added the WidgetMenu widget type.
Revamped the theme again. I'm experimenting to see which approach I like most.
- Excuse the little W3C rage. Some things about CSS drive me crazy :p
Tests:
Added 22 bbcode_full_parse tests.
Added 19 bbcode_regex_parse tests.
Added 27 markdown_parse tests.
Added four UserStore tests. More to come when the test database functionality is added.
Added 18 name_to_slug tests.
Hooks:
Added the pre_render hook.
Added the pre_render_forum_list hook.
Added the pre_render_view_forum hook.
Added the pre_render_topic_list hook.
Added the pre_render_view_topic hook.
Added the pre_render_profile hook.
Added the pre_render_custom_page hook.
Added the pre_render_overview hook.
Added the pre_render_create_topic hook.
Added the pre_render_account_own_edit_critical hook.
Added the pre_render_account_own_edit_avatar hook.
Added the pre_render_account_own_edit_username hook.
Added the pre_render_account_own_edit_email hook.
Added the pre_render_login hook.
Added the pre_render_register hook.
Added the pre_render_ban hook.
Added the pre_render_panel_dashboard hook.
Added the pre_render_panel_forums hook.
Added the pre_render_panel_delete_forum hook.
Added the pre_render_panel_edit_forum hook.
Added the pre_render_panel_settings hook.
Added the pre_render_panel_setting hook.
Added the pre_render_panel_plugins hook.
Added the pre_render_panel_users hook.
Added the pre_render_panel_edit_user hook.
Added the pre_render_panel_groups hook.
Added the pre_render_panel_edit_group hook.
Added the pre_render_panel_edit_group_perms hook.
Added the pre_render_panel_themes hook.
Added the pre_render_panel_mod_log hook.
Added the pre_render_error hook.
Added the pre_render_security_error hook.
Added the create_group_preappend hook.
Added the intercept_build_widgets hook.
Added the simple_forum_check_pre_perms hook.
Added the forum_check_pre_perms hook.
2017-07-09 12:06:04 +00:00
package main
2017-10-16 07:32:58 +00:00
import (
2017-10-21 00:27:47 +00:00
"bytes"
2019-10-06 01:49:31 +00:00
"database/sql"
2017-10-16 07:32:58 +00:00
"fmt"
2020-02-19 05:12:59 +00:00
"io/ioutil"
2021-03-24 12:48:36 +00:00
"net/http"
2017-10-21 00:27:47 +00:00
"net/http/httptest"
2020-02-19 05:12:59 +00:00
"os"
2017-10-16 07:32:58 +00:00
"runtime/debug"
2018-06-06 00:21:22 +00:00
"strconv"
2017-10-16 07:32:58 +00:00
"testing"
"time"
2017-11-11 04:06:16 +00:00
2019-04-19 08:20:10 +00:00
c "github.com/Azareal/Gosora/common"
2020-04-02 22:57:46 +00:00
"github.com/Azareal/Gosora/common/gauth"
2020-04-10 01:32:02 +00:00
"github.com/Azareal/Gosora/common/phrases"
2021-03-24 12:48:36 +00:00
"github.com/Azareal/Gosora/routes"
2020-06-17 22:03:36 +00:00
"github.com/pkg/errors"
2017-10-16 07:32:58 +00:00
)
2018-11-01 06:43:56 +00:00
func miscinit ( t * testing . T ) {
2019-10-06 00:34:09 +00:00
if err := gloinit ( ) ; err != nil {
2018-11-01 06:43:56 +00:00
t . Fatal ( err )
}
}
2017-10-21 00:27:47 +00:00
func recordMustExist ( t * testing . T , err error , errmsg string , args ... interface { } ) {
2017-10-16 07:32:58 +00:00
if err == ErrNoRows {
2018-05-28 06:27:12 +00:00
debug . PrintStack ( )
2017-10-21 00:27:47 +00:00
t . Errorf ( errmsg , args ... )
2017-10-16 07:32:58 +00:00
} else if err != nil {
2018-05-28 06:27:12 +00:00
debug . PrintStack ( )
2017-10-16 07:32:58 +00:00
t . Fatal ( err )
}
}
2017-10-21 00:27:47 +00:00
func recordMustNotExist ( t * testing . T , err error , errmsg string , args ... interface { } ) {
2017-10-16 07:32:58 +00:00
if err == nil {
2018-05-28 06:27:12 +00:00
debug . PrintStack ( )
2017-10-21 00:27:47 +00:00
t . Errorf ( errmsg , args ... )
2017-10-16 07:32:58 +00:00
} else if err != ErrNoRows {
2018-05-28 06:27:12 +00:00
debug . PrintStack ( )
2017-10-16 07:32:58 +00:00
t . Fatal ( err )
}
}
Added the Social Groups plugin. This is still under construction.
Made a few improvements to the ForumStore, including bringing it's API closer in line with the other datastores, adding stubs for future subforum functionality, and improving efficiency in a few places.
The auth interface now handles all the authentication stuff.
Renamed the debug config variable to debug_mode.
Added the PluginPerms API.
Internal Errors will now dump the stack trace in the console.
Added support for installable plugins.
Refactored the routing logic so that the router now handles the common PreRoute logic(exc. /static/)
Added the CreateTable method to the query generator. It might need some tweaking to better support other database systems.
Added the same CreateTable method to the query builder.
Began work on PostgreSQL support.
Added the string-string hook type
Added the pre_render hook type.
Added the ParentID and ParentType fields to forums.
Added the get_forum_url_prefix function.
Added a more generic build_slug function.
Added the get_topic_url_prefix function.
Added the override_perms and override_forum_perms functions for bulk setting and unsetting permissions.
Added more ExtData fields in a few structs and removed them on the Perms struct as the PluginPerms API supersedes them there.
Plugins can now see the router instance.
The plugin initialisation handlers can now throw errors.
Plugins are now initialised after all the forum's subsystems are.
Refactored the unit test logic. For instance, we now use the proper .Log method rather than fmt.Println in many cases.
Sorry, we'll have to break Github's generated file detection, as the build instructions aren't working, unless I put them at the top, and they're far, far more important than getting Github to recognise the generated code as generated code.
Fixed an issue with mysql.go's _init_database() overwriting the dbpassword variable. Not a huge issue, but it is a "gotcha" for those not expecting a ':' at the start.
Fixed an issue with forum creation where the forum permissions didn't get cached.
Fixed a bug in plugin_bbcode where negative numbers in rand would crash Gosora.
Made the outputs of plugin_markdown and plugin_bbcode more compliant with the tests.
Revamped the phrase system to make it easier for us to add language pack related features in the future.
Added the WidgetMenu widget type.
Revamped the theme again. I'm experimenting to see which approach I like most.
- Excuse the little W3C rage. Some things about CSS drive me crazy :p
Tests:
Added 22 bbcode_full_parse tests.
Added 19 bbcode_regex_parse tests.
Added 27 markdown_parse tests.
Added four UserStore tests. More to come when the test database functionality is added.
Added 18 name_to_slug tests.
Hooks:
Added the pre_render hook.
Added the pre_render_forum_list hook.
Added the pre_render_view_forum hook.
Added the pre_render_topic_list hook.
Added the pre_render_view_topic hook.
Added the pre_render_profile hook.
Added the pre_render_custom_page hook.
Added the pre_render_overview hook.
Added the pre_render_create_topic hook.
Added the pre_render_account_own_edit_critical hook.
Added the pre_render_account_own_edit_avatar hook.
Added the pre_render_account_own_edit_username hook.
Added the pre_render_account_own_edit_email hook.
Added the pre_render_login hook.
Added the pre_render_register hook.
Added the pre_render_ban hook.
Added the pre_render_panel_dashboard hook.
Added the pre_render_panel_forums hook.
Added the pre_render_panel_delete_forum hook.
Added the pre_render_panel_edit_forum hook.
Added the pre_render_panel_settings hook.
Added the pre_render_panel_setting hook.
Added the pre_render_panel_plugins hook.
Added the pre_render_panel_users hook.
Added the pre_render_panel_edit_user hook.
Added the pre_render_panel_groups hook.
Added the pre_render_panel_edit_group hook.
Added the pre_render_panel_edit_group_perms hook.
Added the pre_render_panel_themes hook.
Added the pre_render_panel_mod_log hook.
Added the pre_render_error hook.
Added the pre_render_security_error hook.
Added the create_group_preappend hook.
Added the intercept_build_widgets hook.
Added the simple_forum_check_pre_perms hook.
Added the forum_check_pre_perms hook.
2017-07-09 12:06:04 +00:00
func TestUserStore ( t * testing . T ) {
2018-11-01 06:43:56 +00:00
miscinit ( t )
2019-04-19 08:20:10 +00:00
if ! c . PluginsInited {
c . InitPlugins ( )
2017-09-03 04:50:31 +00:00
}
2017-11-02 13:35:19 +00:00
var err error
2020-04-02 07:39:49 +00:00
uc := c . NewMemoryUserCache ( c . Config . UserCacheCapacity )
c . Users , err = c . NewDefaultUserStore ( uc )
2017-11-02 13:35:19 +00:00
expectNilErr ( t , err )
2020-04-02 07:39:49 +00:00
uc . Flush ( )
2017-10-16 07:32:58 +00:00
userStoreTest ( t , 2 )
2019-04-19 08:20:10 +00:00
c . Users , err = c . NewDefaultUserStore ( nil )
2017-11-02 13:35:19 +00:00
expectNilErr ( t , err )
2018-10-04 04:59:14 +00:00
userStoreTest ( t , 5 )
2017-09-15 22:20:01 +00:00
}
2017-10-16 07:32:58 +00:00
func userStoreTest ( t * testing . T , newUserID int ) {
2020-04-02 07:39:49 +00:00
uc := c . Users . GetCache ( )
2017-10-21 00:27:47 +00:00
// Go doesn't have short-circuiting, so this'll allow us to do one liner tests
2020-02-04 11:47:03 +00:00
cacheLength := func ( uc c . UserCache ) int {
if uc == nil {
2017-11-23 05:37:08 +00:00
return 0
}
2020-02-04 11:47:03 +00:00
return uc . Length ( )
2017-09-18 17:03:52 +00:00
}
2021-02-28 02:23:05 +00:00
isCacheLengthZero := func ( uc c . UserCache ) bool {
return cacheLength ( uc ) == 0
}
2021-03-24 12:48:36 +00:00
ex , exf := exp ( t ) , expf ( t )
exf ( isCacheLengthZero ( uc ) , "The initial ucache length should be zero, not %d" , cacheLength ( uc ) )
2017-09-03 04:50:31 +00:00
2019-04-19 08:20:10 +00:00
_ , err := c . Users . Get ( - 1 )
2017-10-16 07:32:58 +00:00
recordMustNotExist ( t , err , "UID #-1 shouldn't exist" )
2021-03-24 12:48:36 +00:00
exf ( isCacheLengthZero ( uc ) , "We found %d items in the user cache and it's supposed to be empty" , cacheLength ( uc ) )
2019-04-19 08:20:10 +00:00
_ , err = c . Users . Get ( 0 )
2017-10-16 07:32:58 +00:00
recordMustNotExist ( t , err , "UID #0 shouldn't exist" )
2021-03-24 12:48:36 +00:00
exf ( isCacheLengthZero ( uc ) , "We found %d items in the user cache and it's supposed to be empty" , cacheLength ( uc ) )
2017-09-03 04:50:31 +00:00
2019-04-19 08:20:10 +00:00
user , err := c . Users . Get ( 1 )
2017-10-16 07:32:58 +00:00
recordMustExist ( t , err , "Couldn't find UID #1" )
2017-09-03 04:50:31 +00:00
2020-02-04 11:47:03 +00:00
expectW := func ( cond , expec bool , prefix , suffix string ) {
2018-10-04 02:58:01 +00:00
midfix := "should not be"
if expec {
midfix = "should be"
}
2021-03-24 12:48:36 +00:00
ex ( cond , prefix + " " + midfix + " " + suffix )
2018-10-04 02:58:01 +00:00
}
2018-12-27 05:42:41 +00:00
// TODO: Add email checks too? Do them separately?
2020-02-04 11:47:03 +00:00
expectUser := func ( u * c . User , uid int , name string , group int , super , admin , mod , banned bool ) {
2021-03-24 12:48:36 +00:00
exf ( u . ID == uid , "u.ID should be %d. Got '%d' instead." , uid , u . ID )
exf ( u . Name == name , "u.Name should be '%s', not '%s'" , name , u . Name )
2019-10-06 00:34:09 +00:00
expectW ( u . Group == group , true , u . Name , "in group" + strconv . Itoa ( group ) )
expectW ( u . IsSuperAdmin == super , super , u . Name , "a super admin" )
expectW ( u . IsAdmin == admin , admin , u . Name , "an admin" )
expectW ( u . IsSuperMod == mod , mod , u . Name , "a super mod" )
expectW ( u . IsMod == mod , mod , u . Name , "a mod" )
expectW ( u . IsBanned == banned , banned , u . Name , "banned" )
2018-10-04 02:58:01 +00:00
}
expectUser ( user , 1 , "Admin" , 1 , true , true , true , false )
2017-10-16 07:32:58 +00:00
2021-02-28 02:23:05 +00:00
user , err = c . Users . GetByName ( "Admin" )
recordMustExist ( t , err , "Couldn't find user 'Admin'" )
expectUser ( user , 1 , "Admin" , 1 , true , true , true , false )
us , err := c . Users . BulkGetByName ( [ ] string { "Admin" } )
recordMustExist ( t , err , "Couldn't find user 'Admin'" )
2021-03-24 12:48:36 +00:00
exf ( len ( us ) == 1 , "len(us) should be 1, not %d" , len ( us ) )
2021-02-28 02:23:05 +00:00
expectUser ( us [ 0 ] , 1 , "Admin" , 1 , true , true , true , false )
2019-04-19 08:20:10 +00:00
_ , err = c . Users . Get ( newUserID )
2017-10-16 07:32:58 +00:00
recordMustNotExist ( t , err , fmt . Sprintf ( "UID #%d shouldn't exist" , newUserID ) )
2017-09-03 04:50:31 +00:00
2021-02-28 02:23:05 +00:00
// TODO: GetByName tests for newUserID
2020-04-02 07:39:49 +00:00
if uc != nil {
expectIntToBeX ( t , uc . Length ( ) , 1 , "User cache length should be 1, not %d" )
_ , err = uc . Get ( - 1 )
2017-10-16 07:32:58 +00:00
recordMustNotExist ( t , err , "UID #-1 shouldn't exist, even in the cache" )
2020-04-02 07:39:49 +00:00
_ , err = uc . Get ( 0 )
2017-10-16 07:32:58 +00:00
recordMustNotExist ( t , err , "UID #0 shouldn't exist, even in the cache" )
2020-04-02 07:39:49 +00:00
user , err = uc . Get ( 1 )
2017-10-16 07:32:58 +00:00
recordMustExist ( t , err , "Couldn't find UID #1 in the cache" )
2017-09-18 17:03:52 +00:00
2021-03-24 12:48:36 +00:00
exf ( user . ID == 1 , "user.ID does not match the requested UID. Got '%d' instead." , user . ID )
exf ( user . Name == "Admin" , "user.Name should be 'Admin', not '%s'" , user . Name )
2017-10-16 07:32:58 +00:00
2020-04-02 07:39:49 +00:00
_ , err = uc . Get ( newUserID )
2017-10-21 00:27:47 +00:00
recordMustNotExist ( t , err , "UID #%d shouldn't exist, even in the cache" , newUserID )
2020-04-02 07:39:49 +00:00
uc . Flush ( )
expectIntToBeX ( t , uc . Length ( ) , 0 , "User cache length should be 0, not %d" )
2017-09-18 17:03:52 +00:00
}
2017-09-10 16:57:22 +00:00
// TODO: Lock onto the specific error type. Is this even possible without sacrificing the detailed information in the error message?
2021-03-02 07:22:32 +00:00
bulkGetMapEmpty := func ( id int ) {
userList , _ := c . Users . BulkGetMap ( [ ] int { id } )
2021-03-24 12:48:36 +00:00
exf ( len ( userList ) == 0 , "The userList length should be 0, not %d" , len ( userList ) )
exf ( isCacheLengthZero ( uc ) , "User cache length should be 0, not %d" , cacheLength ( uc ) )
2021-03-02 07:22:32 +00:00
}
bulkGetMapEmpty ( - 1 )
bulkGetMapEmpty ( 0 )
2017-09-18 17:03:52 +00:00
2021-03-02 07:22:32 +00:00
userList , _ := c . Users . BulkGetMap ( [ ] int { 1 } )
2021-03-24 12:48:36 +00:00
exf ( len ( userList ) == 1 , "Returned map should have one result (UID #1), not %d" , len ( userList ) )
2017-09-15 22:20:01 +00:00
user , ok := userList [ 1 ]
if ! ok {
t . Error ( "We couldn't find UID #1 in the returned map" )
t . Error ( "userList" , userList )
2017-12-25 06:12:19 +00:00
return
2017-09-15 22:20:01 +00:00
}
2021-03-24 12:48:36 +00:00
exf ( user . ID == 1 , "user.ID does not match the requested UID. Got '%d' instead." , user . ID )
2017-09-15 22:20:01 +00:00
2020-04-02 07:39:49 +00:00
if uc != nil {
expectIntToBeX ( t , uc . Length ( ) , 1 , "User cache length should be 1, not %d" )
user , err = uc . Get ( 1 )
2017-10-16 07:32:58 +00:00
recordMustExist ( t , err , "Couldn't find UID #1 in the cache" )
2017-09-18 17:03:52 +00:00
2021-03-24 12:48:36 +00:00
exf ( user . ID == 1 , "user.ID does not match the requested UID. Got '%d' instead." , user . ID )
2020-04-02 07:39:49 +00:00
uc . Flush ( )
2017-09-18 17:03:52 +00:00
}
2021-03-24 12:48:36 +00:00
ex ( ! c . Users . Exists ( - 1 ) , "UID #-1 shouldn't exist" )
ex ( ! c . Users . Exists ( 0 ) , "UID #0 shouldn't exist" )
ex ( c . Users . Exists ( 1 ) , "UID #1 should exist" )
exf ( ! c . Users . Exists ( newUserID ) , "UID #%d shouldn't exist" , newUserID )
2017-10-16 07:32:58 +00:00
2021-03-24 12:48:36 +00:00
exf ( isCacheLengthZero ( uc ) , "User cache length should be 0, not %d" , cacheLength ( uc ) )
2021-03-02 07:22:32 +00:00
expectIntToBeX ( t , c . Users . Count ( ) , 1 , "The number of users should be 1, not %d" )
searchUser := func ( name , email string , gid , count int ) {
f := func ( name , email string , gid , count int , m string ) {
expectIntToBeX ( t , c . Users . CountSearch ( name , email , gid ) , count , "The number of users for " + m + ", not %d" )
}
f ( name , email , 0 , count , fmt . Sprintf ( "name '%s' and email '%s' should be %d" , name , email , count ) )
f ( name , "" , 0 , count , fmt . Sprintf ( "name '%s' should be %d" , name , count ) )
f ( "" , email , 0 , count , fmt . Sprintf ( "email '%s' should be %d" , email , count ) )
f2 := func ( name , email string , gid , offset int , m string , args ... interface { } ) {
ulist , err := c . Users . SearchOffset ( name , email , gid , offset , 15 )
expectNilErr ( t , err )
expectIntToBeX ( t , len ( ulist ) , count , "The number of users for " + fmt . Sprintf ( m , args ... ) + ", not %d" )
}
f2 ( name , email , 0 , 0 , "name '%s' and email '%s' should be %d" , name , email , count )
f2 ( name , "" , 0 , 0 , "name '%s' should be %d" , name , count )
f2 ( "" , email , 0 , 0 , "email '%s' should be %d" , email , count )
count = 0
f2 ( name , email , 0 , 10 , "name '%s' and email '%s' should be %d" , name , email , count )
f2 ( name , "" , 0 , 10 , "name '%s' should be %d" , name , count )
f2 ( "" , email , 0 , 10 , "email '%s' should be %d" , email , count )
f2 ( name , email , 999 , 0 , "name '%s' and email '%s' should be %d" , name , email , 0 )
f2 ( name , "" , 999 , 0 , "name '%s' should be %d" , name , 0 )
f2 ( "" , email , 999 , 0 , "email '%s' should be %d" , email , 0 )
f2 ( name , email , 999 , 10 , "name '%s' and email '%s' should be %d" , name , email , 0 )
f2 ( name , "" , 999 , 10 , "name '%s' should be %d" , name , 0 )
f2 ( "" , email , 999 , 10 , "email '%s' should be %d" , email , 0 )
}
searchUser ( "Sam" , "sam@localhost.loc" , 0 , 0 )
// TODO: CountSearch gid test
2017-09-03 04:50:31 +00:00
2019-10-06 00:34:09 +00:00
awaitingActivation := 5
2018-10-04 02:58:01 +00:00
// TODO: Write tests for the registration validators
2019-04-19 08:20:10 +00:00
uid , err := c . Users . Create ( "Sam" , "ReallyBadPassword" , "sam@localhost.loc" , awaitingActivation , false )
2017-10-21 00:27:47 +00:00
expectNilErr ( t , err )
2021-03-24 12:48:36 +00:00
exf ( uid == newUserID , "The UID of the new user should be %d not %d" , newUserID , uid )
exf ( c . Users . Exists ( newUserID ) , "UID #%d should exist" , newUserID )
2021-03-02 07:22:32 +00:00
expectIntToBeX ( t , c . Users . Count ( ) , 2 , "The number of users should be 2, not %d" )
searchUser ( "Sam" , "sam@localhost.loc" , 0 , 1 )
// TODO: CountSearch gid test
2017-09-03 04:50:31 +00:00
2019-04-19 08:20:10 +00:00
user , err = c . Users . Get ( newUserID )
2017-10-21 00:27:47 +00:00
recordMustExist ( t , err , "Couldn't find UID #%d" , newUserID )
2018-10-04 02:58:01 +00:00
expectUser ( user , newUserID , "Sam" , 5 , false , false , false , false )
2017-09-15 22:20:01 +00:00
2020-04-02 07:39:49 +00:00
if uc != nil {
expectIntToBeX ( t , uc . Length ( ) , 1 , "User cache length should be 1, not %d" )
user , err = uc . Get ( newUserID )
2019-06-16 06:30:58 +00:00
recordMustExist ( t , err , "Couldn't find UID #%d in the cache" , newUserID )
2021-03-24 12:48:36 +00:00
exf ( user . ID == newUserID , "user.ID does not match the requested UID. Got '%d' instead." , user . ID )
2019-06-16 06:30:58 +00:00
}
2019-10-06 01:49:31 +00:00
userList , _ = c . Users . BulkGetMap ( [ ] int { 1 , uid } )
2021-03-24 12:48:36 +00:00
exf ( len ( userList ) == 2 , "Returned map should have 2 results, not %d" , len ( userList ) )
2021-03-02 07:22:32 +00:00
// TODO: More tests on userList
{
userList , _ := c . Users . BulkGetByName ( [ ] string { "Admin" , "Sam" } )
2021-03-24 12:48:36 +00:00
exf ( len ( userList ) == 2 , "Returned list should have 2 results, not %d" , len ( userList ) )
2021-03-02 07:22:32 +00:00
}
2019-06-16 06:30:58 +00:00
2020-04-02 07:39:49 +00:00
if uc != nil {
expectIntToBeX ( t , uc . Length ( ) , 2 , "User cache length should be 2, not %d" )
user , err = uc . Get ( 1 )
2019-06-16 06:30:58 +00:00
recordMustExist ( t , err , "Couldn't find UID #%d in the cache" , 1 )
2021-03-24 12:48:36 +00:00
exf ( user . ID == 1 , "user.ID does not match the requested UID. Got '%d' instead." , user . ID )
2020-04-02 07:39:49 +00:00
user , err = uc . Get ( newUserID )
2019-06-16 06:30:58 +00:00
recordMustExist ( t , err , "Couldn't find UID #%d in the cache" , newUserID )
2021-03-24 12:48:36 +00:00
exf ( user . ID == newUserID , "user.ID does not match the requested UID. Got '%d' instead." , user . ID )
2020-04-02 07:39:49 +00:00
uc . Flush ( )
2019-06-16 06:30:58 +00:00
}
user , err = c . Users . Get ( newUserID )
recordMustExist ( t , err , "Couldn't find UID #%d" , newUserID )
expectUser ( user , newUserID , "Sam" , 5 , false , false , false , false )
2020-04-02 07:39:49 +00:00
if uc != nil {
expectIntToBeX ( t , uc . Length ( ) , 1 , "User cache length should be 1, not %d" )
user , err = uc . Get ( newUserID )
2017-10-21 00:27:47 +00:00
recordMustExist ( t , err , "Couldn't find UID #%d in the cache" , newUserID )
2021-03-24 12:48:36 +00:00
exf ( user . ID == newUserID , "user.ID does not match the requested UID. Got '%d' instead." , user . ID )
2017-09-18 17:03:52 +00:00
}
2021-03-24 12:48:36 +00:00
expectNilErr ( t , user . Activate ( ) )
2017-10-16 07:32:58 +00:00
expectIntToBeX ( t , user . Group , 5 , "Sam should still be in group 5 in this copy" )
// ? - What if we change the caching mechanism so it isn't hard purged and reloaded? We'll deal with that when we come to it, but for now, this is a sign of a cache bug
2019-10-06 00:34:09 +00:00
afterUserFlush := func ( uid int ) {
2020-04-02 07:39:49 +00:00
if uc != nil {
expectIntToBeX ( t , uc . Length ( ) , 0 , "User cache length should be 0, not %d" )
_ , err = uc . Get ( uid )
2018-10-05 07:41:28 +00:00
recordMustNotExist ( t , err , "UID #%d shouldn't be in the cache" , uid )
}
2017-10-16 07:32:58 +00:00
}
2018-10-05 07:41:28 +00:00
afterUserFlush ( newUserID )
2017-10-16 07:32:58 +00:00
2019-04-19 08:20:10 +00:00
user , err = c . Users . Get ( newUserID )
2017-10-21 00:27:47 +00:00
recordMustExist ( t , err , "Couldn't find UID #%d" , newUserID )
2019-04-19 08:20:10 +00:00
expectUser ( user , newUserID , "Sam" , c . Config . DefaultGroup , false , false , false , false )
2017-10-16 07:32:58 +00:00
// Permanent ban
duration , _ := time . ParseDuration ( "0" )
// TODO: Attempt a double ban, double activation, and double unban
2021-03-24 12:48:36 +00:00
expectNilErr ( t , user . Ban ( duration , 1 ) )
exf ( user . Group == c . Config . DefaultGroup , "Sam should be in group %d, not %d" , c . Config . DefaultGroup , user . Group )
2018-10-05 07:41:28 +00:00
afterUserFlush ( newUserID )
2017-10-16 07:32:58 +00:00
2019-04-19 08:20:10 +00:00
user , err = c . Users . Get ( newUserID )
2017-10-21 00:27:47 +00:00
recordMustExist ( t , err , "Couldn't find UID #%d" , newUserID )
2019-04-19 08:20:10 +00:00
expectUser ( user , newUserID , "Sam" , c . BanGroup , false , false , false , true )
2017-10-16 07:32:58 +00:00
// TODO: Do tests against the scheduled updates table and the task system to make sure the ban exists there and gets revoked when it should
2021-03-24 12:48:36 +00:00
expectNilErr ( t , user . Unban ( ) )
2019-04-19 08:20:10 +00:00
expectIntToBeX ( t , user . Group , c . BanGroup , "Sam should still be in the ban group in this copy" )
2018-10-05 07:41:28 +00:00
afterUserFlush ( newUserID )
2017-10-16 07:32:58 +00:00
2019-04-19 08:20:10 +00:00
user , err = c . Users . Get ( newUserID )
2017-10-21 00:27:47 +00:00
recordMustExist ( t , err , "Couldn't find UID #%d" , newUserID )
2019-04-19 08:20:10 +00:00
expectUser ( user , newUserID , "Sam" , c . Config . DefaultGroup , false , false , false , false )
2017-10-21 00:27:47 +00:00
2019-10-06 00:34:09 +00:00
reportsForumID := 1 // TODO: Use the constant in common?
generalForumID := 2
2017-10-21 00:27:47 +00:00
dummyResponseRecorder := httptest . NewRecorder ( )
bytesBuffer := bytes . NewBuffer ( [ ] byte ( "" ) )
2018-06-06 00:21:22 +00:00
dummyRequest1 := httptest . NewRequest ( "" , "/forum/" + strconv . Itoa ( reportsForumID ) , bytesBuffer )
dummyRequest2 := httptest . NewRequest ( "" , "/forum/" + strconv . Itoa ( generalForumID ) , bytesBuffer )
2019-04-19 08:20:10 +00:00
var user2 * c . User
2018-10-04 04:59:14 +00:00
2020-02-04 11:47:03 +00:00
changeGroupTest := func ( oldGroup , newGroup int ) {
2021-03-24 12:48:36 +00:00
expectNilErr ( t , user . ChangeGroup ( newGroup ) )
2018-10-04 04:59:14 +00:00
// ! I don't think ChangeGroup should be changing the value of user... Investigate this.
2021-03-24 12:48:36 +00:00
ex ( oldGroup == user . Group , "Someone's mutated this pointer elsewhere" )
2018-10-04 04:59:14 +00:00
2019-04-19 08:20:10 +00:00
user , err = c . Users . Get ( newUserID )
2018-10-04 04:59:14 +00:00
recordMustExist ( t , err , "Couldn't find UID #%d" , newUserID )
2019-04-19 08:20:10 +00:00
user2 = c . BlankUser ( )
2018-10-04 04:59:14 +00:00
* user2 = * user
}
2017-10-21 00:27:47 +00:00
2020-02-04 11:47:03 +00:00
changeGroupTest2 := func ( rank string , firstShouldBe , secondShouldBe bool ) {
2021-05-02 08:47:19 +00:00
head , e := c . UserCheck ( dummyResponseRecorder , dummyRequest1 , user )
if e != nil {
t . Fatal ( e )
2018-11-12 09:23:36 +00:00
}
2021-05-02 08:47:19 +00:00
head2 , e := c . UserCheck ( dummyResponseRecorder , dummyRequest2 , user2 )
if e != nil {
t . Fatal ( e )
2018-11-12 09:23:36 +00:00
}
2019-04-19 08:20:10 +00:00
ferr := c . ForumUserCheck ( head , dummyResponseRecorder , dummyRequest1 , user , reportsForumID )
2021-03-24 12:48:36 +00:00
ex ( ferr == nil , "There shouldn't be any errors in forumUserCheck" )
ex ( user . Perms . ViewTopic == firstShouldBe , rank + " should be able to access the reports forum" )
2019-04-19 08:20:10 +00:00
ferr = c . ForumUserCheck ( head2 , dummyResponseRecorder , dummyRequest2 , user2 , generalForumID )
2021-03-24 12:48:36 +00:00
ex ( ferr == nil , "There shouldn't be any errors in forumUserCheck" )
ex ( user2 . Perms . ViewTopic == secondShouldBe , "Sam should be able to access the general forum" )
2018-10-04 04:59:14 +00:00
}
2017-10-21 00:27:47 +00:00
2019-04-19 08:20:10 +00:00
changeGroupTest ( c . Config . DefaultGroup , 1 )
2018-10-04 02:58:01 +00:00
expectUser ( user , newUserID , "Sam" , 1 , false , true , true , false )
2018-10-04 04:59:14 +00:00
changeGroupTest2 ( "Admins" , true , true )
2017-10-21 00:27:47 +00:00
2018-10-04 04:59:14 +00:00
changeGroupTest ( 1 , 2 )
2018-10-04 02:58:01 +00:00
expectUser ( user , newUserID , "Sam" , 2 , false , false , true , false )
2018-10-04 04:59:14 +00:00
changeGroupTest2 ( "Mods" , true , true )
2017-10-21 00:27:47 +00:00
2018-10-04 04:59:14 +00:00
changeGroupTest ( 2 , 3 )
2018-10-04 02:58:01 +00:00
expectUser ( user , newUserID , "Sam" , 3 , false , false , false , false )
2018-10-04 04:59:14 +00:00
changeGroupTest2 ( "Members" , false , true )
2021-03-24 12:48:36 +00:00
ex ( user . Perms . ViewTopic != user2 . Perms . ViewTopic , "user.Perms.ViewTopic and user2.Perms.ViewTopic should never match" )
2017-10-21 00:27:47 +00:00
2018-10-04 04:59:14 +00:00
changeGroupTest ( 3 , 4 )
2018-10-04 02:58:01 +00:00
expectUser ( user , newUserID , "Sam" , 4 , false , false , false , true )
2018-10-04 04:59:14 +00:00
changeGroupTest2 ( "Members" , false , true )
2017-10-21 00:27:47 +00:00
2018-10-04 04:59:14 +00:00
changeGroupTest ( 4 , 5 )
2018-10-04 02:58:01 +00:00
expectUser ( user , newUserID , "Sam" , 5 , false , false , false , false )
2018-10-04 04:59:14 +00:00
changeGroupTest2 ( "Members" , false , true )
2017-10-21 00:27:47 +00:00
2018-10-04 04:59:14 +00:00
changeGroupTest ( 5 , 6 )
2018-10-04 02:58:01 +00:00
expectUser ( user , newUserID , "Sam" , 6 , false , false , false , false )
2018-10-04 04:59:14 +00:00
changeGroupTest2 ( "Members" , false , true )
2017-10-21 00:27:47 +00:00
2019-04-19 08:20:10 +00:00
err = user . ChangeGroup ( c . Config . DefaultGroup )
2017-10-21 00:27:47 +00:00
expectNilErr ( t , err )
2021-03-24 12:48:36 +00:00
ex ( user . Group == 6 , "Someone's mutated this pointer elsewhere" )
2017-10-16 07:32:58 +00:00
2021-05-02 08:47:19 +00:00
exf ( user . LastIP == "" , "user.LastIP should be blank not %s" , user . LastIP )
expectNilErr ( t , user . UpdateIP ( "::1" ) )
user , err = c . Users . Get ( newUserID )
recordMustExist ( t , err , "Couldn't find UID #%d" , newUserID )
exf ( user . LastIP == "::1" , "user.LastIP should be %s not %s" , "::1" , user . LastIP )
expectNilErr ( t , c . Users . ClearLastIPs ( ) )
expectNilErr ( t , c . Users . Reload ( newUserID ) )
user , err = c . Users . Get ( newUserID )
recordMustExist ( t , err , "Couldn't find UID #%d" , newUserID )
exf ( user . LastIP == "" , "user.LastIP should be blank not %s" , user . LastIP )
2021-03-02 07:22:32 +00:00
expectNilErr ( t , user . Delete ( ) )
2021-03-24 12:48:36 +00:00
exf ( ! c . Users . Exists ( newUserID ) , "UID #%d should no longer exist" , newUserID )
2018-10-05 07:41:28 +00:00
afterUserFlush ( newUserID )
2021-03-02 07:22:32 +00:00
expectIntToBeX ( t , c . Users . Count ( ) , 1 , "The number of users should be 1, not %d" )
searchUser ( "Sam" , "sam@localhost.loc" , 0 , 0 )
// TODO: CountSearch gid test
2017-10-16 07:32:58 +00:00
2019-04-19 08:20:10 +00:00
_ , err = c . Users . Get ( newUserID )
2017-10-21 00:27:47 +00:00
recordMustNotExist ( t , err , "UID #%d shouldn't exist" , newUserID )
2017-11-05 01:04:57 +00:00
2018-10-04 02:58:01 +00:00
// And a unicode test, even though I doubt it'll fail
2019-04-19 08:20:10 +00:00
uid , err = c . Users . Create ( "サム" , "😀😀😀" , "sam@localhost.loc" , awaitingActivation , false )
2018-10-04 02:58:01 +00:00
expectNilErr ( t , err )
2021-03-24 12:48:36 +00:00
exf ( uid == newUserID + 1 , "The UID of the new user should be %d" , newUserID + 1 )
exf ( c . Users . Exists ( newUserID + 1 ) , "UID #%d should exist" , newUserID + 1 )
2018-10-04 02:58:01 +00:00
2019-04-19 08:20:10 +00:00
user , err = c . Users . Get ( newUserID + 1 )
2018-10-04 02:58:01 +00:00
recordMustExist ( t , err , "Couldn't find UID #%d" , newUserID + 1 )
expectUser ( user , newUserID + 1 , "サム" , 5 , false , false , false , false )
2021-03-24 12:48:36 +00:00
expectNilErr ( t , user . Delete ( ) )
exf ( ! c . Users . Exists ( newUserID + 1 ) , "UID #%d should no longer exist" , newUserID + 1 )
2018-10-04 02:58:01 +00:00
2018-10-04 04:59:14 +00:00
// MySQL utf8mb4 username test
2019-04-19 08:20:10 +00:00
uid , err = c . Users . Create ( "😀😀😀" , "😀😀😀" , "sam@localhost.loc" , awaitingActivation , false )
2018-10-04 04:59:14 +00:00
expectNilErr ( t , err )
2021-03-24 12:48:36 +00:00
exf ( uid == newUserID + 2 , "The UID of the new user should be %d" , newUserID + 2 )
exf ( c . Users . Exists ( newUserID + 2 ) , "UID #%d should exist" , newUserID + 2 )
2018-10-04 04:59:14 +00:00
2019-04-19 08:20:10 +00:00
user , err = c . Users . Get ( newUserID + 2 )
2018-10-04 04:59:14 +00:00
recordMustExist ( t , err , "Couldn't find UID #%d" , newUserID + 1 )
expectUser ( user , newUserID + 2 , "😀😀😀" , 5 , false , false , false , false )
2021-03-24 12:48:36 +00:00
expectNilErr ( t , user . Delete ( ) )
exf ( ! c . Users . Exists ( newUserID + 2 ) , "UID #%d should no longer exist" , newUserID + 2 )
2018-10-04 04:59:14 +00:00
2018-10-04 02:58:01 +00:00
// TODO: Add unicode login tests somewhere? Probably with the rest of the auth tests
2017-11-05 01:04:57 +00:00
// TODO: Add tests for the Cache* methods
2017-10-21 00:27:47 +00:00
}
// TODO: Add an error message to this?
func expectNilErr ( t * testing . T , item error ) {
if item != nil {
debug . PrintStack ( )
t . Fatal ( item )
}
2017-10-16 07:32:58 +00:00
}
2020-02-04 11:47:03 +00:00
func expectIntToBeX ( t * testing . T , item , expect int , errmsg string ) {
2017-10-16 07:32:58 +00:00
if item != expect {
debug . PrintStack ( )
t . Fatalf ( errmsg , item )
}
}
func expect ( t * testing . T , item bool , errmsg string ) {
if ! item {
debug . PrintStack ( )
2017-10-21 00:27:47 +00:00
t . Fatal ( errmsg )
2017-09-15 22:20:01 +00:00
}
}
2021-02-27 06:13:03 +00:00
func expectf ( t * testing . T , item bool , errmsg string , args ... interface { } ) {
if ! item {
debug . PrintStack ( )
t . Fatalf ( errmsg , args ... )
}
}
2021-03-24 12:48:36 +00:00
func exp ( t * testing . T ) func ( bool , string ) {
return func ( val bool , errmsg string ) {
if ! val {
debug . PrintStack ( )
t . Fatal ( errmsg )
}
}
}
func expf ( t * testing . T ) func ( bool , string , ... interface { } ) {
return func ( val bool , errmsg string , params ... interface { } ) {
if ! val {
debug . PrintStack ( )
t . Fatalf ( errmsg , params ... )
}
}
}
2017-11-05 01:04:57 +00:00
func TestPermsMiddleware ( t * testing . T ) {
2018-11-01 06:43:56 +00:00
miscinit ( t )
2019-04-19 08:20:10 +00:00
if ! c . PluginsInited {
c . InitPlugins ( )
2017-11-05 01:04:57 +00:00
}
dummyResponseRecorder := httptest . NewRecorder ( )
bytesBuffer := bytes . NewBuffer ( [ ] byte ( "" ) )
dummyRequest := httptest . NewRequest ( "" , "/forum/1" , bytesBuffer )
2019-04-19 08:20:10 +00:00
user := c . BlankUser ( )
2021-03-24 12:48:36 +00:00
ex := exp ( t )
2017-11-05 01:04:57 +00:00
2021-03-24 12:48:36 +00:00
f := func ( ff func ( w http . ResponseWriter , r * http . Request , u * c . User ) c . RouteError ) bool {
ferr := ff ( dummyResponseRecorder , dummyRequest , user )
return ferr == nil
}
2017-11-05 01:04:57 +00:00
2021-03-24 12:48:36 +00:00
ex ( ! f ( c . SuperModOnly ) , "Blank users shouldn't be supermods" )
2017-11-05 01:04:57 +00:00
user . IsSuperMod = false
2021-03-24 12:48:36 +00:00
ex ( ! f ( c . SuperModOnly ) , "Non-supermods shouldn't be allowed through supermod gates" )
2017-11-05 01:04:57 +00:00
user . IsSuperMod = true
2021-03-24 12:48:36 +00:00
ex ( f ( c . SuperModOnly ) , "Supermods should be allowed through supermod gates" )
2017-11-05 01:04:57 +00:00
// TODO: Loop over the Control Panel routes and make sure only supermods can get in
2017-11-05 09:55:34 +00:00
2019-04-19 08:20:10 +00:00
user = c . BlankUser ( )
2017-11-05 09:55:34 +00:00
2021-03-24 12:48:36 +00:00
ex ( ! f ( c . MemberOnly ) , "Blank users shouldn't be considered loggedin" )
2017-11-05 09:55:34 +00:00
user . Loggedin = false
2021-03-24 12:48:36 +00:00
ex ( ! f ( c . MemberOnly ) , "Guests shouldn't be able to access member areas" )
2017-11-05 09:55:34 +00:00
user . Loggedin = true
2021-03-24 12:48:36 +00:00
ex ( f ( c . MemberOnly ) , "Logged in users should be able to access member areas" )
2017-11-05 09:55:34 +00:00
// TODO: Loop over the /user/ routes and make sure only members can access the ones other than /user/username
2017-12-19 03:53:13 +00:00
2019-04-19 08:20:10 +00:00
user = c . BlankUser ( )
2017-12-19 03:53:13 +00:00
2021-03-24 12:48:36 +00:00
ex ( ! f ( c . AdminOnly ) , "Blank users shouldn't be considered admins" )
user . IsAdmin = false
ex ( ! f ( c . AdminOnly ) , "Non-admins shouldn't be able to access admin areas" )
user . IsAdmin = true
ex ( f ( c . AdminOnly ) , "Admins should be able to access admin areas" )
2017-12-19 03:53:13 +00:00
2021-03-24 12:48:36 +00:00
user = c . BlankUser ( )
2017-12-19 03:53:13 +00:00
2021-03-24 12:48:36 +00:00
ex ( ! f ( c . SuperAdminOnly ) , "Blank users shouldn't be considered super admins" )
user . IsSuperAdmin = false
ex ( ! f ( c . SuperAdminOnly ) , "Non-super admins shouldn't be allowed through the super admin gate" )
2017-12-19 03:53:13 +00:00
user . IsSuperAdmin = true
2021-03-24 12:48:36 +00:00
ex ( f ( c . SuperAdminOnly ) , "Super admins should be allowed through super admin gates" )
2017-12-19 03:53:13 +00:00
// TODO: Make sure only super admins can access the backups route
//dummyResponseRecorder = httptest.NewRecorder()
//bytesBuffer = bytes.NewBuffer([]byte(""))
//dummyRequest = httptest.NewRequest("", "/panel/backups/", bytesBuffer)
2017-11-05 01:04:57 +00:00
}
2017-09-15 22:20:01 +00:00
func TestTopicStore ( t * testing . T ) {
2018-11-01 06:43:56 +00:00
miscinit ( t )
2019-04-19 08:20:10 +00:00
if ! c . PluginsInited {
c . InitPlugins ( )
2017-09-15 22:20:01 +00:00
}
2017-11-02 13:35:19 +00:00
var err error
2019-04-19 08:20:10 +00:00
tcache := c . NewMemoryTopicCache ( c . Config . TopicCacheCapacity )
c . Topics , err = c . NewDefaultTopicStore ( tcache )
2017-11-02 13:35:19 +00:00
expectNilErr ( t , err )
2020-01-04 05:30:25 +00:00
c . Config . DisablePostIP = false
topicStoreTest ( t , 2 , "::1" )
c . Config . DisablePostIP = true
2020-02-04 11:47:03 +00:00
topicStoreTest ( t , 3 , "" )
2020-01-04 05:30:25 +00:00
2019-04-19 08:20:10 +00:00
c . Topics , err = c . NewDefaultTopicStore ( nil )
2017-11-02 13:35:19 +00:00
expectNilErr ( t , err )
2020-01-04 05:30:25 +00:00
c . Config . DisablePostIP = false
topicStoreTest ( t , 4 , "::1" )
c . Config . DisablePostIP = true
2020-02-04 11:47:03 +00:00
topicStoreTest ( t , 5 , "" )
2017-09-15 22:20:01 +00:00
}
2020-01-04 05:30:25 +00:00
func topicStoreTest ( t * testing . T , newID int , ip string ) {
2019-04-19 08:20:10 +00:00
var topic * c . Topic
2017-09-15 22:20:01 +00:00
var err error
2021-05-02 08:47:19 +00:00
ex , exf := exp ( t ) , expf ( t )
2017-09-15 22:20:01 +00:00
2019-04-19 08:20:10 +00:00
_ , err = c . Topics . Get ( - 1 )
2017-10-21 00:27:47 +00:00
recordMustNotExist ( t , err , "TID #-1 shouldn't exist" )
2019-04-19 08:20:10 +00:00
_ , err = c . Topics . Get ( 0 )
2017-10-21 00:27:47 +00:00
recordMustNotExist ( t , err , "TID #0 shouldn't exist" )
2017-09-15 22:20:01 +00:00
2019-04-19 08:20:10 +00:00
topic , err = c . Topics . Get ( 1 )
2017-10-16 07:32:58 +00:00
recordMustExist ( t , err , "Couldn't find TID #1" )
2021-05-02 08:47:19 +00:00
exf ( topic . ID == 1 , "topic.ID does not match the requested TID. Got '%d' instead." , topic . ID )
2017-09-03 04:50:31 +00:00
2017-09-15 22:20:01 +00:00
// TODO: Add BulkGetMap() to the TopicStore
2021-05-02 08:47:19 +00:00
ex ( ! c . Topics . Exists ( - 1 ) , "TID #-1 shouldn't exist" )
ex ( ! c . Topics . Exists ( 0 ) , "TID #0 shouldn't exist" )
ex ( c . Topics . Exists ( 1 ) , "TID #1 should exist" )
2017-09-03 04:50:31 +00:00
2019-06-01 12:31:48 +00:00
count := c . Topics . Count ( )
2021-05-02 08:47:19 +00:00
exf ( count == 1 , "Global count for topics should be 1, not %d" , count )
2018-10-07 10:46:49 +00:00
2020-01-02 21:52:41 +00:00
//Create(fid int, topicName string, content string, uid int, ip string) (tid int, err error)
2020-01-04 05:30:25 +00:00
tid , err := c . Topics . Create ( 2 , "Test Topic" , "Topic Content" , 1 , ip )
2018-10-07 10:46:49 +00:00
expectNilErr ( t , err )
2021-05-02 08:47:19 +00:00
exf ( tid == newID , "TID for the new topic should be %d, not %d" , newID , tid )
exf ( c . Topics . Exists ( newID ) , "TID #%d should exist" , newID )
2018-10-07 10:46:49 +00:00
2019-06-01 12:31:48 +00:00
count = c . Topics . Count ( )
2021-05-02 08:47:19 +00:00
exf ( count == 2 , "Global count for topics should be 2, not %d" , count )
2018-10-07 10:46:49 +00:00
2019-10-06 00:34:09 +00:00
iFrag := func ( cond bool ) string {
2018-10-07 10:46:49 +00:00
if ! cond {
return "n't"
}
return ""
}
2020-02-09 10:00:08 +00:00
testTopic := func ( tid int , title , content string , createdBy int , ip string , parentID int , isClosed , sticky bool ) {
2019-04-19 08:20:10 +00:00
topic , err = c . Topics . Get ( tid )
2018-10-07 10:46:49 +00:00
recordMustExist ( t , err , fmt . Sprintf ( "Couldn't find TID #%d" , tid ) )
2021-05-02 08:47:19 +00:00
exf ( topic . ID == tid , "topic.ID does not match the requested TID. Got '%d' instead." , topic . ID )
exf ( topic . GetID ( ) == tid , "topic.ID does not match the requested TID. Got '%d' instead." , topic . GetID ( ) )
exf ( topic . Title == title , "The topic's name should be '%s', not %s" , title , topic . Title )
exf ( topic . Content == content , "The topic's body should be '%s', not %s" , content , topic . Content )
exf ( topic . CreatedBy == createdBy , "The topic's creator should be %d, not %d" , createdBy , topic . CreatedBy )
exf ( topic . IP == ip , "The topic's IP should be '%s', not %s" , ip , topic . IP )
exf ( topic . ParentID == parentID , "The topic's parent forum should be %d, not %d" , parentID , topic . ParentID )
exf ( topic . IsClosed == isClosed , "This topic should%s be locked" , iFrag ( topic . IsClosed ) )
exf ( topic . Sticky == sticky , "This topic should%s be sticky" , iFrag ( topic . Sticky ) )
exf ( topic . GetTable ( ) == "topics" , "The topic's table should be 'topics', not %s" , topic . GetTable ( ) )
2018-10-07 10:46:49 +00:00
}
2020-02-19 05:12:59 +00:00
tc := c . Topics . GetCache ( )
2019-10-06 00:34:09 +00:00
shouldNotBeIn := func ( tid int ) {
2020-02-19 05:12:59 +00:00
if tc != nil {
_ , err = tc . Get ( tid )
2018-10-07 10:46:49 +00:00
recordMustNotExist ( t , err , "Topic cache should be empty" )
}
Added the Social Groups plugin. This is still under construction.
Made a few improvements to the ForumStore, including bringing it's API closer in line with the other datastores, adding stubs for future subforum functionality, and improving efficiency in a few places.
The auth interface now handles all the authentication stuff.
Renamed the debug config variable to debug_mode.
Added the PluginPerms API.
Internal Errors will now dump the stack trace in the console.
Added support for installable plugins.
Refactored the routing logic so that the router now handles the common PreRoute logic(exc. /static/)
Added the CreateTable method to the query generator. It might need some tweaking to better support other database systems.
Added the same CreateTable method to the query builder.
Began work on PostgreSQL support.
Added the string-string hook type
Added the pre_render hook type.
Added the ParentID and ParentType fields to forums.
Added the get_forum_url_prefix function.
Added a more generic build_slug function.
Added the get_topic_url_prefix function.
Added the override_perms and override_forum_perms functions for bulk setting and unsetting permissions.
Added more ExtData fields in a few structs and removed them on the Perms struct as the PluginPerms API supersedes them there.
Plugins can now see the router instance.
The plugin initialisation handlers can now throw errors.
Plugins are now initialised after all the forum's subsystems are.
Refactored the unit test logic. For instance, we now use the proper .Log method rather than fmt.Println in many cases.
Sorry, we'll have to break Github's generated file detection, as the build instructions aren't working, unless I put them at the top, and they're far, far more important than getting Github to recognise the generated code as generated code.
Fixed an issue with mysql.go's _init_database() overwriting the dbpassword variable. Not a huge issue, but it is a "gotcha" for those not expecting a ':' at the start.
Fixed an issue with forum creation where the forum permissions didn't get cached.
Fixed a bug in plugin_bbcode where negative numbers in rand would crash Gosora.
Made the outputs of plugin_markdown and plugin_bbcode more compliant with the tests.
Revamped the phrase system to make it easier for us to add language pack related features in the future.
Added the WidgetMenu widget type.
Revamped the theme again. I'm experimenting to see which approach I like most.
- Excuse the little W3C rage. Some things about CSS drive me crazy :p
Tests:
Added 22 bbcode_full_parse tests.
Added 19 bbcode_regex_parse tests.
Added 27 markdown_parse tests.
Added four UserStore tests. More to come when the test database functionality is added.
Added 18 name_to_slug tests.
Hooks:
Added the pre_render hook.
Added the pre_render_forum_list hook.
Added the pre_render_view_forum hook.
Added the pre_render_topic_list hook.
Added the pre_render_view_topic hook.
Added the pre_render_profile hook.
Added the pre_render_custom_page hook.
Added the pre_render_overview hook.
Added the pre_render_create_topic hook.
Added the pre_render_account_own_edit_critical hook.
Added the pre_render_account_own_edit_avatar hook.
Added the pre_render_account_own_edit_username hook.
Added the pre_render_account_own_edit_email hook.
Added the pre_render_login hook.
Added the pre_render_register hook.
Added the pre_render_ban hook.
Added the pre_render_panel_dashboard hook.
Added the pre_render_panel_forums hook.
Added the pre_render_panel_delete_forum hook.
Added the pre_render_panel_edit_forum hook.
Added the pre_render_panel_settings hook.
Added the pre_render_panel_setting hook.
Added the pre_render_panel_plugins hook.
Added the pre_render_panel_users hook.
Added the pre_render_panel_edit_user hook.
Added the pre_render_panel_groups hook.
Added the pre_render_panel_edit_group hook.
Added the pre_render_panel_edit_group_perms hook.
Added the pre_render_panel_themes hook.
Added the pre_render_panel_mod_log hook.
Added the pre_render_error hook.
Added the pre_render_security_error hook.
Added the create_group_preappend hook.
Added the intercept_build_widgets hook.
Added the simple_forum_check_pre_perms hook.
Added the forum_check_pre_perms hook.
2017-07-09 12:06:04 +00:00
}
2020-02-19 05:12:59 +00:00
if tc != nil {
_ , err = tc . Get ( newID )
2018-10-07 10:46:49 +00:00
expectNilErr ( t , err )
}
2020-01-04 05:30:25 +00:00
testTopic ( newID , "Test Topic" , "Topic Content" , 1 , ip , 2 , false , false )
2018-10-07 10:46:49 +00:00
expectNilErr ( t , topic . Lock ( ) )
shouldNotBeIn ( newID )
2020-01-04 05:30:25 +00:00
testTopic ( newID , "Test Topic" , "Topic Content" , 1 , ip , 2 , true , false )
2018-10-07 10:46:49 +00:00
expectNilErr ( t , topic . Unlock ( ) )
shouldNotBeIn ( newID )
2020-01-04 05:30:25 +00:00
testTopic ( newID , "Test Topic" , "Topic Content" , 1 , ip , 2 , false , false )
2018-10-07 10:46:49 +00:00
expectNilErr ( t , topic . Stick ( ) )
shouldNotBeIn ( newID )
2020-01-04 05:30:25 +00:00
testTopic ( newID , "Test Topic" , "Topic Content" , 1 , ip , 2 , false , true )
2018-10-07 10:46:49 +00:00
expectNilErr ( t , topic . Unstick ( ) )
shouldNotBeIn ( newID )
2020-01-04 05:30:25 +00:00
testTopic ( newID , "Test Topic" , "Topic Content" , 1 , ip , 2 , false , false )
2018-10-07 10:46:49 +00:00
expectNilErr ( t , topic . MoveTo ( 1 ) )
shouldNotBeIn ( newID )
2020-01-04 05:30:25 +00:00
testTopic ( newID , "Test Topic" , "Topic Content" , 1 , ip , 1 , false , false )
2018-10-07 10:46:49 +00:00
// TODO: Add more tests for more *Topic methods
2021-05-02 08:47:19 +00:00
expectNilErr ( t , c . Topics . ClearIPs ( ) )
expectNilErr ( t , c . Topics . Reload ( topic . ID ) )
testTopic ( newID , "Test Topic" , "Topic Content" , 1 , "" , 1 , false , false )
// TODO: Add more tests for more *Topic methods
2018-10-07 10:46:49 +00:00
expectNilErr ( t , topic . Delete ( ) )
shouldNotBeIn ( newID )
2019-04-19 08:20:10 +00:00
_ , err = c . Topics . Get ( newID )
2018-10-07 10:46:49 +00:00
recordMustNotExist ( t , err , fmt . Sprintf ( "TID #%d shouldn't exist" , newID ) )
2021-05-02 08:47:19 +00:00
exf ( ! c . Topics . Exists ( newID ) , "TID #%d shouldn't exist" , newID )
2017-11-06 16:24:45 +00:00
// TODO: Test topic creation and retrieving that created topic plus reload and inspecting the cache
2017-07-12 11:05:18 +00:00
}
func TestForumStore ( t * testing . T ) {
2018-11-01 06:43:56 +00:00
miscinit ( t )
2019-04-19 08:20:10 +00:00
if ! c . PluginsInited {
c . InitPlugins ( )
2017-09-03 04:50:31 +00:00
}
2021-03-24 12:48:36 +00:00
ex , exf := exp ( t ) , expf ( t )
2019-05-27 09:00:40 +00:00
// TODO: Test ForumStore.Reload
2017-09-03 04:50:31 +00:00
2019-05-18 12:17:50 +00:00
fcache , ok := c . Forums . ( c . ForumCache )
2021-03-24 12:48:36 +00:00
ex ( ok , "Unable to cast ForumStore to ForumCache" )
ex ( c . Forums . Count ( ) == 2 , "The forumstore global count should be 2" )
ex ( fcache . Length ( ) == 2 , "The forum cache length should be 2" )
2019-05-18 10:31:51 +00:00
2019-04-19 08:20:10 +00:00
_ , err := c . Forums . Get ( - 1 )
2017-10-21 00:27:47 +00:00
recordMustNotExist ( t , err , "FID #-1 shouldn't exist" )
2019-04-19 08:20:10 +00:00
_ , err = c . Forums . Get ( 0 )
2017-10-21 00:27:47 +00:00
recordMustNotExist ( t , err , "FID #0 shouldn't exist" )
2017-09-03 04:50:31 +00:00
2021-05-02 08:47:19 +00:00
testForum := func ( f * c . Forum , fid int , name string , active bool , desc string ) {
exf ( f . ID == fid , "forum.ID should be %d, not %d." , fid , f . ID )
// TODO: Check the preset and forum permissions
exf ( f . Name == name , "forum.Name should be %s, not %s" , name , f . Name )
str := ""
if ! active {
str = "n't"
}
exf ( f . Active == active , "The reports forum should%s be active" , str )
exf ( f . Desc == desc , "forum.Desc should be '%s' not '%s'" , desc , f . Desc )
}
2019-04-19 08:20:10 +00:00
forum , err := c . Forums . Get ( 1 )
2017-10-16 07:32:58 +00:00
recordMustExist ( t , err , "Couldn't find FID #1" )
2019-10-06 00:34:09 +00:00
expectDesc := "All the reports go here"
2021-05-02 08:47:19 +00:00
testForum ( forum , 1 , "Reports" , false , expectDesc )
2019-05-18 12:17:50 +00:00
forum , err = c . Forums . BypassGet ( 1 )
recordMustExist ( t , err , "Couldn't find FID #1" )
2017-09-03 04:50:31 +00:00
2019-04-19 08:20:10 +00:00
forum , err = c . Forums . Get ( 2 )
2018-10-05 07:41:28 +00:00
recordMustExist ( t , err , "Couldn't find FID #2" )
2019-05-18 12:17:50 +00:00
forum , err = c . Forums . BypassGet ( 2 )
recordMustExist ( t , err , "Couldn't find FID #2" )
2017-11-05 01:04:57 +00:00
expectDesc = "A place for general discussions which don't fit elsewhere"
2021-05-02 08:47:19 +00:00
testForum ( forum , 2 , "General" , true , expectDesc )
2017-09-15 22:20:01 +00:00
2019-05-27 09:00:40 +00:00
// Forum reload test, kind of hacky but gets the job done
/ *
2019-10-06 01:49:31 +00:00
CacheGet ( id int ) ( * Forum , error )
CacheSet ( forum * Forum ) error
2019-05-27 09:00:40 +00:00
* /
2021-03-24 12:48:36 +00:00
ex ( ok , "ForumCache should be available" )
2019-05-27 09:00:40 +00:00
forum . Name = "nanana"
fcache . CacheSet ( forum )
forum , err = c . Forums . Get ( 2 )
recordMustExist ( t , err , "Couldn't find FID #2" )
2021-03-24 12:48:36 +00:00
exf ( forum . Name == "nanana" , "The faux name should be nanana not %s" , forum . Name )
2019-10-06 01:49:31 +00:00
expectNilErr ( t , c . Forums . Reload ( 2 ) )
2019-05-27 09:00:40 +00:00
forum , err = c . Forums . Get ( 2 )
recordMustExist ( t , err , "Couldn't find FID #2" )
2021-03-24 12:48:36 +00:00
exf ( forum . Name == "General" , "The proper name should be 2 not %s" , forum . Name )
2019-05-27 09:00:40 +00:00
2021-03-24 12:48:36 +00:00
ex ( ! c . Forums . Exists ( - 1 ) , "FID #-1 shouldn't exist" )
ex ( ! c . Forums . Exists ( 0 ) , "FID #0 shouldn't exist" )
ex ( c . Forums . Exists ( 1 ) , "FID #1 should exist" )
ex ( c . Forums . Exists ( 2 ) , "FID #2 should exist" )
ex ( ! c . Forums . Exists ( 3 ) , "FID #3 shouldn't exist" )
2019-05-18 10:31:51 +00:00
_ , err = c . Forums . Create ( "" , "" , true , "all" )
2021-03-24 12:48:36 +00:00
ex ( err != nil , "A forum shouldn't be successfully created, if it has a blank name" )
2017-11-05 01:04:57 +00:00
2019-04-19 08:20:10 +00:00
fid , err := c . Forums . Create ( "Test Forum" , "" , true , "all" )
2017-12-25 06:12:19 +00:00
expectNilErr ( t , err )
2021-03-24 12:48:36 +00:00
ex ( fid == 3 , "The first forum we create should have an ID of 3" )
ex ( c . Forums . Exists ( 3 ) , "FID #2 should exist" )
2019-05-18 10:31:51 +00:00
2021-03-24 12:48:36 +00:00
ex ( c . Forums . Count ( ) == 3 , "The forumstore global count should be 3" )
ex ( fcache . Length ( ) == 3 , "The forum cache length should be 3" )
2017-12-25 06:12:19 +00:00
2019-04-19 08:20:10 +00:00
forum , err = c . Forums . Get ( 3 )
2017-12-25 06:12:19 +00:00
recordMustExist ( t , err , "Couldn't find FID #3" )
2019-05-18 12:17:50 +00:00
forum , err = c . Forums . BypassGet ( 3 )
recordMustExist ( t , err , "Couldn't find FID #3" )
2021-05-02 08:47:19 +00:00
testForum ( forum , 3 , "Test Forum" , true , "" )
2017-12-25 06:12:19 +00:00
// TODO: More forum creation tests
2019-05-18 10:31:51 +00:00
expectNilErr ( t , c . Forums . Delete ( 3 ) )
2021-03-24 12:48:36 +00:00
ex ( forum . ID == 3 , "forum pointer shenanigans" )
ex ( c . Forums . Count ( ) == 2 , "The forumstore global count should be 2" )
ex ( fcache . Length ( ) == 2 , "The forum cache length should be 2" )
ex ( ! c . Forums . Exists ( 3 ) , "FID #3 shouldn't exist after being deleted" )
2019-05-18 10:31:51 +00:00
_ , err = c . Forums . Get ( 3 )
recordMustNotExist ( t , err , "FID #3 shouldn't exist after being deleted" )
2019-05-18 12:17:50 +00:00
_ , err = c . Forums . BypassGet ( 3 )
recordMustNotExist ( t , err , "FID #3 shouldn't exist after being deleted" )
2021-03-24 12:48:36 +00:00
ex ( c . Forums . Delete ( c . ReportForumID ) != nil , "The reports forum shouldn't be deletable" )
exf ( c . Forums . Exists ( c . ReportForumID ) , "FID #%d should still exist" , c . ReportForumID )
2019-05-18 12:17:50 +00:00
_ , err = c . Forums . Get ( c . ReportForumID )
2021-03-24 12:48:36 +00:00
exf ( err == nil , "FID #%d should still exist" , c . ReportForumID )
2019-05-18 12:17:50 +00:00
_ , err = c . Forums . BypassGet ( c . ReportForumID )
2021-03-24 12:48:36 +00:00
exf ( err == nil , "FID #%d should still exist" , c . ReportForumID )
2019-05-18 12:17:50 +00:00
eforums := map [ int ] bool { 1 : true , 2 : true }
{
forums , err := c . Forums . GetAll ( )
expectNilErr ( t , err )
found := make ( map [ int ] * c . Forum )
for _ , forum := range forums {
_ , ok := eforums [ forum . ID ]
2021-03-24 12:48:36 +00:00
exf ( ok , "unknown forum #%d in forums" , forum . ID )
2019-05-18 12:17:50 +00:00
found [ forum . ID ] = forum
}
for fid , _ := range eforums {
_ , ok := found [ fid ]
2021-03-24 12:48:36 +00:00
exf ( ok , "unable to find expected forum #%d in forums" , fid )
2019-05-18 12:17:50 +00:00
}
}
{
fids , err := c . Forums . GetAllIDs ( )
expectNilErr ( t , err )
found := make ( map [ int ] bool )
for _ , fid := range fids {
_ , ok := eforums [ fid ]
2021-03-24 12:48:36 +00:00
exf ( ok , "unknown fid #%d in fids" , fid )
2019-05-18 12:17:50 +00:00
found [ fid ] = true
}
for fid , _ := range eforums {
_ , ok := found [ fid ]
2021-03-24 12:48:36 +00:00
exf ( ok , "unable to find expected fid #%d in fids" , fid )
2019-05-18 12:17:50 +00:00
}
}
vforums := map [ int ] bool { 2 : true }
{
forums , err := c . Forums . GetAllVisible ( )
expectNilErr ( t , err )
found := make ( map [ int ] * c . Forum )
for _ , forum := range forums {
_ , ok := vforums [ forum . ID ]
2021-03-24 12:48:36 +00:00
exf ( ok , "unknown forum #%d in forums" , forum . ID )
2019-05-18 12:17:50 +00:00
found [ forum . ID ] = forum
}
for fid , _ := range vforums {
_ , ok := found [ fid ]
2021-03-24 12:48:36 +00:00
exf ( ok , "unable to find expected forum #%d in forums" , fid )
2019-05-18 12:17:50 +00:00
}
}
{
fids , err := c . Forums . GetAllVisibleIDs ( )
expectNilErr ( t , err )
found := make ( map [ int ] bool )
for _ , fid := range fids {
_ , ok := vforums [ fid ]
2021-03-24 12:48:36 +00:00
exf ( ok , "unknown fid #%d in fids" , fid )
2019-05-18 12:17:50 +00:00
found [ fid ] = true
}
for fid , _ := range vforums {
_ , ok := found [ fid ]
2021-03-24 12:48:36 +00:00
exf ( ok , "unable to find expected fid #%d in fids" , fid )
2019-05-18 12:17:50 +00:00
}
}
2019-05-18 10:31:51 +00:00
2021-03-24 12:48:36 +00:00
forum , err = c . Forums . Get ( 2 )
expectNilErr ( t , err )
prevTopicCount := forum . TopicCount
tid , err := c . Topics . Create ( forum . ID , "Forum Meta Test" , "Forum Meta Test" , 1 , "" )
expectNilErr ( t , err )
forum , err = c . Forums . Get ( 2 )
expectNilErr ( t , err )
exf ( forum . TopicCount == ( prevTopicCount + 1 ) , "forum.TopicCount should be %d not %d" , prevTopicCount + 1 , forum . TopicCount )
exf ( forum . LastTopicID == tid , "forum.LastTopicID should be %d not %d" , tid , forum . LastTopicID )
exf ( forum . LastPage == 1 , "forum.LastPage should be %d not %d" , 1 , forum . LastPage )
// TODO: Test topic creation and forum topic metadata
2017-11-05 01:04:57 +00:00
// TODO: Test forum update
2019-05-18 10:31:51 +00:00
// TODO: Other forumstore stuff and forumcache?
2017-11-05 01:04:57 +00:00
}
// TODO: Implement this
func TestForumPermsStore ( t * testing . T ) {
2018-11-01 06:43:56 +00:00
miscinit ( t )
2019-04-19 08:20:10 +00:00
if ! c . PluginsInited {
c . InitPlugins ( )
2017-09-15 22:20:01 +00:00
}
2021-03-24 12:48:36 +00:00
ex := exp ( t )
2019-06-19 03:16:03 +00:00
2020-02-09 10:00:08 +00:00
f := func ( fid , gid int , msg string , inv ... bool ) {
2019-10-06 01:49:31 +00:00
fp , err := c . FPStore . Get ( fid , gid )
2021-03-24 12:48:36 +00:00
if err == ErrNoRows {
fp = c . BlankForumPerms ( )
} else {
expectNilErr ( t , err )
}
2019-10-06 00:34:09 +00:00
vt := fp . ViewTopic
if len ( inv ) > 0 && inv [ 0 ] == true {
vt = ! vt
}
2021-03-24 12:48:36 +00:00
ex ( vt , msg )
2019-10-06 00:34:09 +00:00
}
2019-06-19 03:16:03 +00:00
2021-03-24 12:48:36 +00:00
// TODO: Test reporting
2019-10-06 00:34:09 +00:00
initialState := func ( ) {
2019-10-06 01:49:31 +00:00
f ( 1 , 1 , "admins should be able to see reports" )
f ( 1 , 2 , "mods should be able to see reports" )
f ( 1 , 3 , "members should not be able to see reports" , true )
f ( 1 , 4 , "banned users should not be able to see reports" , true )
f ( 2 , 1 , "admins should be able to see general" )
f ( 2 , 3 , "members should be able to see general" )
f ( 2 , 6 , "guests should be able to see general" )
2019-06-19 03:16:03 +00:00
}
initialState ( )
expectNilErr ( t , c . FPStore . Reload ( 1 ) )
initialState ( )
expectNilErr ( t , c . FPStore . Reload ( 2 ) )
initialState ( )
2021-03-24 12:48:36 +00:00
gid , err := c . Groups . Create ( "FP Test" , "FP Test" , false , false , false )
expectNilErr ( t , err )
fid , err := c . Forums . Create ( "FP Test" , "FP Test" , true , "" )
expectNilErr ( t , err )
u := c . GuestUser . Copy ( )
rt := func ( gid , fid int , shouldSucceed bool ) {
w := httptest . NewRecorder ( )
bytesBuffer := bytes . NewBuffer ( [ ] byte ( "" ) )
sfid := strconv . Itoa ( fid )
req := httptest . NewRequest ( "" , "/forum/" + sfid , bytesBuffer )
u . Group = gid
h , err := c . UserCheck ( w , req , & u )
expectNilErr ( t , err )
rerr := routes . ViewForum ( w , req , & u , h , sfid )
if shouldSucceed {
ex ( rerr == nil , "ViewForum should succeed" )
} else {
ex ( rerr != nil , "ViewForum should not succeed" )
}
}
rt ( 1 , fid , false )
rt ( 2 , fid , false )
rt ( 3 , fid , false )
rt ( 4 , fid , false )
rt ( gid , fid , false )
fp , err := c . FPStore . GetCopy ( fid , gid )
if err == sql . ErrNoRows {
fp = * c . BlankForumPerms ( )
} else if err != nil {
expectNilErr ( t , err )
}
fmt . Printf ( "fp: %+v\n" , fp )
f ( fid , 1 , "admins should not be able to see fp test" , true )
f ( fid , 2 , "mods should not be able to see fp test" , true )
f ( fid , 3 , "members should not be able to see fp test" , true )
f ( fid , 4 , "banned users should not be able to see fp test" , true )
f ( fid , gid , "fp test should not be able to see fp test" , true )
fp . ViewTopic = true
forum , err := c . Forums . Get ( fid )
expectNilErr ( t , err )
expectNilErr ( t , forum . SetPerms ( & fp , "custom" , gid ) )
rt ( 1 , fid , false )
rt ( 2 , fid , false )
rt ( 3 , fid , false )
rt ( 4 , fid , false )
rt ( gid , fid , true )
fp , err = c . FPStore . GetCopy ( fid , gid )
if err == sql . ErrNoRows {
fp = * c . BlankForumPerms ( )
} else if err != nil {
expectNilErr ( t , err )
}
f ( fid , 1 , "admins should not be able to see fp test" , true )
f ( fid , 2 , "mods should not be able to see fp test" , true )
f ( fid , 3 , "members should not be able to see fp test" , true )
f ( fid , 4 , "banned users should not be able to see fp test" , true )
f ( fid , gid , "fp test should be able to see fp test" )
expectNilErr ( t , c . Forums . Delete ( fid ) )
rt ( 1 , fid , false )
rt ( 2 , fid , false )
rt ( 3 , fid , false )
rt ( 4 , fid , false )
rt ( gid , fid , false )
// TODO: Test changing forum permissions
2017-09-15 22:20:01 +00:00
}
2017-11-05 01:04:57 +00:00
// TODO: Test the group permissions
2019-06-27 23:35:34 +00:00
// TODO: Test group.CanSee for forum presets + group perms
2017-09-15 22:20:01 +00:00
func TestGroupStore ( t * testing . T ) {
2018-11-01 06:43:56 +00:00
miscinit ( t )
2019-04-19 08:20:10 +00:00
if ! c . PluginsInited {
c . InitPlugins ( )
2017-09-15 22:20:01 +00:00
}
2021-03-24 12:48:36 +00:00
ex , exf := exp ( t ) , expf ( t )
2017-09-15 22:20:01 +00:00
2019-04-19 08:20:10 +00:00
_ , err := c . Groups . Get ( - 1 )
2017-10-21 00:27:47 +00:00
recordMustNotExist ( t , err , "GID #-1 shouldn't exist" )
2017-09-15 22:20:01 +00:00
2017-10-16 07:32:58 +00:00
// TODO: Refactor the group store to remove GID #0
2020-02-19 10:32:26 +00:00
g , err := c . Groups . Get ( 0 )
2017-10-16 07:32:58 +00:00
recordMustExist ( t , err , "Couldn't find GID #0" )
2021-03-24 12:48:36 +00:00
exf ( g . ID == 0 , "g.ID doesn't not match the requested GID. Got '%d' instead." , g . ID )
exf ( g . Name == "Unknown" , "GID #0 is named '%s' and not 'Unknown'" , g . Name )
2017-09-15 22:20:01 +00:00
2020-02-19 10:32:26 +00:00
g , err = c . Groups . Get ( 1 )
2017-10-16 07:32:58 +00:00
recordMustExist ( t , err , "Couldn't find GID #1" )
2021-03-24 12:48:36 +00:00
exf ( g . ID == 1 , "g.ID doesn't not match the requested GID. Got '%d' instead.'" , g . ID )
ex ( len ( g . CanSee ) > 0 , "g.CanSee should not be zero" )
2017-09-15 22:20:01 +00:00
2021-03-24 12:48:36 +00:00
ex ( ! c . Groups . Exists ( - 1 ) , "GID #-1 shouldn't exist" )
2017-10-21 00:27:47 +00:00
// 0 aka Unknown, for system posts and other oddities
2021-03-24 12:48:36 +00:00
ex ( c . Groups . Exists ( 0 ) , "GID #0 should exist" )
ex ( c . Groups . Exists ( 1 ) , "GID #1 should exist" )
2017-10-21 00:27:47 +00:00
2021-03-24 12:48:36 +00:00
isAdmin , isMod , isBanned := true , true , false
2019-04-19 08:20:10 +00:00
gid , err := c . Groups . Create ( "Testing" , "Test" , isAdmin , isMod , isBanned )
2017-10-21 00:27:47 +00:00
expectNilErr ( t , err )
2021-03-24 12:48:36 +00:00
ex ( c . Groups . Exists ( gid ) , "The group we just made doesn't exist" )
ff := func ( i bool ) string {
if ! i {
return "n't"
}
return ""
}
f := func ( gid int , isBanned , isMod , isAdmin bool ) {
ex ( g . ID == gid , "The group ID should match the requested ID" )
exf ( g . IsAdmin == isAdmin , "This should%s be an admin group" , ff ( isAdmin ) )
exf ( g . IsMod == isMod , "This should%s be a mod group" , ff ( isMod ) )
exf ( g . IsBanned == isBanned , "This should%s be a ban group" , ff ( isBanned ) )
}
2017-10-21 00:27:47 +00:00
2020-02-19 10:32:26 +00:00
g , err = c . Groups . Get ( gid )
2017-10-21 00:27:47 +00:00
expectNilErr ( t , err )
2021-03-24 12:48:36 +00:00
f ( gid , false , true , true )
ex ( len ( g . CanSee ) == 0 , "g.CanSee should be empty" )
2017-10-21 00:27:47 +00:00
2021-03-24 12:48:36 +00:00
isAdmin , isMod , isBanned = false , true , true
2019-04-19 08:20:10 +00:00
gid , err = c . Groups . Create ( "Testing 2" , "Test" , isAdmin , isMod , isBanned )
2017-10-21 00:27:47 +00:00
expectNilErr ( t , err )
2021-03-24 12:48:36 +00:00
ex ( c . Groups . Exists ( gid ) , "The group we just made doesn't exist" )
2017-10-21 00:27:47 +00:00
2020-02-19 10:32:26 +00:00
g , err = c . Groups . Get ( gid )
2017-10-21 00:27:47 +00:00
expectNilErr ( t , err )
2021-03-24 12:48:36 +00:00
f ( gid , false , true , false )
2017-10-21 00:27:47 +00:00
// TODO: Make sure this pointer doesn't change once we refactor the group store to stop updating the pointer
2021-03-24 12:48:36 +00:00
expectNilErr ( t , g . ChangeRank ( false , false , true ) )
2017-10-21 00:27:47 +00:00
2020-02-19 10:32:26 +00:00
g , err = c . Groups . Get ( gid )
2017-10-21 00:27:47 +00:00
expectNilErr ( t , err )
2021-03-24 12:48:36 +00:00
f ( gid , true , false , false )
2017-10-21 00:27:47 +00:00
2021-03-24 12:48:36 +00:00
expectNilErr ( t , g . ChangeRank ( true , true , true ) )
2017-10-21 00:27:47 +00:00
2020-02-19 10:32:26 +00:00
g , err = c . Groups . Get ( gid )
2017-10-21 00:27:47 +00:00
expectNilErr ( t , err )
2021-03-24 12:48:36 +00:00
f ( gid , false , true , true )
ex ( len ( g . CanSee ) == 0 , "len(g.CanSee) should be 0" )
2017-10-21 00:27:47 +00:00
2021-03-24 12:48:36 +00:00
expectNilErr ( t , g . ChangeRank ( false , true , true ) )
2017-10-21 00:27:47 +00:00
2019-06-18 10:02:45 +00:00
forum , err := c . Forums . Get ( 2 )
expectNilErr ( t , err )
forumPerms , err := c . FPStore . GetCopy ( 2 , gid )
if err == sql . ErrNoRows {
forumPerms = * c . BlankForumPerms ( )
} else if err != nil {
expectNilErr ( t , err )
}
forumPerms . ViewTopic = true
err = forum . SetPerms ( & forumPerms , "custom" , gid )
expectNilErr ( t , err )
2020-02-19 10:32:26 +00:00
g , err = c . Groups . Get ( gid )
2017-10-21 00:27:47 +00:00
expectNilErr ( t , err )
2021-03-24 12:48:36 +00:00
f ( gid , false , true , false )
ex ( g . CanSee != nil , "g.CanSee must not be nil" )
ex ( len ( g . CanSee ) == 1 , "len(g.CanSee) should not be one" )
ex ( g . CanSee [ 0 ] == 2 , "g.CanSee[0] should be 2" )
2020-02-19 10:32:26 +00:00
canSee := g . CanSee
2017-10-21 00:27:47 +00:00
2017-11-05 01:04:57 +00:00
// Make sure the data is static
2021-03-24 12:48:36 +00:00
expectNilErr ( t , c . Groups . Reload ( gid ) )
2017-11-05 01:04:57 +00:00
2020-02-19 10:32:26 +00:00
g , err = c . Groups . Get ( gid )
2017-11-05 01:04:57 +00:00
expectNilErr ( t , err )
2021-03-24 12:48:36 +00:00
f ( gid , false , true , false )
2017-11-05 01:04:57 +00:00
2019-06-18 10:02:45 +00:00
// TODO: Don't enforce a specific order here
canSeeTest := func ( a , b [ ] int ) bool {
if ( a == nil ) != ( b == nil ) {
return false
}
if len ( a ) != len ( b ) {
return false
}
for i := range a {
if a [ i ] != b [ i ] {
return false
}
}
return true
}
2021-03-24 12:48:36 +00:00
ex ( canSeeTest ( g . CanSee , canSee ) , "g.CanSee is not being reused" )
2019-06-18 10:02:45 +00:00
2017-10-21 00:27:47 +00:00
// TODO: Test group deletion
2017-11-05 01:04:57 +00:00
// TODO: Test group reload
// TODO: Test group cache set
Added the Social Groups plugin. This is still under construction.
Made a few improvements to the ForumStore, including bringing it's API closer in line with the other datastores, adding stubs for future subforum functionality, and improving efficiency in a few places.
The auth interface now handles all the authentication stuff.
Renamed the debug config variable to debug_mode.
Added the PluginPerms API.
Internal Errors will now dump the stack trace in the console.
Added support for installable plugins.
Refactored the routing logic so that the router now handles the common PreRoute logic(exc. /static/)
Added the CreateTable method to the query generator. It might need some tweaking to better support other database systems.
Added the same CreateTable method to the query builder.
Began work on PostgreSQL support.
Added the string-string hook type
Added the pre_render hook type.
Added the ParentID and ParentType fields to forums.
Added the get_forum_url_prefix function.
Added a more generic build_slug function.
Added the get_topic_url_prefix function.
Added the override_perms and override_forum_perms functions for bulk setting and unsetting permissions.
Added more ExtData fields in a few structs and removed them on the Perms struct as the PluginPerms API supersedes them there.
Plugins can now see the router instance.
The plugin initialisation handlers can now throw errors.
Plugins are now initialised after all the forum's subsystems are.
Refactored the unit test logic. For instance, we now use the proper .Log method rather than fmt.Println in many cases.
Sorry, we'll have to break Github's generated file detection, as the build instructions aren't working, unless I put them at the top, and they're far, far more important than getting Github to recognise the generated code as generated code.
Fixed an issue with mysql.go's _init_database() overwriting the dbpassword variable. Not a huge issue, but it is a "gotcha" for those not expecting a ':' at the start.
Fixed an issue with forum creation where the forum permissions didn't get cached.
Fixed a bug in plugin_bbcode where negative numbers in rand would crash Gosora.
Made the outputs of plugin_markdown and plugin_bbcode more compliant with the tests.
Revamped the phrase system to make it easier for us to add language pack related features in the future.
Added the WidgetMenu widget type.
Revamped the theme again. I'm experimenting to see which approach I like most.
- Excuse the little W3C rage. Some things about CSS drive me crazy :p
Tests:
Added 22 bbcode_full_parse tests.
Added 19 bbcode_regex_parse tests.
Added 27 markdown_parse tests.
Added four UserStore tests. More to come when the test database functionality is added.
Added 18 name_to_slug tests.
Hooks:
Added the pre_render hook.
Added the pre_render_forum_list hook.
Added the pre_render_view_forum hook.
Added the pre_render_topic_list hook.
Added the pre_render_view_topic hook.
Added the pre_render_profile hook.
Added the pre_render_custom_page hook.
Added the pre_render_overview hook.
Added the pre_render_create_topic hook.
Added the pre_render_account_own_edit_critical hook.
Added the pre_render_account_own_edit_avatar hook.
Added the pre_render_account_own_edit_username hook.
Added the pre_render_account_own_edit_email hook.
Added the pre_render_login hook.
Added the pre_render_register hook.
Added the pre_render_ban hook.
Added the pre_render_panel_dashboard hook.
Added the pre_render_panel_forums hook.
Added the pre_render_panel_delete_forum hook.
Added the pre_render_panel_edit_forum hook.
Added the pre_render_panel_settings hook.
Added the pre_render_panel_setting hook.
Added the pre_render_panel_plugins hook.
Added the pre_render_panel_users hook.
Added the pre_render_panel_edit_user hook.
Added the pre_render_panel_groups hook.
Added the pre_render_panel_edit_group hook.
Added the pre_render_panel_edit_group_perms hook.
Added the pre_render_panel_themes hook.
Added the pre_render_panel_mod_log hook.
Added the pre_render_error hook.
Added the pre_render_security_error hook.
Added the create_group_preappend hook.
Added the intercept_build_widgets hook.
Added the simple_forum_check_pre_perms hook.
Added the forum_check_pre_perms hook.
2017-07-09 12:06:04 +00:00
}
2020-02-09 10:00:08 +00:00
func TestGroupPromotions ( t * testing . T ) {
miscinit ( t )
if ! c . PluginsInited {
c . InitPlugins ( )
}
2021-03-24 12:48:36 +00:00
ex , exf := exp ( t ) , expf ( t )
2020-02-09 10:00:08 +00:00
_ , err := c . GroupPromotions . Get ( - 1 )
recordMustNotExist ( t , err , "GP #-1 shouldn't exist" )
_ , err = c . GroupPromotions . Get ( 0 )
recordMustNotExist ( t , err , "GP #0 shouldn't exist" )
_ , err = c . GroupPromotions . Get ( 1 )
recordMustNotExist ( t , err , "GP #1 shouldn't exist" )
expectNilErr ( t , c . GroupPromotions . Delete ( 1 ) )
//GetByGroup(gid int) (gps []*GroupPromotion, err error)
testPromo := func ( exid , from , to , level , posts , registeredFor int , shouldFail bool ) {
gpid , err := c . GroupPromotions . Create ( from , to , false , level , posts , registeredFor )
2021-03-24 12:48:36 +00:00
exf ( gpid == exid , "gpid should be %d not %d" , exid , gpid )
2020-02-09 10:00:08 +00:00
//fmt.Println("gpid:", gpid)
gp , err := c . GroupPromotions . Get ( gpid )
expectNilErr ( t , err )
2021-03-24 12:48:36 +00:00
exf ( gp . ID == gpid , "gp.ID should be %d not %d" , gpid , gp . ID )
exf ( gp . From == from , "gp.From should be %d not %d" , from , gp . From )
exf ( gp . To == to , "gp.To should be %d not %d" , to , gp . To )
ex ( ! gp . TwoWay , "gp.TwoWay should be false not true" )
exf ( gp . Level == level , "gp.Level should be %d not %d" , level , gp . Level )
exf ( gp . Posts == posts , "gp.Posts should be %d not %d" , posts , gp . Posts )
exf ( gp . MinTime == 0 , "gp.MinTime should be %d not %d" , 0 , gp . MinTime )
exf ( gp . RegisteredFor == registeredFor , "gp.RegisteredFor should be %d not %d" , registeredFor , gp . RegisteredFor )
2020-02-09 10:00:08 +00:00
uid , err := c . Users . Create ( "Lord_" + strconv . Itoa ( gpid ) , "I_Rule" , "" , from , false )
expectNilErr ( t , err )
u , err := c . Users . Get ( uid )
expectNilErr ( t , err )
2021-03-24 12:48:36 +00:00
exf ( u . ID == uid , "u.ID should be %d not %d" , uid , u . ID )
exf ( u . Group == from , "u.Group should be %d not %d" , from , u . Group )
2020-02-09 10:00:08 +00:00
err = c . GroupPromotions . PromoteIfEligible ( u , u . Level , u . Posts , u . CreatedAt )
expectNilErr ( t , err )
u . CacheRemove ( )
u , err = c . Users . Get ( uid )
expectNilErr ( t , err )
2021-03-24 12:48:36 +00:00
exf ( u . ID == uid , "u.ID should be %d not %d" , uid , u . ID )
2020-02-09 10:00:08 +00:00
if shouldFail {
2021-03-24 12:48:36 +00:00
exf ( u . Group == from , "u.Group should be (from-group) %d not %d" , from , u . Group )
2020-02-09 10:00:08 +00:00
} else {
2021-03-24 12:48:36 +00:00
exf ( u . Group == to , "u.Group should be (to-group)%d not %d" , to , u . Group )
2020-02-09 10:00:08 +00:00
}
expectNilErr ( t , c . GroupPromotions . Delete ( gpid ) )
_ , err = c . GroupPromotions . Get ( gpid )
recordMustNotExist ( t , err , fmt . Sprintf ( "GP #%d should no longer exist" , gpid ) )
}
testPromo ( 1 , 1 , 2 , 0 , 0 , 0 , false )
testPromo ( 2 , 1 , 2 , 5 , 5 , 0 , true )
testPromo ( 3 , 1 , 2 , 0 , 0 , 1 , true )
}
2017-10-12 03:24:14 +00:00
func TestReplyStore ( t * testing . T ) {
2018-11-01 06:43:56 +00:00
miscinit ( t )
2019-04-19 08:20:10 +00:00
if ! c . PluginsInited {
c . InitPlugins ( )
2017-10-12 03:24:14 +00:00
}
2021-05-02 08:47:19 +00:00
_ , e := c . Rstore . Get ( - 1 )
recordMustNotExist ( t , e , "RID #-1 shouldn't exist" )
_ , e = c . Rstore . Get ( 0 )
recordMustNotExist ( t , e , "RID #0 shouldn't exist" )
2017-10-21 00:27:47 +00:00
2020-01-04 05:30:25 +00:00
c . Config . DisablePostIP = false
2021-03-24 21:39:27 +00:00
testReplyStore ( t , 2 , "::1" )
2020-01-04 05:30:25 +00:00
c . Config . DisablePostIP = true
2021-03-24 21:39:27 +00:00
testReplyStore ( t , 5 , "" )
2020-01-04 05:30:25 +00:00
}
2021-03-24 21:39:27 +00:00
func testReplyStore ( t * testing . T , newID int , ip string ) {
2021-03-24 12:48:36 +00:00
ex , exf := exp ( t ) , expf ( t )
2021-04-07 14:23:11 +00:00
replyTest2 := func ( r * c . Reply , e error , rid , parentID , createdBy int , content , ip string ) {
expectNilErr ( t , e )
2021-03-24 12:48:36 +00:00
exf ( r . ID == rid , "RID #%d has the wrong ID. It should be %d not %d" , rid , rid , r . ID )
exf ( r . ParentID == parentID , "The parent topic of RID #%d should be %d not %d" , rid , parentID , r . ParentID )
exf ( r . CreatedBy == createdBy , "The creator of RID #%d should be %d not %d" , rid , createdBy , r . CreatedBy )
exf ( r . Content == content , "The contents of RID #%d should be '%s' not %s" , rid , content , r . Content )
exf ( r . IP == ip , "The IP of RID#%d should be '%s' not %s" , rid , ip , r . IP )
2018-10-08 04:15:06 +00:00
}
2019-05-17 08:40:41 +00:00
2020-02-04 11:47:03 +00:00
replyTest := func ( rid , parentID , createdBy int , content , ip string ) {
2021-04-07 14:23:11 +00:00
r , e := c . Rstore . Get ( rid )
replyTest2 ( r , e , rid , parentID , createdBy , content , ip )
r , e = c . Rstore . GetCache ( ) . Get ( rid )
replyTest2 ( r , e , rid , parentID , createdBy , content , ip )
2019-05-17 08:40:41 +00:00
}
2021-04-07 14:23:11 +00:00
replyTest ( 1 , 1 , 1 , "A reply!" , "" )
2017-11-06 16:24:45 +00:00
2019-05-17 08:40:41 +00:00
// ! This is hard to do deterministically as the system may pre-load certain items but let's give it a try:
//_, err = c.Rstore.GetCache().Get(1)
//recordMustNotExist(t, err, "RID #1 shouldn't be in the cache")
2020-01-04 05:30:25 +00:00
_ , err := c . Rstore . Get ( newID )
2017-11-06 16:24:45 +00:00
recordMustNotExist ( t , err , "RID #2 shouldn't exist" )
2021-03-24 21:39:27 +00:00
newPostCount := 1
tid , err := c . Topics . Create ( 2 , "Reply Test Topic" , "Reply Test Topic" , 1 , "" )
2017-11-08 07:28:33 +00:00
expectNilErr ( t , err )
2021-03-24 21:39:27 +00:00
topic , err := c . Topics . Get ( tid )
expectNilErr ( t , err )
exf ( topic . PostCount == newPostCount , "topic.PostCount should be %d, not %d" , newPostCount , topic . PostCount )
exf ( topic . LastReplyID == 0 , "topic.LastReplyID should be %d not %d" , 0 , topic . LastReplyID )
ex ( topic . CreatedAt == topic . LastReplyAt , "topic.LastReplyAt should equal it's topic.CreatedAt" )
exf ( topic . LastReplyBy == 1 , "topic.LastReplyBy should be %d not %d" , 1 , topic . LastReplyBy )
2019-05-17 08:40:41 +00:00
2020-01-04 05:30:25 +00:00
_ , err = c . Rstore . GetCache ( ) . Get ( newID )
recordMustNotExist ( t , err , "RID #%d shouldn't be in the cache" , newID )
2019-05-17 08:40:41 +00:00
2021-03-24 21:39:27 +00:00
time . Sleep ( 2 * time . Second )
uid , err := c . Users . Create ( "Reply Topic Test User" + strconv . Itoa ( newID ) , "testpassword" , "" , 2 , true )
expectNilErr ( t , err )
rid , err := c . Rstore . Create ( topic , "Fofofo" , ip , uid )
2017-11-06 16:24:45 +00:00
expectNilErr ( t , err )
2021-03-24 12:48:36 +00:00
exf ( rid == newID , "The next reply ID should be %d not %d" , newID , rid )
2021-03-24 21:39:27 +00:00
exf ( topic . PostCount == newPostCount , "The old topic in memory's post count should be %d, not %d" , newPostCount + 1 , topic . PostCount )
2018-10-08 04:15:06 +00:00
// TODO: Test the reply count on the topic
2021-03-24 21:39:27 +00:00
exf ( topic . LastReplyID == 0 , "topic.LastReplyID should be %d not %d" , 0 , topic . LastReplyID )
ex ( topic . CreatedAt == topic . LastReplyAt , "topic.LastReplyAt should equal it's topic.CreatedAt" )
2018-10-08 04:15:06 +00:00
2021-03-24 21:39:27 +00:00
replyTest ( newID , tid , uid , "Fofofo" , ip )
2017-10-12 03:24:14 +00:00
2021-03-24 21:39:27 +00:00
topic , err = c . Topics . Get ( tid )
2018-10-08 04:15:06 +00:00
expectNilErr ( t , err )
2021-03-24 21:39:27 +00:00
exf ( topic . PostCount == newPostCount + 1 , "topic.PostCount should be %d, not %d" , newPostCount + 1 , topic . PostCount )
exf ( topic . LastReplyID == rid , "topic.LastReplyID should be %d not %d" , rid , topic . LastReplyID )
ex ( topic . CreatedAt != topic . LastReplyAt , "topic.LastReplyAt should not equal it's topic.CreatedAt" )
exf ( topic . LastReplyBy == uid , "topic.LastReplyBy should be %d not %d" , uid , topic . LastReplyBy )
expectNilErr ( t , topic . CreateActionReply ( "destroy" , ip , 1 ) )
exf ( topic . PostCount == newPostCount + 1 , "The old topic in memory's post count should be %d, not %d" , newPostCount + 1 , topic . PostCount )
replyTest ( newID + 1 , tid , 1 , "" , ip )
2018-10-08 04:15:06 +00:00
// TODO: Check the actionType field of the reply, this might not be loaded by TopicStore, maybe we should add it there?
2021-03-24 21:39:27 +00:00
topic , err = c . Topics . Get ( tid )
2017-11-06 16:24:45 +00:00
expectNilErr ( t , err )
2021-03-24 21:39:27 +00:00
exf ( topic . PostCount == newPostCount + 2 , "topic.PostCount should be %d, not %d" , newPostCount + 2 , topic . PostCount )
2021-03-25 09:04:02 +00:00
exf ( topic . LastReplyID != rid , "topic.LastReplyID should not be %d" , rid )
arid := topic . LastReplyID
2018-12-31 09:03:49 +00:00
2019-01-21 12:27:59 +00:00
// TODO: Expand upon this
2020-01-04 05:30:25 +00:00
rid , err = c . Rstore . Create ( topic , "hiii" , ip , 1 )
2018-12-31 09:03:49 +00:00
expectNilErr ( t , err )
2020-01-04 05:30:25 +00:00
replyTest ( rid , topic . ID , 1 , "hiii" , ip )
2019-05-17 08:40:41 +00:00
reply , err := c . Rstore . Get ( rid )
expectNilErr ( t , err )
expectNilErr ( t , reply . SetPost ( "huuu" ) )
2021-03-24 12:48:36 +00:00
exf ( reply . Content == "hiii" , "topic.Content should be hiii, not %s" , reply . Content )
2021-05-02 08:47:19 +00:00
2019-05-17 08:40:41 +00:00
reply , err = c . Rstore . Get ( rid )
2021-05-02 08:47:19 +00:00
replyTest2 ( reply , err , rid , topic . ID , 1 , "huuu" , ip )
expectNilErr ( t , c . Rstore . ClearIPs ( ) )
_ = c . Rstore . GetCache ( ) . Remove ( rid )
replyTest ( rid , topic . ID , 1 , "huuu" , "" )
2019-05-17 08:40:41 +00:00
expectNilErr ( t , reply . Delete ( ) )
// No pointer shenanigans x.x
2021-02-28 02:23:05 +00:00
// TODO: Log reply.ID and rid in cases of pointer shenanigans?
2021-03-24 12:48:36 +00:00
ex ( reply . ID == rid , "pointer shenanigans" )
2019-05-17 08:40:41 +00:00
_ , err = c . Rstore . GetCache ( ) . Get ( rid )
recordMustNotExist ( t , err , fmt . Sprintf ( "RID #%d shouldn't be in the cache" , rid ) )
_ , err = c . Rstore . Get ( rid )
recordMustNotExist ( t , err , fmt . Sprintf ( "RID #%d shouldn't exist" , rid ) )
2021-03-25 09:04:02 +00:00
topic , err = c . Topics . Get ( tid )
expectNilErr ( t , err )
exf ( topic . LastReplyID == arid , "topic.LastReplyID should be %d not %d" , arid , topic . LastReplyID )
2019-05-17 08:40:41 +00:00
// TODO: Write a test for this
//(topic *TopicUser) Replies(offset int, pFrag int, user *User) (rlist []*ReplyUser, ogdesc string, err error)
// TODO: Add tests for *Reply
// TODO: Add tests for ReplyCache
2017-10-12 03:24:14 +00:00
}
2020-04-10 01:32:02 +00:00
func TestLikes ( t * testing . T ) {
miscinit ( t )
if ! c . PluginsInited {
c . InitPlugins ( )
}
2021-05-02 08:47:19 +00:00
_ , exf := exp ( t ) , expf ( t )
bulkExists := func ( iids [ ] int , sentBy int , targetType string , expCount int ) {
ids , e := c . Likes . BulkExists ( iids , sentBy , "replies" )
//recordMustNotExist(t, e, "no likes should be found")
expectNilErr ( t , e )
exf ( len ( ids ) == expCount , "len ids should be %d" , expCount )
idMap := make ( map [ int ] struct { } )
for _ , id := range ids {
idMap [ id ] = struct { } { }
}
for _ , iid := range iids {
_ , ok := idMap [ iid ]
exf ( ok , "missing iid %d in idMap" , iid )
}
idCount := 0
expectNilErr ( t , c . Likes . BulkExistsFunc ( iids , sentBy , targetType , func ( _ int ) error {
idCount ++
return nil
} ) )
exf ( idCount == expCount , "idCount should be %d not %d" , expCount , idCount )
}
2020-04-10 01:32:02 +00:00
uid := 1
2021-05-02 08:47:19 +00:00
bulkExists ( [ ] int { } , uid , "replies" , 0 )
2020-04-10 01:32:02 +00:00
2021-05-02 08:47:19 +00:00
topic , e := c . Topics . Get ( 1 )
expectNilErr ( t , e )
rid , e := c . Rstore . Create ( topic , "hiii" , "" , uid )
expectNilErr ( t , e )
r , e := c . Rstore . Get ( rid )
expectNilErr ( t , e )
2020-04-10 01:32:02 +00:00
expectNilErr ( t , r . Like ( uid ) )
2021-05-02 08:47:19 +00:00
bulkExists ( [ ] int { rid } , uid , "replies" , 1 )
2020-04-10 01:32:02 +00:00
2021-05-02 08:47:19 +00:00
rid2 , e := c . Rstore . Create ( topic , "hi 2 u 2" , "" , uid )
expectNilErr ( t , e )
r2 , e := c . Rstore . Get ( rid2 )
expectNilErr ( t , e )
2020-04-10 01:32:02 +00:00
expectNilErr ( t , r2 . Like ( uid ) )
2021-05-02 08:47:19 +00:00
bulkExists ( [ ] int { rid , rid2 } , uid , "replies" , 2 )
2020-04-10 01:32:02 +00:00
expectNilErr ( t , r . Unlike ( uid ) )
2021-05-02 08:47:19 +00:00
bulkExists ( [ ] int { rid2 } , uid , "replies" , 1 )
2020-04-10 01:32:02 +00:00
expectNilErr ( t , r2 . Unlike ( uid ) )
2021-05-02 08:47:19 +00:00
bulkExists ( [ ] int { } , uid , "replies" , 0 )
2020-04-10 01:32:02 +00:00
//BulkExists(ids []int, sentBy int, targetType string) (eids []int, err error)
expectNilErr ( t , topic . Like ( 1 , uid ) )
expectNilErr ( t , topic . Unlike ( uid ) )
}
2020-02-19 10:32:26 +00:00
func TestAttachments ( t * testing . T ) {
miscinit ( t )
if ! c . PluginsInited {
c . InitPlugins ( )
}
2021-03-24 12:48:36 +00:00
ex , exf := exp ( t ) , expf ( t )
2020-02-19 10:32:26 +00:00
filename := "n0-48.png"
srcFile := "./test_data/" + filename
destFile := "./attachs/" + filename
2021-05-02 08:47:19 +00:00
ft := func ( e error ) {
if e != nil && e != sql . ErrNoRows {
t . Error ( e )
}
}
2021-03-24 12:48:36 +00:00
ex ( c . Attachments . Count ( ) == 0 , "the number of attachments should be 0" )
ex ( c . Attachments . CountIn ( "topics" , 1 ) == 0 , "the number of attachments in topic 1 should be 0" )
exf ( c . Attachments . CountInPath ( filename ) == 0 , "the number of attachments with path '%s' should be 0" , filename )
2021-05-02 08:47:19 +00:00
_ , e := c . Attachments . FGet ( 1 )
ft ( e )
ex ( e == sql . ErrNoRows , ".FGet should have no results" )
_ , e = c . Attachments . Get ( 1 )
ft ( e )
ex ( e == sql . ErrNoRows , ".Get should have no results" )
_ , e = c . Attachments . MiniGetList ( "topics" , 1 )
ft ( e )
ex ( e == sql . ErrNoRows , ".MiniGetList should have no results" )
_ , e = c . Attachments . BulkMiniGetList ( "topics" , [ ] int { 1 } )
ft ( e )
ex ( e == sql . ErrNoRows , ".BulkMiniGetList should have no results" )
2020-02-19 10:32:26 +00:00
2020-02-20 04:32:49 +00:00
simUpload := func ( ) {
// Sim an upload, try a proper upload through the proper pathway later on
2021-05-02 08:47:19 +00:00
_ , e = os . Stat ( destFile )
if e != nil && ! os . IsNotExist ( e ) {
expectNilErr ( t , e )
} else if e == nil {
expectNilErr ( t , os . Remove ( destFile ) )
2020-02-20 04:32:49 +00:00
}
2021-05-02 08:47:19 +00:00
input , e := ioutil . ReadFile ( srcFile )
expectNilErr ( t , e )
expectNilErr ( t , ioutil . WriteFile ( destFile , input , 0644 ) )
2020-02-19 10:32:26 +00:00
}
2020-02-20 04:32:49 +00:00
simUpload ( )
2020-02-19 10:32:26 +00:00
2021-05-02 08:47:19 +00:00
tid , e := c . Topics . Create ( 2 , "Attach Test" , "Filler Body" , 1 , "" )
expectNilErr ( t , e )
aid , e := c . Attachments . Add ( 2 , "forums" , tid , "topics" , 1 , filename , "" )
expectNilErr ( t , e )
2021-03-24 12:48:36 +00:00
exf ( aid == 1 , "aid should be 1 not %d" , aid )
2020-02-20 04:32:49 +00:00
expectNilErr ( t , c . Attachments . AddLinked ( "topics" , tid ) )
2021-03-24 12:48:36 +00:00
ex ( c . Attachments . Count ( ) == 1 , "the number of attachments should be 1" )
exf ( c . Attachments . CountIn ( "topics" , tid ) == 1 , "the number of attachments in topic %d should be 1" , tid )
exf ( c . Attachments . CountInPath ( filename ) == 1 , "the number of attachments with path '%s' should be 1" , filename )
2020-02-19 10:32:26 +00:00
2021-05-02 08:47:19 +00:00
et := func ( a * c . MiniAttachment , aid , sid , oid , uploadedBy int , path , extra , ext string ) {
2021-03-24 12:48:36 +00:00
exf ( a . ID == aid , "ID should be %d not %d" , aid , a . ID )
exf ( a . SectionID == sid , "SectionID should be %d not %d" , sid , a . SectionID )
exf ( a . OriginID == oid , "OriginID should be %d not %d" , oid , a . OriginID )
exf ( a . UploadedBy == uploadedBy , "UploadedBy should be %d not %d" , uploadedBy , a . UploadedBy )
exf ( a . Path == path , "Path should be %s not %s" , path , a . Path )
exf ( a . Extra == extra , "Extra should be %s not %s" , extra , a . Extra )
ex ( a . Image , "Image should be true" )
exf ( a . Ext == ext , "Ext should be %s not %s" , ext , a . Ext )
2020-02-20 04:32:49 +00:00
}
2021-05-02 08:47:19 +00:00
et2 := func ( a * c . Attachment , aid , sid , oid , uploadedBy int , path , extra , ext string ) {
2021-03-24 12:48:36 +00:00
exf ( a . ID == aid , "ID should be %d not %d" , aid , a . ID )
exf ( a . SectionID == sid , "SectionID should be %d not %d" , sid , a . SectionID )
exf ( a . OriginID == oid , "OriginID should be %d not %d" , oid , a . OriginID )
exf ( a . UploadedBy == uploadedBy , "UploadedBy should be %d not %d" , uploadedBy , a . UploadedBy )
exf ( a . Path == path , "Path should be %s not %s" , path , a . Path )
exf ( a . Extra == extra , "Extra should be %s not %s" , extra , a . Extra )
ex ( a . Image , "Image should be true" )
exf ( a . Ext == ext , "Ext should be %s not %s" , ext , a . Ext )
2020-02-19 10:32:26 +00:00
}
2020-02-20 10:57:50 +00:00
f2 := func ( aid , sid , oid int , extra string , topic bool ) {
2020-02-19 10:32:26 +00:00
var tbl string
if topic {
tbl = "topics"
} else {
tbl = "replies"
}
2021-05-02 08:47:19 +00:00
fa , e := c . Attachments . FGet ( aid )
expectNilErr ( t , e )
et2 ( fa , aid , sid , oid , 1 , filename , extra , "png" )
2020-02-20 04:32:49 +00:00
2021-05-02 08:47:19 +00:00
a , e := c . Attachments . Get ( aid )
expectNilErr ( t , e )
et ( a , aid , sid , oid , 1 , filename , extra , "png" )
2020-02-19 10:32:26 +00:00
2021-05-02 08:47:19 +00:00
alist , e := c . Attachments . MiniGetList ( tbl , oid )
expectNilErr ( t , e )
2021-03-24 12:48:36 +00:00
exf ( len ( alist ) == 1 , "len(alist) should be 1 not %d" , len ( alist ) )
2020-02-19 10:32:26 +00:00
a = alist [ 0 ]
2021-05-02 08:47:19 +00:00
et ( a , aid , sid , oid , 1 , filename , extra , "png" )
2020-02-19 10:32:26 +00:00
2021-05-02 08:47:19 +00:00
amap , e := c . Attachments . BulkMiniGetList ( tbl , [ ] int { oid } )
expectNilErr ( t , e )
2021-03-24 12:48:36 +00:00
exf ( len ( amap ) == 1 , "len(amap) should be 1 not %d" , len ( amap ) )
2020-02-19 10:32:26 +00:00
alist , ok := amap [ oid ]
if ! ok {
t . Logf ( "key %d not found in amap" , oid )
}
2021-03-24 12:48:36 +00:00
exf ( len ( alist ) == 1 , "len(alist) should be 1 not %d" , len ( alist ) )
2020-02-19 10:32:26 +00:00
a = alist [ 0 ]
2021-05-02 08:47:19 +00:00
et ( a , aid , sid , oid , 1 , filename , extra , "png" )
2020-02-19 10:32:26 +00:00
}
2021-05-02 08:47:19 +00:00
topic , e := c . Topics . Get ( tid )
expectNilErr ( t , e )
2021-03-24 12:48:36 +00:00
exf ( topic . AttachCount == 1 , "topic.AttachCount should be 1 not %d" , topic . AttachCount )
2020-02-20 10:57:50 +00:00
f2 ( aid , 2 , tid , "" , true )
expectNilErr ( t , topic . MoveTo ( 1 ) )
f2 ( aid , 1 , tid , "" , true )
expectNilErr ( t , c . Attachments . MoveTo ( 2 , tid , "topics" ) )
f2 ( aid , 2 , tid , "" , true )
2020-02-19 10:32:26 +00:00
2020-02-20 10:57:50 +00:00
// TODO: ShowAttachment test
2020-02-19 10:32:26 +00:00
2020-02-20 04:32:49 +00:00
deleteTest := func ( aid , oid int , topic bool ) {
var tbl string
if topic {
tbl = "topics"
} else {
tbl = "replies"
}
//expectNilErr(t, c.Attachments.Delete(aid))
expectNilErr ( t , c . DeleteAttachment ( aid ) )
2021-03-24 12:48:36 +00:00
ex ( c . Attachments . Count ( ) == 0 , "the number of attachments should be 0" )
exf ( c . Attachments . CountIn ( tbl , oid ) == 0 , "the number of attachments in topic %d should be 0" , tid )
exf ( c . Attachments . CountInPath ( filename ) == 0 , "the number of attachments with path '%s' should be 0" , filename )
2021-05-02 08:47:19 +00:00
_ , e = c . Attachments . FGet ( aid )
ft ( e )
ex ( e == sql . ErrNoRows , ".FGet should have no results" )
_ , e = c . Attachments . Get ( aid )
ft ( e )
ex ( e == sql . ErrNoRows , ".Get should have no results" )
_ , e = c . Attachments . MiniGetList ( tbl , oid )
ft ( e )
ex ( e == sql . ErrNoRows , ".MiniGetList should have no results" )
_ , e = c . Attachments . BulkMiniGetList ( tbl , [ ] int { oid } )
ft ( e )
ex ( e == sql . ErrNoRows , ".BulkMiniGetList should have no results" )
2020-02-19 10:32:26 +00:00
}
2020-02-20 04:32:49 +00:00
deleteTest ( aid , tid , true )
2021-05-02 08:47:19 +00:00
topic , e = c . Topics . Get ( tid )
expectNilErr ( t , e )
2021-03-24 12:48:36 +00:00
exf ( topic . AttachCount == 0 , "topic.AttachCount should be 0 not %d" , topic . AttachCount )
2020-02-19 10:32:26 +00:00
2020-02-20 04:32:49 +00:00
simUpload ( )
2021-05-02 08:47:19 +00:00
rid , e := c . Rstore . Create ( topic , "Reply Filler" , "" , 1 )
expectNilErr ( t , e )
aid , e = c . Attachments . Add ( 2 , "forums" , rid , "replies" , 1 , filename , strconv . Itoa ( topic . ID ) )
expectNilErr ( t , e )
2021-03-24 12:48:36 +00:00
exf ( aid == 2 , "aid should be 2 not %d" , aid )
2020-02-20 04:32:49 +00:00
expectNilErr ( t , c . Attachments . AddLinked ( "replies" , rid ) )
2021-05-02 08:47:19 +00:00
r , e := c . Rstore . Get ( rid )
expectNilErr ( t , e )
2021-03-24 12:48:36 +00:00
exf ( r . AttachCount == 1 , "r.AttachCount should be 1 not %d" , r . AttachCount )
2020-02-20 10:57:50 +00:00
f2 ( aid , 2 , rid , strconv . Itoa ( topic . ID ) , false )
expectNilErr ( t , c . Attachments . MoveTo ( 1 , rid , "replies" ) )
f2 ( aid , 1 , rid , strconv . Itoa ( topic . ID ) , false )
2020-02-20 04:32:49 +00:00
deleteTest ( aid , rid , false )
2021-05-02 08:47:19 +00:00
r , e = c . Rstore . Get ( rid )
expectNilErr ( t , e )
2021-03-24 12:48:36 +00:00
exf ( r . AttachCount == 0 , "r.AttachCount should be 0 not %d" , r . AttachCount )
2020-02-19 10:32:26 +00:00
// TODO: Path overlap tests
}
2020-02-20 05:36:54 +00:00
func TestPolls ( t * testing . T ) {
miscinit ( t )
if ! c . PluginsInited {
c . InitPlugins ( )
}
2021-05-02 08:47:19 +00:00
ex , exf := exp ( t ) , expf ( t )
2020-02-20 05:36:54 +00:00
2021-02-28 02:23:05 +00:00
shouldNotExist := func ( id int ) {
2021-05-02 08:47:19 +00:00
exf ( ! c . Polls . Exists ( id ) , "poll %d should not exist" , id )
_ , e := c . Polls . Get ( id )
recordMustNotExist ( t , e , fmt . Sprintf ( "poll %d shouldn't exist" , id ) )
2021-02-28 02:23:05 +00:00
}
shouldNotExist ( - 1 )
shouldNotExist ( 0 )
shouldNotExist ( 1 )
2021-05-04 11:14:48 +00:00
exf ( c . Polls . Count ( ) == 0 , "count should be %d not %d" , 0 , c . Polls . Count ( ) )
2020-02-20 05:36:54 +00:00
2021-05-02 08:47:19 +00:00
tid , e := c . Topics . Create ( 2 , "Poll Test" , "Filler Body" , 1 , "" )
expectNilErr ( t , e )
topic , e := c . Topics . Get ( tid )
2021-05-12 03:28:00 +00:00
expectNilErr ( t , e )
exf ( topic . Poll == 0 , "t.Poll should be %d not %d" , 0 , topic . Poll )
2020-02-20 05:36:54 +00:00
/ * Options map [ int ] string
Results map [ int ] int // map[optionIndex]points
QuickOptions [ ] PollOption // TODO: Fix up the template transpiler so we don't need to use this hack anymore
} * /
pollType := 0 // Basic single choice
2021-05-02 08:47:19 +00:00
pid , e := c . Polls . Create ( topic , pollType , map [ int ] string { 0 : "item 1" , 1 : "item 2" , 2 : "item 3" } )
expectNilErr ( t , e )
exf ( pid == 1 , "poll id should be 1 not %d" , pid )
ex ( c . Polls . Exists ( 1 ) , "poll 1 should exist" )
2021-05-04 11:14:48 +00:00
exf ( c . Polls . Count ( ) == 1 , "count should be %d not %d" , 1 , c . Polls . Count ( ) )
2021-05-12 03:28:00 +00:00
topic , e = c . Topics . BypassGet ( tid )
expectNilErr ( t , e )
exf ( topic . Poll == pid , "t.Poll should be %d not %d" , pid , topic . Poll )
2021-02-28 02:23:05 +00:00
testPoll := func ( p * c . Poll , id , parentID int , parentTable string , ptype int , antiCheat bool , voteCount int ) {
2021-05-02 08:47:19 +00:00
ef := exf
ef ( p . ID == id , "p.ID should be %d not %d" , id , p . ID )
ef ( p . ParentID == parentID , "p.ParentID should be %d not %d" , parentID , p . ParentID )
ef ( p . ParentTable == parentTable , "p.ParentID should be %s not %s" , parentTable , p . ParentTable )
ef ( p . Type == ptype , "p.ParentID should be %d not %d" , ptype , p . Type )
2021-02-28 02:23:05 +00:00
s := "false"
if p . AntiCheat {
s = "true"
}
2021-05-02 08:47:19 +00:00
ef ( p . AntiCheat == antiCheat , "p.AntiCheat should be " , s )
2021-02-28 02:23:05 +00:00
// TODO: More fields
2021-05-02 08:47:19 +00:00
ef ( p . VoteCount == voteCount , "p.VoteCount should be %d not %d" , voteCount , p . VoteCount )
2021-02-28 02:23:05 +00:00
}
2021-05-02 08:47:19 +00:00
p , e := c . Polls . Get ( 1 )
expectNilErr ( t , e )
2021-02-28 02:23:05 +00:00
testPoll ( p , 1 , tid , "topics" , 0 , false , 0 )
2020-02-20 05:36:54 +00:00
2020-02-20 11:28:49 +00:00
expectNilErr ( t , p . CastVote ( 0 , 1 , "" ) )
expectNilErr ( t , c . Polls . Reload ( p . ID ) )
2021-05-02 08:47:19 +00:00
p , e = c . Polls . Get ( 1 )
expectNilErr ( t , e )
2021-02-28 02:23:05 +00:00
testPoll ( p , 1 , tid , "topics" , 0 , false , 1 )
2020-02-20 11:28:49 +00:00
2021-05-02 08:47:19 +00:00
expectNilErr ( t , c . Polls . ClearIPs ( ) )
// TODO: Test to see if it worked
2020-02-20 11:28:49 +00:00
expectNilErr ( t , p . Delete ( ) )
2021-05-02 08:47:19 +00:00
ex ( ! c . Polls . Exists ( 1 ) , "poll 1 should no longer exist" )
_ , e = c . Polls . Get ( 1 )
recordMustNotExist ( t , e , "poll 1 should no longer exist" )
2021-05-04 11:14:48 +00:00
exf ( c . Polls . Count ( ) == 0 , "count should be %d not %d" , 0 , c . Polls . Count ( ) )
2021-05-12 03:28:00 +00:00
topic , e = c . Topics . BypassGet ( tid )
expectNilErr ( t , e )
exf ( topic . Poll == pid , "t.Poll should be %d not %d" , pid , topic . Poll )
expectNilErr ( t , topic . SetPoll ( 999 ) )
topic , e = c . Topics . BypassGet ( tid )
expectNilErr ( t , e )
exf ( topic . Poll == pid , "t.Poll should be %d not %d" , pid , topic . Poll )
expectNilErr ( t , topic . SetPoll ( 0 ) )
topic , e = c . Topics . BypassGet ( tid )
expectNilErr ( t , e )
exf ( topic . Poll == pid , "t.Poll should be %d not %d" , pid , topic . Poll )
expectNilErr ( t , topic . RemovePoll ( ) )
topic , e = c . Topics . BypassGet ( tid )
expectNilErr ( t , e )
exf ( topic . Poll == 0 , "t.Poll should be %d not %d" , 0 , topic . Poll )
2020-02-20 05:36:54 +00:00
}
2020-02-21 11:42:21 +00:00
func TestSearch ( t * testing . T ) {
miscinit ( t )
if ! c . PluginsInited {
c . InitPlugins ( )
}
2021-05-02 08:47:19 +00:00
exf := expf ( t )
2020-02-21 11:42:21 +00:00
title := "search"
body := "bab bab bab bab"
q := "search"
2021-05-02 08:47:19 +00:00
tid , e := c . Topics . Create ( 2 , title , body , 1 , "" )
expectNilErr ( t , e )
2020-02-21 11:42:21 +00:00
2021-05-02 08:47:19 +00:00
tids , e := c . RepliesSearch . Query ( q , [ ] int { 2 } )
//fmt.Printf("tids: %+v\n", tids)
expectNilErr ( t , e )
exf ( len ( tids ) == 1 , "len(tids) should be 1 not %d" , len ( tids ) )
2020-02-21 11:42:21 +00:00
2021-05-02 08:47:19 +00:00
topic , e := c . Topics . Get ( tids [ 0 ] )
expectNilErr ( t , e )
exf ( topic . ID == tid , "topic.ID should be %d not %d" , tid , topic . ID )
exf ( topic . Title == title , "topic.Title should be %s not %s" , title , topic . Title )
2020-02-21 11:42:21 +00:00
2021-05-02 08:47:19 +00:00
tids , e = c . RepliesSearch . Query ( q , [ ] int { 1 , 2 } )
//fmt.Printf("tids: %+v\n", tids)
expectNilErr ( t , e )
exf ( len ( tids ) == 1 , "len(tids) should be 1 not %d" , len ( tids ) )
2020-02-21 11:42:21 +00:00
q = "bab"
2021-05-02 08:47:19 +00:00
tids , e = c . RepliesSearch . Query ( q , [ ] int { 1 , 2 } )
//fmt.Printf("tids: %+v\n", tids)
expectNilErr ( t , e )
exf ( len ( tids ) == 1 , "len(tids) should be 1 not %d" , len ( tids ) )
2020-02-21 11:42:21 +00:00
}
2017-10-12 03:24:14 +00:00
func TestProfileReplyStore ( t * testing . T ) {
2018-11-01 06:43:56 +00:00
miscinit ( t )
2019-04-19 08:20:10 +00:00
if ! c . PluginsInited {
c . InitPlugins ( )
2017-10-12 03:24:14 +00:00
}
2021-05-02 08:47:19 +00:00
_ , e := c . Prstore . Get ( - 1 )
recordMustNotExist ( t , e , "PRID #-1 shouldn't exist" )
_ , e = c . Prstore . Get ( 0 )
recordMustNotExist ( t , e , "PRID #0 shouldn't exist" )
_ , e = c . Prstore . Get ( 1 )
recordMustNotExist ( t , e , "PRID #1 shouldn't exist" )
2017-11-06 16:24:45 +00:00
2020-01-04 05:30:25 +00:00
c . Config . DisablePostIP = false
testProfileReplyStore ( t , 1 , "::1" )
c . Config . DisablePostIP = true
2020-02-04 11:47:03 +00:00
testProfileReplyStore ( t , 2 , "" )
2020-01-04 05:30:25 +00:00
}
func testProfileReplyStore ( t * testing . T , newID int , ip string ) {
2021-03-24 12:48:36 +00:00
exf := expf ( t )
2018-05-28 06:27:12 +00:00
// ? - Commented this one out as strong constraints like this put an unreasonable load on the database, we only want errors if a delete which should succeed fails
2019-04-19 08:20:10 +00:00
//profileReply := c.BlankProfileReply(1)
2021-05-02 08:47:19 +00:00
//e = profileReply.Delete()
//expect(t,e != nil,"You shouldn't be able to delete profile replies which don't exist")
2018-05-28 06:27:12 +00:00
2019-10-06 00:34:09 +00:00
profileID := 1
2021-05-02 08:47:19 +00:00
prid , e := c . Prstore . Create ( profileID , "Haha" , 1 , ip )
expectNilErr ( t , e )
2021-03-24 12:48:36 +00:00
exf ( prid == newID , "The first profile reply should have an ID of %d" , newID )
2017-11-06 16:24:45 +00:00
2021-05-02 08:47:19 +00:00
pr , e := c . Prstore . Get ( newID )
expectNilErr ( t , e )
exf ( pr . ID == newID , "The profile reply should have an ID of %d not %d" , newID , pr . ID )
exf ( pr . ParentID == 1 , "The parent ID of the profile reply should be 1 not %d" , pr . ParentID )
exf ( pr . Content == "Haha" , "The profile reply's contents should be 'Haha' not '%s'" , pr . Content )
exf ( pr . CreatedBy == 1 , "The profile reply's creator should be 1 not %d" , pr . CreatedBy )
exf ( pr . IP == ip , "The profile reply's IP should be '%s' not '%s'" , ip , pr . IP )
expectNilErr ( t , c . Prstore . ClearIPs ( ) )
pr , e = c . Prstore . Get ( newID )
expectNilErr ( t , e )
2021-03-24 12:48:36 +00:00
exf ( pr . ID == newID , "The profile reply should have an ID of %d not %d" , newID , pr . ID )
exf ( pr . ParentID == 1 , "The parent ID of the profile reply should be 1 not %d" , pr . ParentID )
exf ( pr . Content == "Haha" , "The profile reply's contents should be 'Haha' not '%s'" , pr . Content )
exf ( pr . CreatedBy == 1 , "The profile reply's creator should be 1 not %d" , pr . CreatedBy )
2021-05-02 08:47:19 +00:00
ip = ""
2021-03-24 12:48:36 +00:00
exf ( pr . IP == ip , "The profile reply's IP should be '%s' not '%s'" , ip , pr . IP )
2017-11-06 16:24:45 +00:00
2021-03-24 12:48:36 +00:00
expectNilErr ( t , pr . Delete ( ) )
2021-05-02 08:47:19 +00:00
_ , e = c . Prstore . Get ( newID )
exf ( e != nil , "PRID #%d shouldn't exist after being deleted" , newID )
2017-11-06 16:24:45 +00:00
Cascade delete attachments properly.
Cascade delete replied to topic events for replies properly.
Cascade delete likes on topic posts properly.
Cascade delete replies and their children properly.
Recalculate user stats properly when items are deleted.
Users can now unlike topic opening posts.
Add a recalculator to fix abnormalities across upgrades.
Try fixing a last_ip daily update bug.
Add Existable interface.
Add Delete method to LikeStore.
Add Each, Exists, Create, CountUser, CountMegaUser and CountBigUser methods to ReplyStore.
Add CountUser, CountMegaUser, CountBigUser methods to TopicStore.
Add Each method to UserStore.
Add Add, Delete and DeleteResource methods to SubscriptionStore.
Add Delete, DeleteByParams, DeleteByParamsExtra and AidsByParamsExtra methods to ActivityStream.
Add Exists method to ProfileReplyStore.
Add DropColumn, RenameColumn and ChangeColumn to the database adapters.
Shorten ipaddress column names to ip.
- topics table.
- replies table
- users_replies table.
- polls_votes table.
Add extra column to activity_stream table.
Fix an issue upgrading sites to MariaDB 10.3 from older versions of Gosora. Please report any other issues you find.
You need to run the updater / patcher for this commit.
2020-01-31 07:22:08 +00:00
// TODO: Test pr.SetBody() and pr.Creator()
2017-10-12 03:24:14 +00:00
}
2020-02-11 03:13:38 +00:00
func TestConvos ( t * testing . T ) {
miscinit ( t )
if ! c . PluginsInited {
c . InitPlugins ( )
}
2021-03-24 12:48:36 +00:00
ex , exf := exp ( t ) , expf ( t )
2020-02-11 03:13:38 +00:00
2021-02-27 06:13:03 +00:00
sf := func ( i interface { } , e error ) error {
return e
}
mf := func ( e error , msg string , exists bool ) {
if ! exists {
recordMustNotExist ( t , e , msg )
} else {
recordMustExist ( t , e , msg )
}
}
gu := func ( uid , offset int , exists bool ) {
s := ""
if ! exists {
s = " not"
}
mf ( sf ( c . Convos . GetUser ( uid , offset ) ) , fmt . Sprintf ( "convo getuser %d %d should%s exist" , uid , offset , s ) , exists )
}
gue := func ( uid , offset int , exists bool ) {
s := ""
if ! exists {
s = " not"
}
mf ( sf ( c . Convos . GetUserExtra ( uid , offset ) ) , fmt . Sprintf ( "convo getuserextra %d %d should%s exist" , uid , offset , s ) , exists )
}
2021-03-24 12:48:36 +00:00
ex ( c . Convos . GetUserCount ( - 1 ) == 0 , "getusercount should be 0" )
ex ( c . Convos . GetUserCount ( 0 ) == 0 , "getusercount should be 0" )
2021-02-27 06:13:03 +00:00
mf ( sf ( c . Convos . Get ( - 1 ) ) , "convo -1 should not exist" , false )
mf ( sf ( c . Convos . Get ( 0 ) ) , "convo 0 should not exist" , false )
gu ( - 1 , - 1 , false )
gu ( - 1 , 0 , false )
gu ( 0 , 0 , false )
gue ( - 1 , - 1 , false )
gue ( - 1 , 0 , false )
gue ( 0 , 0 , false )
2021-03-24 12:48:36 +00:00
nf := func ( cid , count int ) {
2021-02-27 06:13:03 +00:00
ex := count > 0
s := ""
if ! ex {
s = " not"
}
mf ( sf ( c . Convos . Get ( cid ) ) , fmt . Sprintf ( "convo %d should%s exist" , cid , s ) , ex )
gu ( 1 , 0 , ex )
gu ( 1 , 5 , false ) // invariant may change in future tests
2021-03-24 12:48:36 +00:00
exf ( c . Convos . GetUserCount ( 1 ) == count , "getusercount should be %d" , count )
2021-02-27 06:13:03 +00:00
gue ( 1 , 0 , ex )
gue ( 1 , 5 , false ) // invariant may change in future tests
2021-03-24 12:48:36 +00:00
exf ( c . Convos . Count ( ) == count , "convos count should be %d" , count )
2021-02-27 06:13:03 +00:00
}
nf ( 1 , 0 )
2020-02-11 03:13:38 +00:00
2021-02-27 06:13:03 +00:00
awaitingActivation := 5
uid , err := c . Users . Create ( "Saturn" , "ReallyBadPassword" , "" , awaitingActivation , false )
expectNilErr ( t , err )
2020-02-11 03:13:38 +00:00
2021-02-27 06:13:03 +00:00
cid , err := c . Convos . Create ( "hehe" , 1 , [ ] int { uid } )
2020-02-19 05:12:59 +00:00
expectNilErr ( t , err )
2021-03-24 12:48:36 +00:00
ex ( cid == 1 , "cid should be 1" )
ex ( c . Convos . Count ( ) == 1 , "convos count should be 1" )
2020-02-19 05:12:59 +00:00
co , err := c . Convos . Get ( cid )
expectNilErr ( t , err )
2021-03-24 12:48:36 +00:00
ex ( co . ID == 1 , "co.ID should be 1" )
ex ( co . CreatedBy == 1 , "co.CreatedBy should be 1" )
2020-02-11 03:13:38 +00:00
// TODO: CreatedAt test
2021-03-24 12:48:36 +00:00
ex ( co . LastReplyBy == 1 , "co.LastReplyBy should be 1" )
2020-02-11 03:13:38 +00:00
// TODO: LastReplyAt test
2021-02-27 06:13:03 +00:00
expectIntToBeX ( t , co . PostsCount ( ) , 1 , "postscount should be 1, not %d" )
2021-03-24 12:48:36 +00:00
ex ( co . Has ( uid ) , "saturn should be in the conversation" )
ex ( ! co . Has ( 9999 ) , "uid 9999 should not be in the conversation" )
2021-02-27 06:13:03 +00:00
uids , err := co . Uids ( )
expectNilErr ( t , err )
expectIntToBeX ( t , len ( uids ) , 2 , "uids length should be 2, not %d" )
2021-03-24 12:48:36 +00:00
exf ( uids [ 0 ] == uid , "uids[0] should be %d, not %d" , uid , uids [ 0 ] )
exf ( uids [ 1 ] == 1 , "uids[1] should be %d, not %d" , 1 , uids [ 1 ] )
2021-02-27 06:13:03 +00:00
nf ( cid , 1 )
expectNilErr ( t , c . Convos . Delete ( cid ) )
expectIntToBeX ( t , co . PostsCount ( ) , 0 , "postscount should be 0, not %d" )
2021-03-24 12:48:36 +00:00
ex ( ! co . Has ( uid ) , "saturn should not be in a deleted conversation" )
2021-02-27 06:13:03 +00:00
uids , err = co . Uids ( )
expectNilErr ( t , err )
expectIntToBeX ( t , len ( uids ) , 0 , "uids length should be 0, not %d" )
nf ( cid , 0 )
2020-02-11 03:13:38 +00:00
// TODO: More tests
2021-02-27 06:13:03 +00:00
// Block tests
ok , err := c . UserBlocks . IsBlockedBy ( 1 , 1 )
expectNilErr ( t , err )
2021-03-24 12:48:36 +00:00
ex ( ! ok , "there shouldn't be any blocks" )
2021-02-27 06:13:03 +00:00
ok , err = c . UserBlocks . BulkIsBlockedBy ( [ ] int { 1 } , 1 )
expectNilErr ( t , err )
2021-03-24 12:48:36 +00:00
ex ( ! ok , "there shouldn't be any blocks" )
2021-02-27 06:13:03 +00:00
bf := func ( blocker , offset , perPage , expectLen , blockee int ) {
l , err := c . UserBlocks . BlockedByOffset ( blocker , offset , perPage )
expectNilErr ( t , err )
2021-03-24 12:48:36 +00:00
exf ( len ( l ) == expectLen , "there should be %d users blocked by %d not %d" , expectLen , blocker , len ( l ) )
2021-02-27 06:13:03 +00:00
if len ( l ) > 0 {
2021-03-24 12:48:36 +00:00
exf ( l [ 0 ] == blockee , "blocked uid should be %d not %d" , blockee , l [ 0 ] )
2021-02-27 06:13:03 +00:00
}
}
nbf := func ( blocker , blockee int ) {
ok , err := c . UserBlocks . IsBlockedBy ( 1 , 2 )
expectNilErr ( t , err )
2021-03-24 12:48:36 +00:00
ex ( ! ok , "there shouldn't be any blocks" )
2021-02-27 06:13:03 +00:00
ok , err = c . UserBlocks . BulkIsBlockedBy ( [ ] int { 1 } , 2 )
expectNilErr ( t , err )
2021-03-24 12:48:36 +00:00
ex ( ! ok , "there shouldn't be any blocks" )
2021-02-27 06:13:03 +00:00
expectIntToBeX ( t , c . UserBlocks . BlockedByCount ( 1 ) , 0 , "blockedbycount for 1 should be 1, not %d" )
bf ( 1 , 0 , 1 , 0 , 0 )
bf ( 1 , 0 , 15 , 0 , 0 )
bf ( 1 , 1 , 15 , 0 , 0 )
bf ( 1 , 5 , 15 , 0 , 0 )
}
nbf ( 1 , 2 )
expectNilErr ( t , c . UserBlocks . Add ( 1 , 2 ) )
ok , err = c . UserBlocks . IsBlockedBy ( 1 , 2 )
expectNilErr ( t , err )
2021-03-24 12:48:36 +00:00
ex ( ok , "2 should be blocked by 1" )
2021-02-27 06:13:03 +00:00
expectIntToBeX ( t , c . UserBlocks . BlockedByCount ( 1 ) , 1 , "blockedbycount for 1 should be 1, not %d" )
bf ( 1 , 0 , 1 , 1 , 2 )
bf ( 1 , 0 , 15 , 1 , 2 )
bf ( 1 , 1 , 15 , 0 , 0 )
bf ( 1 , 5 , 15 , 0 , 0 )
// Double add test
expectNilErr ( t , c . UserBlocks . Add ( 1 , 2 ) )
ok , err = c . UserBlocks . IsBlockedBy ( 1 , 2 )
expectNilErr ( t , err )
2021-03-24 12:48:36 +00:00
ex ( ok , "2 should be blocked by 1" )
2021-02-27 06:13:03 +00:00
//expectIntToBeX(t, c.UserBlocks.BlockedByCount(1), 1, "blockedbycount for 1 should be 1, not %d") // todo: fix this
//bf(1, 0, 1, 1, 2) // todo: fix this
//bf(1, 0, 15, 1, 2) // todo: fix this
//bf(1, 1, 15, 0, 0) // todo: fix this
bf ( 1 , 5 , 15 , 0 , 0 )
expectNilErr ( t , c . UserBlocks . Remove ( 1 , 2 ) )
nbf ( 1 , 2 )
// Double remove test
expectNilErr ( t , c . UserBlocks . Remove ( 1 , 2 ) )
nbf ( 1 , 2 )
// TODO: Self-block test
// TODO: More Block tests
2020-02-11 03:13:38 +00:00
}
2019-06-01 12:31:48 +00:00
func TestActivityStream ( t * testing . T ) {
miscinit ( t )
2021-05-02 08:47:19 +00:00
ex , exf := exp ( t ) , expf ( t )
2019-06-01 12:31:48 +00:00
2021-03-24 12:48:36 +00:00
ex ( c . Activity . Count ( ) == 0 , "activity stream count should be 0" )
2021-05-02 08:47:19 +00:00
gNone := func ( id int ) {
_ , e := c . Activity . Get ( id )
recordMustNotExist ( t , e , "activity item " + strconv . Itoa ( id ) + " shouldn't exist" )
}
gNone ( - 1 )
gNone ( 0 )
gNone ( 1 )
countAsid := func ( asid , count int ) {
exf ( c . ActivityMatches . CountAsid ( asid ) == count , "activity stream matches count for asid %d should be %d not %d" , asid , count , c . ActivityMatches . CountAsid ( asid ) )
}
countAsid ( - 1 , 0 )
countAsid ( 0 , 0 )
countAsid ( 1 , 0 )
2019-06-01 12:31:48 +00:00
a := c . Alert { ActorID : 1 , TargetUserID : 1 , Event : "like" , ElementType : "topic" , ElementID : 1 }
2021-05-02 08:47:19 +00:00
id , e := c . Activity . Add ( a )
expectNilErr ( t , e )
2021-03-24 12:48:36 +00:00
ex ( id == 1 , "new activity item id should be 1" )
2019-06-01 12:31:48 +00:00
2021-03-24 12:48:36 +00:00
ex ( c . Activity . Count ( ) == 1 , "activity stream count should be 1" )
2021-05-02 08:47:19 +00:00
al , e := c . Activity . Get ( 1 )
expectNilErr ( t , e )
exf ( al . ASID == id , "alert asid should be %d not %d" , id , al . ASID )
2021-03-24 12:48:36 +00:00
ex ( al . ActorID == 1 , "alert actorid should be 1" )
ex ( al . TargetUserID == 1 , "alert targetuserid should be 1" )
ex ( al . Event == "like" , "alert event type should be like" )
ex ( al . ElementType == "topic" , "alert element type should be topic" )
ex ( al . ElementID == 1 , "alert element id should be 1" )
2021-05-02 08:47:19 +00:00
countAsid ( id , 0 )
tuid , e := c . Users . Create ( "Activity Target" , "Activity Target" , "" , 1 , true )
expectNilErr ( t , e )
expectNilErr ( t , c . ActivityMatches . Add ( tuid , 1 ) )
countAsid ( id , 1 )
expectNilErr ( t , c . ActivityMatches . Delete ( tuid , id ) )
countAsid ( id , 0 )
expectNilErr ( t , c . ActivityMatches . Add ( tuid , 1 ) )
countAsid ( id , 1 )
changed , e := c . ActivityMatches . DeleteAndCountChanged ( tuid , id )
expectNilErr ( t , e )
exf ( changed == 1 , "changed should be %d not %d" , 1 , changed )
countAsid ( id , 0 )
expectNilErr ( t , c . ActivityMatches . Add ( tuid , 1 ) )
countAsid ( id , 1 )
// TODO: Add more tests
2021-03-24 12:48:36 +00:00
expectNilErr ( t , c . Activity . Delete ( id ) )
ex ( c . Activity . Count ( ) == 0 , "activity stream count should be 0" )
2021-05-02 08:47:19 +00:00
gNone ( id )
countAsid ( id , 0 )
2021-03-24 12:48:36 +00:00
// TODO: More tests
2019-06-01 12:31:48 +00:00
}
2018-10-08 02:51:59 +00:00
func TestLogs ( t * testing . T ) {
2021-03-24 12:48:36 +00:00
ex , exf := exp ( t ) , expf ( t )
2018-11-01 06:43:56 +00:00
miscinit ( t )
2020-02-09 10:00:08 +00:00
gTests := func ( s c . LogStore , phrase string ) {
2021-03-24 12:48:36 +00:00
ex ( s . Count ( ) == 0 , "There shouldn't be any " + phrase )
2020-02-09 10:00:08 +00:00
logs , err := s . GetOffset ( 0 , 25 )
2018-10-08 02:51:59 +00:00
expectNilErr ( t , err )
2021-03-24 12:48:36 +00:00
ex ( len ( logs ) == 0 , "The log slice should be empty" )
2018-10-08 02:51:59 +00:00
}
2019-04-19 08:20:10 +00:00
gTests ( c . ModLogs , "modlogs" )
gTests ( c . AdminLogs , "adminlogs" )
2018-10-08 02:51:59 +00:00
2020-02-09 10:00:08 +00:00
gTests2 := func ( s c . LogStore , phrase string ) {
err := s . Create ( "something" , 0 , "bumblefly" , "::1" , 1 )
2018-10-08 02:51:59 +00:00
expectNilErr ( t , err )
2020-02-09 10:00:08 +00:00
count := s . Count ( )
2021-03-24 12:48:36 +00:00
exf ( count == 1 , "store.Count should return one, not %d" , count )
2020-02-09 10:00:08 +00:00
logs , err := s . GetOffset ( 0 , 25 )
2018-10-08 02:51:59 +00:00
recordMustExist ( t , err , "We should have at-least one " + phrase )
2021-03-24 12:48:36 +00:00
ex ( len ( logs ) == 1 , "The length of the log slice should be one" )
2018-10-08 02:51:59 +00:00
2020-02-11 03:13:38 +00:00
l := logs [ 0 ]
2021-03-24 12:48:36 +00:00
ex ( l . Action == "something" , "l.Action is not something" )
ex ( l . ElementID == 0 , "l.ElementID is not 0" )
ex ( l . ElementType == "bumblefly" , "l.ElementType is not bumblefly" )
ex ( l . IP == "::1" , "l.IP is not ::1" )
ex ( l . ActorID == 1 , "l.ActorID is not 1" )
2018-10-08 02:51:59 +00:00
// TODO: Add a test for log.DoneAt? Maybe throw in some dates and times which are clearly impossible but which may occur due to timezone bugs?
}
2019-04-19 08:20:10 +00:00
gTests2 ( c . ModLogs , "modlog" )
gTests2 ( c . AdminLogs , "adminlog" )
2018-10-08 02:51:59 +00:00
}
2021-05-02 08:47:19 +00:00
func TestRegLogs ( t * testing . T ) {
miscinit ( t )
if ! c . PluginsInited {
c . InitPlugins ( )
}
exf := expf ( t )
mustNone := func ( ) {
exf ( c . RegLogs . Count ( ) == 0 , "count should be %d not %d" , 0 , c . RegLogs . Count ( ) )
items , e := c . RegLogs . GetOffset ( 0 , 10 )
expectNilErr ( t , e )
exf ( len ( items ) == 0 , "len(items) should be %d not %d" , 0 , len ( items ) )
expectNilErr ( t , c . RegLogs . Purge ( ) )
exf ( c . RegLogs . Count ( ) == 0 , "count should be %d not %d" , 0 , c . RegLogs . Count ( ) )
items , e = c . RegLogs . GetOffset ( 0 , 10 )
expectNilErr ( t , e )
exf ( len ( items ) == 0 , "len(items) should be %d not %d" , 0 , len ( items ) )
}
mustNone ( )
regLog := & c . RegLogItem { Username : "Aa" , Email : "aa@example.com" , FailureReason : "fake" , Success : false , IP : "" }
id , e := regLog . Create ( )
exf ( id == 1 , "id should be %d not %d" , 1 , id )
expectNilErr ( t , e )
exf ( c . RegLogs . Count ( ) == 1 , "count should be %d not %d" , 1 , c . RegLogs . Count ( ) )
items , e := c . RegLogs . GetOffset ( 0 , 10 )
expectNilErr ( t , e )
exf ( len ( items ) == 1 , "len(items) should be %d not %d" , 1 , len ( items ) )
// TODO: Add more tests
expectNilErr ( t , c . RegLogs . DeleteOlderThanDays ( 2 ) )
exf ( c . RegLogs . Count ( ) == 1 , "count should be %d not %d" , 1 , c . RegLogs . Count ( ) )
items , e = c . RegLogs . GetOffset ( 0 , 10 )
expectNilErr ( t , e )
exf ( len ( items ) == 1 , "len(items) should be %d not %d" , 1 , len ( items ) )
// TODO: Add more tests
// TODO: Commit() test?
dayAgo := time . Now ( ) . AddDate ( 0 , 0 , - 5 )
items [ 0 ] . DoneAt = dayAgo . Format ( "2006-01-02 15:04:05" )
expectNilErr ( t , items [ 0 ] . Commit ( ) )
exf ( c . RegLogs . Count ( ) == 1 , "count should be %d not %d" , 1 , c . RegLogs . Count ( ) )
items , e = c . RegLogs . GetOffset ( 0 , 10 )
expectNilErr ( t , e )
exf ( len ( items ) == 1 , "len(items) should be %d not %d" , 1 , len ( items ) )
// TODO: Add more tests
expectNilErr ( t , c . RegLogs . DeleteOlderThanDays ( 2 ) )
mustNone ( )
regLog = & c . RegLogItem { Username : "Aa" , Email : "aa@example.com" , FailureReason : "fake" , Success : false , IP : "" }
id , e = regLog . Create ( )
exf ( id == 2 , "id should be %d not %d" , 2 , id )
expectNilErr ( t , e )
exf ( c . RegLogs . Count ( ) == 1 , "count should be %d not %d" , 1 , c . RegLogs . Count ( ) )
items , e = c . RegLogs . GetOffset ( 0 , 10 )
expectNilErr ( t , e )
exf ( len ( items ) == 1 , "len(items) should be %d not %d" , 1 , len ( items ) )
// TODO: Add more tests
expectNilErr ( t , c . RegLogs . Purge ( ) )
mustNone ( )
// TODO: Add more tests
}
func TestLoginLogs ( t * testing . T ) {
miscinit ( t )
if ! c . PluginsInited {
c . InitPlugins ( )
}
ex , exf := exp ( t ) , expf ( t )
uid , e := c . Users . Create ( "Log Test" , "Log Test" , "" , 1 , true )
expectNilErr ( t , e )
exf ( c . LoginLogs . CountUser ( - 1 ) == 0 , "countuser(-1) should be %d not %d" , 0 , c . LoginLogs . CountUser ( - 1 ) )
exf ( c . LoginLogs . CountUser ( 0 ) == 0 , "countuser(0) should be %d not %d" , 0 , c . LoginLogs . CountUser ( 0 ) )
exf ( c . LoginLogs . CountUser ( 1 ) == 0 , "countuser(1) should be %d not %d" , 0 , c . LoginLogs . CountUser ( 1 ) )
goNone := func ( uid , offset , perPage int ) {
items , e := c . LoginLogs . GetOffset ( uid , offset , perPage )
expectNilErr ( t , e )
exf ( len ( items ) == 0 , "len(items) should be %d not %d" , 0 , len ( items ) )
}
goNone ( - 1 , 0 , 10 )
goNone ( 0 , 0 , 10 )
goNone ( 1 , 0 , 10 )
goNone ( 1 , 1 , 10 )
goNone ( 1 , 0 , 0 )
mustNone := func ( ) {
exf ( c . LoginLogs . Count ( ) == 0 , "count should be %d not %d" , 0 , c . LoginLogs . Count ( ) )
exf ( c . LoginLogs . CountUser ( uid ) == 0 , "countuser(%d) should be %d not %d" , uid , 0 , c . LoginLogs . CountUser ( uid ) )
goNone ( uid , 0 , 10 )
goNone ( uid , 1 , 10 )
goNone ( uid , 0 , 0 )
}
mustNone ( )
logItem := & c . LoginLogItem { UID : uid , Success : true , IP : "" }
_ , e = logItem . Create ( )
expectNilErr ( t , e )
exf ( c . LoginLogs . Count ( ) == 1 , "count should be %d not %d" , 1 , c . LoginLogs . Count ( ) )
exf ( c . LoginLogs . CountUser ( uid ) == 1 , "countuser(%d) should be %d not %d" , uid , 1 , c . LoginLogs . CountUser ( uid ) )
items , e := c . LoginLogs . GetOffset ( uid , 0 , 10 )
expectNilErr ( t , e )
exf ( len ( items ) == 1 , "len(items) should be %d not %d" , 1 , len ( items ) )
// TODO: More tests
exf ( items [ 0 ] . UID == uid , "UID should be %d not %d" , uid , items [ 0 ] . UID )
ex ( items [ 0 ] . Success , "Success should be true" )
ex ( items [ 0 ] . IP == "" , "IP should be blank" )
goNone ( uid , 1 , 10 )
goNone ( uid , 0 , 0 )
dayAgo := time . Now ( ) . AddDate ( 0 , 0 , - 5 )
items [ 0 ] . DoneAt = dayAgo . Format ( "2006-01-02 15:04:05" )
prevDoneAt := items [ 0 ] . DoneAt
expectNilErr ( t , items [ 0 ] . Commit ( ) )
items , e = c . LoginLogs . GetOffset ( uid , 0 , 10 )
expectNilErr ( t , e )
exf ( len ( items ) == 1 , "len(items) should be %d not %d" , 1 , len ( items ) )
// TODO: More tests
exf ( items [ 0 ] . UID == uid , "UID should be %d not %d" , uid , items [ 0 ] . UID )
ex ( items [ 0 ] . Success , "Success should be true" )
ex ( items [ 0 ] . IP == "" , "IP should be blank" )
exf ( items [ 0 ] . DoneAt == prevDoneAt , "DoneAt should be %s not %s" , prevDoneAt , items [ 0 ] . DoneAt )
goNone ( uid , 1 , 10 )
goNone ( uid , 0 , 0 )
expectNilErr ( t , c . LoginLogs . DeleteOlderThanDays ( 2 ) )
mustNone ( )
logItem = & c . LoginLogItem { UID : uid , Success : true , IP : "" }
_ , e = logItem . Create ( )
expectNilErr ( t , e )
exf ( c . LoginLogs . Count ( ) == 1 , "count should be %d not %d" , 1 , c . LoginLogs . Count ( ) )
exf ( c . LoginLogs . CountUser ( uid ) == 1 , "countuser(%d) should be %d not %d" , uid , 1 , c . LoginLogs . CountUser ( uid ) )
items , e = c . LoginLogs . GetOffset ( uid , 0 , 10 )
expectNilErr ( t , e )
exf ( len ( items ) == 1 , "len(items) should be %d not %d" , 1 , len ( items ) )
// TODO: More tests
exf ( items [ 0 ] . UID == uid , "UID should be %d not %d" , uid , items [ 0 ] . UID )
ex ( items [ 0 ] . Success , "Success should be true" )
ex ( items [ 0 ] . IP == "" , "IP should be blank" )
goNone ( uid , 1 , 10 )
goNone ( uid , 0 , 0 )
expectNilErr ( t , c . LoginLogs . Purge ( ) )
mustNone ( )
}
2018-10-08 02:51:59 +00:00
2018-07-29 10:54:12 +00:00
func TestPluginManager ( t * testing . T ) {
2018-11-01 06:43:56 +00:00
miscinit ( t )
2019-04-19 08:20:10 +00:00
if ! c . PluginsInited {
c . InitPlugins ( )
2018-07-29 10:54:12 +00:00
}
2021-03-24 12:48:36 +00:00
ex := exp ( t )
2018-07-29 10:54:12 +00:00
2019-04-19 08:20:10 +00:00
_ , ok := c . Plugins [ "fairy-dust" ]
2021-03-24 12:48:36 +00:00
ex ( ! ok , "Plugin fairy-dust shouldn't exist" )
2020-02-09 10:00:08 +00:00
pl , ok := c . Plugins [ "bbcode" ]
2021-03-24 12:48:36 +00:00
ex ( ok , "Plugin bbcode should exist" )
ex ( ! pl . Installable , "Plugin bbcode shouldn't be installable" )
ex ( ! pl . Installed , "Plugin bbcode shouldn't be 'installed'" )
ex ( ! pl . Active , "Plugin bbcode shouldn't be active" )
2021-05-02 08:47:19 +00:00
active , e := pl . BypassActive ( )
expectNilErr ( t , e )
2021-03-24 12:48:36 +00:00
ex ( ! active , "Plugin bbcode shouldn't be active in the database either" )
2021-05-02 08:47:19 +00:00
hasPlugin , e := pl . InDatabase ( )
expectNilErr ( t , e )
2021-03-24 12:48:36 +00:00
ex ( ! hasPlugin , "Plugin bbcode shouldn't exist in the database" )
2018-07-29 10:54:12 +00:00
// TODO: Add some test cases for SetActive and SetInstalled before calling AddToDatabase
2020-02-09 10:00:08 +00:00
expectNilErr ( t , pl . AddToDatabase ( true , false ) )
2021-03-24 12:48:36 +00:00
ex ( ! pl . Installable , "Plugin bbcode shouldn't be installable" )
ex ( ! pl . Installed , "Plugin bbcode shouldn't be 'installed'" )
ex ( pl . Active , "Plugin bbcode should be active" )
2021-05-02 08:47:19 +00:00
active , e = pl . BypassActive ( )
expectNilErr ( t , e )
2021-03-24 12:48:36 +00:00
ex ( active , "Plugin bbcode should be active in the database too" )
2021-05-02 08:47:19 +00:00
hasPlugin , e = pl . InDatabase ( )
expectNilErr ( t , e )
2021-03-24 12:48:36 +00:00
ex ( hasPlugin , "Plugin bbcode should exist in the database" )
ex ( pl . Init != nil , "Plugin bbcode should have an init function" )
2020-02-09 10:00:08 +00:00
expectNilErr ( t , pl . Init ( pl ) )
2018-07-29 10:54:12 +00:00
2020-02-09 10:00:08 +00:00
expectNilErr ( t , pl . SetActive ( true ) )
2021-03-24 12:48:36 +00:00
ex ( ! pl . Installable , "Plugin bbcode shouldn't be installable" )
ex ( ! pl . Installed , "Plugin bbcode shouldn't be 'installed'" )
ex ( pl . Active , "Plugin bbcode should still be active" )
2021-05-02 08:47:19 +00:00
active , e = pl . BypassActive ( )
expectNilErr ( t , e )
2021-03-24 12:48:36 +00:00
ex ( active , "Plugin bbcode should still be active in the database too" )
2021-05-02 08:47:19 +00:00
hasPlugin , e = pl . InDatabase ( )
expectNilErr ( t , e )
2021-03-24 12:48:36 +00:00
ex ( hasPlugin , "Plugin bbcode should still exist in the database" )
2018-07-29 10:54:12 +00:00
2020-02-09 10:00:08 +00:00
expectNilErr ( t , pl . SetActive ( false ) )
2021-03-24 12:48:36 +00:00
ex ( ! pl . Installable , "Plugin bbcode shouldn't be installable" )
ex ( ! pl . Installed , "Plugin bbcode shouldn't be 'installed'" )
ex ( ! pl . Active , "Plugin bbcode shouldn't be active" )
2021-05-02 08:47:19 +00:00
active , e = pl . BypassActive ( )
expectNilErr ( t , e )
2021-03-24 12:48:36 +00:00
ex ( ! active , "Plugin bbcode shouldn't be active in the database" )
2021-05-02 08:47:19 +00:00
hasPlugin , e = pl . InDatabase ( )
expectNilErr ( t , e )
2021-03-24 12:48:36 +00:00
ex ( hasPlugin , "Plugin bbcode should still exist in the database" )
ex ( pl . Deactivate != nil , "Plugin bbcode should have an init function" )
2020-02-09 10:00:08 +00:00
pl . Deactivate ( pl ) // Returns nothing
2018-07-29 10:54:12 +00:00
// Not installable, should not be mutated
2021-03-24 12:48:36 +00:00
ex ( pl . SetInstalled ( true ) == c . ErrPluginNotInstallable , "Plugin was set as installed despite not being installable" )
ex ( ! pl . Installable , "Plugin bbcode shouldn't be installable" )
ex ( ! pl . Installed , "Plugin bbcode shouldn't be 'installed'" )
ex ( ! pl . Active , "Plugin bbcode shouldn't be active" )
2021-05-02 08:47:19 +00:00
active , e = pl . BypassActive ( )
expectNilErr ( t , e )
2021-03-24 12:48:36 +00:00
ex ( ! active , "Plugin bbcode shouldn't be active in the database either" )
2021-05-02 08:47:19 +00:00
hasPlugin , e = pl . InDatabase ( )
expectNilErr ( t , e )
2021-03-24 12:48:36 +00:00
ex ( hasPlugin , "Plugin bbcode should still exist in the database" )
2018-07-29 10:54:12 +00:00
2021-03-24 12:48:36 +00:00
ex ( pl . SetInstalled ( false ) == c . ErrPluginNotInstallable , "Plugin was set as not installed despite not being installable" )
ex ( ! pl . Installable , "Plugin bbcode shouldn't be installable" )
ex ( ! pl . Installed , "Plugin bbcode shouldn't be 'installed'" )
ex ( ! pl . Active , "Plugin bbcode shouldn't be active" )
2021-05-02 08:47:19 +00:00
active , e = pl . BypassActive ( )
expectNilErr ( t , e )
2021-03-24 12:48:36 +00:00
ex ( ! active , "Plugin bbcode shouldn't be active in the database either" )
2021-05-02 08:47:19 +00:00
hasPlugin , e = pl . InDatabase ( )
expectNilErr ( t , e )
2021-03-24 12:48:36 +00:00
ex ( hasPlugin , "Plugin bbcode should still exist in the database" )
2018-07-29 10:54:12 +00:00
// This isn't really installable, but we want to get a few tests done before getting plugins which are stateful
2020-02-09 10:00:08 +00:00
pl . Installable = true
expectNilErr ( t , pl . SetInstalled ( true ) )
2021-03-24 12:48:36 +00:00
ex ( pl . Installable , "Plugin bbcode should be installable" )
ex ( pl . Installed , "Plugin bbcode should be 'installed'" )
ex ( ! pl . Active , "Plugin bbcode shouldn't be active" )
2021-05-02 08:47:19 +00:00
active , e = pl . BypassActive ( )
expectNilErr ( t , e )
2021-03-24 12:48:36 +00:00
ex ( ! active , "Plugin bbcode shouldn't be active in the database either" )
2021-05-02 08:47:19 +00:00
hasPlugin , e = pl . InDatabase ( )
expectNilErr ( t , e )
2021-03-24 12:48:36 +00:00
ex ( hasPlugin , "Plugin bbcode should still exist in the database" )
2018-07-29 10:54:12 +00:00
2020-02-09 10:00:08 +00:00
expectNilErr ( t , pl . SetInstalled ( false ) )
2021-03-24 12:48:36 +00:00
ex ( pl . Installable , "Plugin bbcode should be installable" )
ex ( ! pl . Installed , "Plugin bbcode shouldn't be 'installed'" )
ex ( ! pl . Active , "Plugin bbcode shouldn't be active" )
2021-05-02 08:47:19 +00:00
active , e = pl . BypassActive ( )
expectNilErr ( t , e )
2021-03-24 12:48:36 +00:00
ex ( ! active , "Plugin bbcode shouldn't be active in the database either" )
2021-05-02 08:47:19 +00:00
hasPlugin , e = pl . InDatabase ( )
expectNilErr ( t , e )
2021-03-24 12:48:36 +00:00
ex ( hasPlugin , "Plugin bbcode should still exist in the database" )
2018-07-29 13:02:48 +00:00
// Bugs sometimes arise when we try to delete a hook when there are multiple, so test for that
// TODO: Do a finer grained test for that case...? A bigger test might catch more odd cases with multiple plugins
2020-02-09 10:00:08 +00:00
pl2 , ok := c . Plugins [ "markdown" ]
2021-03-24 12:48:36 +00:00
ex ( ok , "Plugin markdown should exist" )
ex ( ! pl2 . Installable , "Plugin markdown shouldn't be installable" )
ex ( ! pl2 . Installed , "Plugin markdown shouldn't be 'installed'" )
ex ( ! pl2 . Active , "Plugin markdown shouldn't be active" )
2021-05-02 08:47:19 +00:00
active , e = pl2 . BypassActive ( )
expectNilErr ( t , e )
2021-03-24 12:48:36 +00:00
ex ( ! active , "Plugin markdown shouldn't be active in the database either" )
2021-05-02 08:47:19 +00:00
hasPlugin , e = pl2 . InDatabase ( )
expectNilErr ( t , e )
2021-03-24 12:48:36 +00:00
ex ( ! hasPlugin , "Plugin markdown shouldn't exist in the database" )
2018-07-29 13:02:48 +00:00
2020-02-09 10:00:08 +00:00
expectNilErr ( t , pl2 . AddToDatabase ( true , false ) )
expectNilErr ( t , pl2 . Init ( pl2 ) )
expectNilErr ( t , pl . SetActive ( true ) )
expectNilErr ( t , pl . Init ( pl ) )
pl2 . Deactivate ( pl2 )
expectNilErr ( t , pl2 . SetActive ( false ) )
pl . Deactivate ( pl )
expectNilErr ( t , pl . SetActive ( false ) )
2018-07-31 04:02:09 +00:00
// Hook tests
2019-10-06 01:49:31 +00:00
ht := func ( ) * c . HookTable {
return c . GetHookTable ( )
}
2021-03-24 12:48:36 +00:00
ex ( ht ( ) . Sshook ( "haha" , "ho" ) == "ho" , "Sshook shouldn't have anything bound to it yet" )
2018-10-21 13:54:32 +00:00
handle := func ( in string ) ( out string ) {
2018-07-31 04:02:09 +00:00
return in + "hi"
}
2020-02-09 10:00:08 +00:00
pl . AddHook ( "haha" , handle )
2021-03-24 12:48:36 +00:00
ex ( ht ( ) . Sshook ( "haha" , "ho" ) == "hohi" , "Sshook didn't give hohi" )
2020-02-09 10:00:08 +00:00
pl . RemoveHook ( "haha" , handle )
2021-03-24 12:48:36 +00:00
ex ( ht ( ) . Sshook ( "haha" , "ho" ) == "ho" , "Sshook shouldn't have anything bound to it anymore" )
2018-10-21 13:54:32 +00:00
2021-03-24 12:48:36 +00:00
/ * ex ( ht ( ) . Hook ( "haha" , "ho" ) == "ho" , "Hook shouldn't have anything bound to it yet" )
2018-10-21 13:54:32 +00:00
handle2 := func ( inI interface { } ) ( out interface { } ) {
return inI . ( string ) + "hi"
}
2020-02-09 10:00:08 +00:00
pl . AddHook ( "hehe" , handle2 )
2021-03-24 12:48:36 +00:00
ex ( ht ( ) . Hook ( "hehe" , "ho" ) . ( string ) == "hohi" , "Hook didn't give hohi" )
2020-02-09 10:00:08 +00:00
pl . RemoveHook ( "hehe" , handle2 )
2021-03-24 12:48:36 +00:00
ex ( ht ( ) . Hook ( "hehe" , "ho" ) . ( string ) == "ho" , "Hook shouldn't have anything bound to it anymore" ) * /
2018-07-31 04:02:09 +00:00
// TODO: Add tests for more hook types
}
func TestPhrases ( t * testing . T ) {
2019-10-06 11:32:00 +00:00
getPhrase := phrases . GetPermPhrase
2020-02-09 10:00:08 +00:00
tp := func ( name , expects string ) {
2019-10-06 01:49:31 +00:00
res := getPhrase ( name )
expect ( t , res == expects , "Not the expected phrase, got '" + res + "' instead" )
2019-10-06 00:34:09 +00:00
}
2019-10-06 01:49:31 +00:00
tp ( "BanUsers" , "Can ban users" )
tp ( "NoSuchPerm" , "{lang.perms[NoSuchPerm]}" )
tp ( "ViewTopic" , "Can view topics" )
tp ( "NoSuchPerm" , "{lang.perms[NoSuchPerm]}" )
2018-07-31 04:02:09 +00:00
// TODO: Cover the other phrase types, also try switching between languages to see if anything strange happens
2018-07-29 10:54:12 +00:00
}
2019-05-10 03:52:37 +00:00
func TestMetaStore ( t * testing . T ) {
2021-05-02 08:47:19 +00:00
ex , exf := exp ( t ) , expf ( t )
m , e := c . Meta . Get ( "magic" )
ex ( m == "" , "meta var magic should be empty" )
recordMustNotExist ( t , e , "meta var magic should not exist" )
2019-05-11 23:07:24 +00:00
2021-05-02 08:47:19 +00:00
expectVal := func ( name , expect string ) {
m , e = c . Meta . Get ( name )
expectNilErr ( t , e )
exf ( m == expect , "meta var %s should be %s" , name , expect )
}
2019-05-11 23:07:24 +00:00
2021-05-02 08:47:19 +00:00
expectNilErr ( t , c . Meta . Set ( "magic" , "lol" ) )
expectVal ( "magic" , "lol" )
2021-03-02 07:22:32 +00:00
expectNilErr ( t , c . Meta . Set ( "magic" , "wha" ) )
2021-05-02 08:47:19 +00:00
expectVal ( "magic" , "wha" )
2019-05-11 23:07:24 +00:00
2021-05-02 08:47:19 +00:00
m , e = c . Meta . Get ( "giggle" )
ex ( m == "" , "meta var giggle should be empty" )
recordMustNotExist ( t , e , "meta var giggle should not exist" )
2019-05-11 23:07:24 +00:00
2021-05-02 08:47:19 +00:00
expectNilErr ( t , c . Meta . SetInt ( "magic" , 1 ) )
expectVal ( "magic" , "1" )
expectNilErr ( t , c . Meta . SetInt64 ( "magic" , 5 ) )
expectVal ( "magic" , "5" )
2019-05-10 03:52:37 +00:00
}
2019-11-09 02:34:12 +00:00
func TestPages ( t * testing . T ) {
2021-03-24 12:48:36 +00:00
ex := exp ( t )
ex ( c . Pages . Count ( ) == 0 , "Page count should be 0" )
2021-05-02 08:47:19 +00:00
_ , e := c . Pages . Get ( 1 )
recordMustNotExist ( t , e , "Page 1 should not exist yet" )
2019-11-09 02:34:12 +00:00
expectNilErr ( t , c . Pages . Delete ( - 1 ) )
expectNilErr ( t , c . Pages . Delete ( 0 ) )
expectNilErr ( t , c . Pages . Delete ( 1 ) )
2021-05-02 08:47:19 +00:00
_ , e = c . Pages . Get ( 1 )
recordMustNotExist ( t , e , "Page 1 should not exist yet" )
//e = c.Pages.Reload(1)
//recordMustNotExist(t,e,"Page 1 should not exist yet")
2019-11-09 02:34:12 +00:00
ipage := c . BlankCustomPage ( )
ipage . Name = "test"
ipage . Title = "Test"
ipage . Body = "A test page"
2021-05-02 08:47:19 +00:00
pid , e := ipage . Create ( )
expectNilErr ( t , e )
2021-03-24 12:48:36 +00:00
ex ( pid == 1 , "The first page should have an ID of 1" )
ex ( c . Pages . Count ( ) == 1 , "Page count should be 1" )
2019-11-09 02:34:12 +00:00
2021-05-02 08:47:19 +00:00
test := func ( pid int , ep * c . CustomPage ) {
p , e := c . Pages . Get ( pid )
expectNilErr ( t , e )
ex ( p . Name == ep . Name , "The page name should be " + ep . Name )
ex ( p . Title == ep . Title , "The page title should be " + ep . Title )
ex ( p . Body == ep . Body , "The page body should be " + ep . Body )
}
test ( 1 , ipage )
2019-11-09 02:34:12 +00:00
opage , err := c . Pages . Get ( 1 )
expectNilErr ( t , err )
opage . Name = "t"
opage . Title = "T"
opage . Body = "testing"
expectNilErr ( t , opage . Commit ( ) )
2021-05-02 08:47:19 +00:00
test ( 1 , opage )
2019-11-09 02:34:12 +00:00
2021-03-24 12:48:36 +00:00
expectNilErr ( t , c . Pages . Delete ( 1 ) )
ex ( c . Pages . Count ( ) == 0 , "Page count should be 0" )
2021-05-02 08:47:19 +00:00
_ , e = c . Pages . Get ( 1 )
recordMustNotExist ( t , e , "Page 1 should not exist" )
//e = c.Pages.Reload(1)
//recordMustNotExist(t,e,"Page 1 should not exist")
2019-11-09 02:34:12 +00:00
// TODO: More tests
}
2018-08-04 11:46:36 +00:00
func TestWordFilters ( t * testing . T ) {
2021-03-24 12:48:36 +00:00
ex , exf := exp ( t ) , expf ( t )
2018-08-04 11:46:36 +00:00
// TODO: Test the word filters and their store
2021-03-24 12:48:36 +00:00
ex ( c . WordFilters . Length ( ) == 0 , "Word filter list should be empty" )
ex ( c . WordFilters . EstCount ( ) == 0 , "Word filter list should be empty" )
ex ( c . WordFilters . Count ( ) == 0 , "Word filter list should be empty" )
2019-04-19 08:20:10 +00:00
filters , err := c . WordFilters . GetAll ( )
2018-08-04 11:46:36 +00:00
expectNilErr ( t , err ) // TODO: Slightly confusing that we don't get ErrNoRow here
2021-03-24 12:48:36 +00:00
ex ( len ( filters ) == 0 , "Word filter map should be empty" )
2018-08-04 11:46:36 +00:00
// TODO: Add a test for ParseMessage relating to word filters
2019-11-09 02:34:12 +00:00
_ , err = c . WordFilters . Get ( 1 )
recordMustNotExist ( t , err , "filter 1 should not exist" )
2018-08-04 11:46:36 +00:00
2019-11-08 22:07:05 +00:00
wfid , err := c . WordFilters . Create ( "imbecile" , "lovely" )
2018-08-04 11:46:36 +00:00
expectNilErr ( t , err )
2021-03-24 12:48:36 +00:00
ex ( wfid == 1 , "The first word filter should have an ID of 1" )
ex ( c . WordFilters . Length ( ) == 1 , "Word filter list should not be empty" )
ex ( c . WordFilters . EstCount ( ) == 1 , "Word filter list should not be empty" )
ex ( c . WordFilters . Count ( ) == 1 , "Word filter list should not be empty" )
2019-11-08 22:07:05 +00:00
2021-03-02 07:22:32 +00:00
ftest := func ( f * c . WordFilter , id int , find , replace string ) {
2021-03-24 12:48:36 +00:00
exf ( f . ID == id , "Word filter ID should be %d, not %d" , id , f . ID )
exf ( f . Find == find , "Word filter needle should be '%s', not '%s'" , find , f . Find )
exf ( f . Replace == replace , "Word filter replacement should be '%s', not '%s'" , replace , f . Replace )
2021-03-02 07:22:32 +00:00
}
2019-04-19 08:20:10 +00:00
filters , err = c . WordFilters . GetAll ( )
2018-08-04 11:46:36 +00:00
expectNilErr ( t , err )
2021-03-24 12:48:36 +00:00
ex ( len ( filters ) == 1 , "Word filter map should not be empty" )
2021-03-02 07:22:32 +00:00
ftest ( filters [ 1 ] , 1 , "imbecile" , "lovely" )
2019-11-08 22:07:05 +00:00
2021-03-02 07:22:32 +00:00
filter , err := c . WordFilters . Get ( 1 )
2019-11-08 22:07:05 +00:00
expectNilErr ( t , err )
2021-03-02 07:22:32 +00:00
ftest ( filter , 1 , "imbecile" , "lovely" )
2019-11-08 22:07:05 +00:00
2019-11-09 02:34:12 +00:00
// Update
expectNilErr ( t , c . WordFilters . Update ( 1 , "b" , "a" ) )
2021-03-24 12:48:36 +00:00
ex ( c . WordFilters . Length ( ) == 1 , "Word filter list should not be empty" )
ex ( c . WordFilters . EstCount ( ) == 1 , "Word filter list should not be empty" )
ex ( c . WordFilters . Count ( ) == 1 , "Word filter list should not be empty" )
2019-11-09 02:34:12 +00:00
filters , err = c . WordFilters . GetAll ( )
expectNilErr ( t , err )
2021-03-24 12:48:36 +00:00
ex ( len ( filters ) == 1 , "Word filter map should not be empty" )
2021-03-02 07:22:32 +00:00
ftest ( filters [ 1 ] , 1 , "b" , "a" )
2019-11-09 02:34:12 +00:00
filter , err = c . WordFilters . Get ( 1 )
expectNilErr ( t , err )
2021-03-02 07:22:32 +00:00
ftest ( filter , 1 , "b" , "a" )
2019-11-09 02:34:12 +00:00
2018-08-04 11:46:36 +00:00
// TODO: Add a test for ParseMessage relating to word filters
2021-03-02 07:22:32 +00:00
expectNilErr ( t , c . WordFilters . Delete ( 1 ) )
2019-11-09 02:34:12 +00:00
2021-03-24 12:48:36 +00:00
ex ( c . WordFilters . Length ( ) == 0 , "Word filter list should be empty" )
ex ( c . WordFilters . EstCount ( ) == 0 , "Word filter list should be empty" )
ex ( c . WordFilters . Count ( ) == 0 , "Word filter list should be empty" )
2019-11-09 02:34:12 +00:00
filters , err = c . WordFilters . GetAll ( )
expectNilErr ( t , err ) // TODO: Slightly confusing that we don't get ErrNoRow here
2021-03-24 12:48:36 +00:00
ex ( len ( filters ) == 0 , "Word filter map should be empty" )
2019-11-09 02:34:12 +00:00
_ , err = c . WordFilters . Get ( 1 )
recordMustNotExist ( t , err , "filter 1 should not exist" )
// TODO: Any more tests we could do?
2018-08-04 11:46:36 +00:00
}
2020-04-02 20:19:42 +00:00
func TestMFAStore ( t * testing . T ) {
2021-03-24 12:48:36 +00:00
exf := expf ( t )
2020-04-02 20:19:42 +00:00
2021-05-02 08:47:19 +00:00
mustNone := func ( ) {
_ , e := c . MFAstore . Get ( - 1 )
recordMustNotExist ( t , e , "mfa uid -1 should not exist" )
_ , e = c . MFAstore . Get ( 0 )
recordMustNotExist ( t , e , "mfa uid 0 should not exist" )
_ , e = c . MFAstore . Get ( 1 )
recordMustNotExist ( t , e , "mfa uid 1 should not exist" )
}
mustNone ( )
secret , e := c . GenerateGAuthSecret ( )
expectNilErr ( t , e )
2020-04-02 20:19:42 +00:00
expectNilErr ( t , c . MFAstore . Create ( secret , 1 ) )
2021-05-02 08:47:19 +00:00
_ , e = c . MFAstore . Get ( 0 )
recordMustNotExist ( t , e , "mfa uid 0 should not exist" )
2020-04-02 20:19:42 +00:00
var scratches [ ] string
2021-05-02 08:47:19 +00:00
it , e := c . MFAstore . Get ( 1 )
2020-04-02 20:19:42 +00:00
test := func ( j int ) {
2021-05-02 08:47:19 +00:00
expectNilErr ( t , e )
2021-03-24 12:48:36 +00:00
exf ( it . UID == 1 , "UID should be 1 not %d" , it . UID )
exf ( it . Secret == secret , "Secret should be '%s' not %s" , secret , it . Secret )
exf ( len ( it . Scratch ) == 8 , "Scratch should be 8 not %d" , len ( it . Scratch ) )
2020-04-02 20:19:42 +00:00
for i , scratch := range it . Scratch {
2021-03-24 12:48:36 +00:00
exf ( scratch != "" , "scratch %d should not be empty" , i )
2020-04-02 20:19:42 +00:00
if scratches != nil {
if j == i {
2021-03-24 12:48:36 +00:00
exf ( scratches [ i ] != scratch , "scratches[%d] should not be %s" , i , scratches [ i ] )
2020-04-02 20:19:42 +00:00
} else {
2021-03-24 12:48:36 +00:00
exf ( scratches [ i ] == scratch , "scratches[%d] should be %s not %s" , i , scratches [ i ] , scratch )
2020-04-02 20:19:42 +00:00
}
}
}
scratches = make ( [ ] string , 8 )
copy ( scratches , it . Scratch )
}
test ( 0 )
for i := 0 ; i < len ( scratches ) ; i ++ {
expectNilErr ( t , it . BurnScratch ( i ) )
2021-05-02 08:47:19 +00:00
it , e = c . MFAstore . Get ( 1 )
2020-04-02 20:19:42 +00:00
test ( i )
}
2021-05-02 08:47:19 +00:00
token , e := gauth . GetTOTPToken ( secret )
expectNilErr ( t , e )
2020-04-02 22:57:46 +00:00
expectNilErr ( t , c . Auth . ValidateMFAToken ( token , 1 ) )
2020-04-02 22:24:32 +00:00
expectNilErr ( t , it . Delete ( ) )
2021-05-02 08:47:19 +00:00
mustNone ( )
2020-04-02 20:19:42 +00:00
}
2019-01-21 12:27:59 +00:00
// TODO: Expand upon the valid characters which can go in URLs?
Added the Social Groups plugin. This is still under construction.
Made a few improvements to the ForumStore, including bringing it's API closer in line with the other datastores, adding stubs for future subforum functionality, and improving efficiency in a few places.
The auth interface now handles all the authentication stuff.
Renamed the debug config variable to debug_mode.
Added the PluginPerms API.
Internal Errors will now dump the stack trace in the console.
Added support for installable plugins.
Refactored the routing logic so that the router now handles the common PreRoute logic(exc. /static/)
Added the CreateTable method to the query generator. It might need some tweaking to better support other database systems.
Added the same CreateTable method to the query builder.
Began work on PostgreSQL support.
Added the string-string hook type
Added the pre_render hook type.
Added the ParentID and ParentType fields to forums.
Added the get_forum_url_prefix function.
Added a more generic build_slug function.
Added the get_topic_url_prefix function.
Added the override_perms and override_forum_perms functions for bulk setting and unsetting permissions.
Added more ExtData fields in a few structs and removed them on the Perms struct as the PluginPerms API supersedes them there.
Plugins can now see the router instance.
The plugin initialisation handlers can now throw errors.
Plugins are now initialised after all the forum's subsystems are.
Refactored the unit test logic. For instance, we now use the proper .Log method rather than fmt.Println in many cases.
Sorry, we'll have to break Github's generated file detection, as the build instructions aren't working, unless I put them at the top, and they're far, far more important than getting Github to recognise the generated code as generated code.
Fixed an issue with mysql.go's _init_database() overwriting the dbpassword variable. Not a huge issue, but it is a "gotcha" for those not expecting a ':' at the start.
Fixed an issue with forum creation where the forum permissions didn't get cached.
Fixed a bug in plugin_bbcode where negative numbers in rand would crash Gosora.
Made the outputs of plugin_markdown and plugin_bbcode more compliant with the tests.
Revamped the phrase system to make it easier for us to add language pack related features in the future.
Added the WidgetMenu widget type.
Revamped the theme again. I'm experimenting to see which approach I like most.
- Excuse the little W3C rage. Some things about CSS drive me crazy :p
Tests:
Added 22 bbcode_full_parse tests.
Added 19 bbcode_regex_parse tests.
Added 27 markdown_parse tests.
Added four UserStore tests. More to come when the test database functionality is added.
Added 18 name_to_slug tests.
Hooks:
Added the pre_render hook.
Added the pre_render_forum_list hook.
Added the pre_render_view_forum hook.
Added the pre_render_topic_list hook.
Added the pre_render_view_topic hook.
Added the pre_render_profile hook.
Added the pre_render_custom_page hook.
Added the pre_render_overview hook.
Added the pre_render_create_topic hook.
Added the pre_render_account_own_edit_critical hook.
Added the pre_render_account_own_edit_avatar hook.
Added the pre_render_account_own_edit_username hook.
Added the pre_render_account_own_edit_email hook.
Added the pre_render_login hook.
Added the pre_render_register hook.
Added the pre_render_ban hook.
Added the pre_render_panel_dashboard hook.
Added the pre_render_panel_forums hook.
Added the pre_render_panel_delete_forum hook.
Added the pre_render_panel_edit_forum hook.
Added the pre_render_panel_settings hook.
Added the pre_render_panel_setting hook.
Added the pre_render_panel_plugins hook.
Added the pre_render_panel_users hook.
Added the pre_render_panel_edit_user hook.
Added the pre_render_panel_groups hook.
Added the pre_render_panel_edit_group hook.
Added the pre_render_panel_edit_group_perms hook.
Added the pre_render_panel_themes hook.
Added the pre_render_panel_mod_log hook.
Added the pre_render_error hook.
Added the pre_render_security_error hook.
Added the create_group_preappend hook.
Added the intercept_build_widgets hook.
Added the simple_forum_check_pre_perms hook.
Added the forum_check_pre_perms hook.
2017-07-09 12:06:04 +00:00
func TestSlugs ( t * testing . T ) {
2019-10-06 00:34:09 +00:00
l := & MEPairList { nil }
2019-04-19 08:20:10 +00:00
c . Config . BuildSlugs = true // Flip this switch, otherwise all the tests will fail
2017-09-03 04:50:31 +00:00
2019-10-06 00:34:09 +00:00
l . Add ( "Unknown" , "unknown" )
l . Add ( "Unknown2" , "unknown2" )
l . Add ( "Unknown " , "unknown" )
l . Add ( "Unknown 2" , "unknown-2" )
l . Add ( "Unknown 2" , "unknown-2" )
l . Add ( "Admin Alice" , "admin-alice" )
l . Add ( "Admin_Alice" , "adminalice" )
l . Add ( "Admin_Alice-" , "adminalice" )
l . Add ( "-Admin_Alice-" , "adminalice" )
l . Add ( "-Admin@Alice-" , "adminalice" )
l . Add ( "-Admin😀Alice-" , "adminalice" )
l . Add ( "u" , "u" )
l . Add ( "" , "untitled" )
l . Add ( " " , "untitled" )
l . Add ( "-" , "untitled" )
l . Add ( "--" , "untitled" )
l . Add ( "é" , "é" )
l . Add ( "-é-" , "é" )
l . Add ( "-你好-" , "untitled" )
l . Add ( "-こにちは-" , "untitled" )
for _ , item := range l . Items {
2017-09-03 04:50:31 +00:00
t . Log ( "Testing string '" + item . Msg + "'" )
2019-10-06 00:34:09 +00:00
res := c . NameToSlug ( item . Msg )
2017-09-03 04:50:31 +00:00
if res != item . Expects {
t . Error ( "Bad output:" , "'" + res + "'" )
t . Error ( "Expected:" , item . Expects )
}
}
Added the Social Groups plugin. This is still under construction.
Made a few improvements to the ForumStore, including bringing it's API closer in line with the other datastores, adding stubs for future subforum functionality, and improving efficiency in a few places.
The auth interface now handles all the authentication stuff.
Renamed the debug config variable to debug_mode.
Added the PluginPerms API.
Internal Errors will now dump the stack trace in the console.
Added support for installable plugins.
Refactored the routing logic so that the router now handles the common PreRoute logic(exc. /static/)
Added the CreateTable method to the query generator. It might need some tweaking to better support other database systems.
Added the same CreateTable method to the query builder.
Began work on PostgreSQL support.
Added the string-string hook type
Added the pre_render hook type.
Added the ParentID and ParentType fields to forums.
Added the get_forum_url_prefix function.
Added a more generic build_slug function.
Added the get_topic_url_prefix function.
Added the override_perms and override_forum_perms functions for bulk setting and unsetting permissions.
Added more ExtData fields in a few structs and removed them on the Perms struct as the PluginPerms API supersedes them there.
Plugins can now see the router instance.
The plugin initialisation handlers can now throw errors.
Plugins are now initialised after all the forum's subsystems are.
Refactored the unit test logic. For instance, we now use the proper .Log method rather than fmt.Println in many cases.
Sorry, we'll have to break Github's generated file detection, as the build instructions aren't working, unless I put them at the top, and they're far, far more important than getting Github to recognise the generated code as generated code.
Fixed an issue with mysql.go's _init_database() overwriting the dbpassword variable. Not a huge issue, but it is a "gotcha" for those not expecting a ':' at the start.
Fixed an issue with forum creation where the forum permissions didn't get cached.
Fixed a bug in plugin_bbcode where negative numbers in rand would crash Gosora.
Made the outputs of plugin_markdown and plugin_bbcode more compliant with the tests.
Revamped the phrase system to make it easier for us to add language pack related features in the future.
Added the WidgetMenu widget type.
Revamped the theme again. I'm experimenting to see which approach I like most.
- Excuse the little W3C rage. Some things about CSS drive me crazy :p
Tests:
Added 22 bbcode_full_parse tests.
Added 19 bbcode_regex_parse tests.
Added 27 markdown_parse tests.
Added four UserStore tests. More to come when the test database functionality is added.
Added 18 name_to_slug tests.
Hooks:
Added the pre_render hook.
Added the pre_render_forum_list hook.
Added the pre_render_view_forum hook.
Added the pre_render_topic_list hook.
Added the pre_render_view_topic hook.
Added the pre_render_profile hook.
Added the pre_render_custom_page hook.
Added the pre_render_overview hook.
Added the pre_render_create_topic hook.
Added the pre_render_account_own_edit_critical hook.
Added the pre_render_account_own_edit_avatar hook.
Added the pre_render_account_own_edit_username hook.
Added the pre_render_account_own_edit_email hook.
Added the pre_render_login hook.
Added the pre_render_register hook.
Added the pre_render_ban hook.
Added the pre_render_panel_dashboard hook.
Added the pre_render_panel_forums hook.
Added the pre_render_panel_delete_forum hook.
Added the pre_render_panel_edit_forum hook.
Added the pre_render_panel_settings hook.
Added the pre_render_panel_setting hook.
Added the pre_render_panel_plugins hook.
Added the pre_render_panel_users hook.
Added the pre_render_panel_edit_user hook.
Added the pre_render_panel_groups hook.
Added the pre_render_panel_edit_group hook.
Added the pre_render_panel_edit_group_perms hook.
Added the pre_render_panel_themes hook.
Added the pre_render_panel_mod_log hook.
Added the pre_render_error hook.
Added the pre_render_security_error hook.
Added the create_group_preappend hook.
Added the intercept_build_widgets hook.
Added the simple_forum_check_pre_perms hook.
Added the forum_check_pre_perms hook.
2017-07-09 12:06:04 +00:00
}
2017-07-12 11:05:18 +00:00
2019-01-21 12:27:59 +00:00
func TestWidgets ( t * testing . T ) {
2021-03-24 12:48:36 +00:00
ex , exf := exp ( t ) , expf ( t )
2021-05-02 08:47:19 +00:00
_ , e := c . Widgets . Get ( 1 )
recordMustNotExist ( t , e , "There shouldn't be any widgets by default" )
2019-04-19 08:20:10 +00:00
widgets := c . Docks . RightSidebar . Items
2021-03-24 12:48:36 +00:00
exf ( len ( widgets ) == 0 , "RightSidebar should have 0 items, not %d" , len ( widgets ) )
2019-01-21 12:27:59 +00:00
2019-04-19 08:20:10 +00:00
widget := & c . Widget { Position : 0 , Side : "rightSidebar" , Type : "simple" , Enabled : true , Location : "global" }
ewidget := & c . WidgetEdit { widget , map [ string ] string { "Name" : "Test" , "Text" : "Testing" } }
2021-05-02 08:47:19 +00:00
wid , e := ewidget . Create ( )
expectNilErr ( t , e )
2021-03-24 12:48:36 +00:00
ex ( wid == 1 , "wid should be 1" )
2019-01-21 12:27:59 +00:00
2020-02-04 11:47:03 +00:00
wtest := func ( w , w2 * c . Widget ) {
2021-03-24 12:48:36 +00:00
ex ( w . Position == w2 . Position , "wrong position" )
ex ( w . Side == w2 . Side , "wrong side" )
ex ( w . Type == w2 . Type , "wrong type" )
ex ( w . Enabled == w2 . Enabled , "wrong enabled" )
ex ( w . Location == w2 . Location , "wrong location" )
2020-02-04 11:47:03 +00:00
}
2019-01-21 12:27:59 +00:00
// TODO: Do a test for the widget body
2021-05-02 08:47:19 +00:00
widget2 , e := c . Widgets . Get ( 1 )
expectNilErr ( t , e )
2020-02-04 11:47:03 +00:00
wtest ( widget , widget2 )
2019-01-21 12:27:59 +00:00
2019-04-19 08:20:10 +00:00
widgets = c . Docks . RightSidebar . Items
2021-03-24 12:48:36 +00:00
exf ( len ( widgets ) == 1 , "RightSidebar should have 1 item, not %d" , len ( widgets ) )
2020-02-04 11:47:03 +00:00
wtest ( widget , widgets [ 0 ] )
2019-01-21 12:27:59 +00:00
widget2 . Enabled = false
2019-04-19 08:20:10 +00:00
ewidget = & c . WidgetEdit { widget2 , map [ string ] string { "Name" : "Test" , "Text" : "Testing" } }
2021-03-02 07:22:32 +00:00
expectNilErr ( t , ewidget . Commit ( ) )
2019-01-21 12:27:59 +00:00
2021-05-02 08:47:19 +00:00
widget2 , e = c . Widgets . Get ( 1 )
expectNilErr ( t , e )
2021-03-24 12:48:36 +00:00
widget . Enabled = false
wtest ( widget , widget2 )
2019-01-21 12:27:59 +00:00
2019-04-19 08:20:10 +00:00
widgets = c . Docks . RightSidebar . Items
2021-03-24 12:48:36 +00:00
exf ( len ( widgets ) == 1 , "RightSidebar should have 1 item, not %d" , len ( widgets ) )
widget . Enabled = false
wtest ( widget , widgets [ 0 ] )
2019-01-21 12:27:59 +00:00
2021-03-24 12:48:36 +00:00
expectNilErr ( t , widget2 . Delete ( ) )
2019-01-21 12:27:59 +00:00
2021-05-02 08:47:19 +00:00
_ , e = c . Widgets . Get ( 1 )
recordMustNotExist ( t , e , "There shouldn't be any widgets anymore" )
2019-04-19 08:20:10 +00:00
widgets = c . Docks . RightSidebar . Items
2021-03-24 12:48:36 +00:00
exf ( len ( widgets ) == 0 , "RightSidebar should have 0 items, not %d" , len ( widgets ) )
2019-01-21 12:27:59 +00:00
}
2021-04-07 14:23:11 +00:00
/ * type ForumActionStoreInt interface {
Get ( faid int ) ( * ForumAction , error )
GetInForum ( fid int ) ( [ ] * ForumAction , error )
GetAll ( ) ( [ ] * ForumAction , error )
GetNewTopicActions ( fid int ) ( [ ] * ForumAction , error )
Add ( fa * ForumAction ) ( int , error )
Delete ( faid int ) error
Exists ( faid int ) bool
Count ( ) int
CountInForum ( fid int ) int
DailyTick ( ) error
} * /
func TestForumActions ( t * testing . T ) {
ex , exf , s := exp ( t ) , expf ( t ) , c . ForumActionStore
count := s . CountInForum ( - 1 )
exf ( count == 0 , "count should be %d not %d" , 0 , count )
count = s . CountInForum ( 0 )
exf ( count == 0 , "count in 0 should be %d not %d" , 0 , count )
ex ( ! s . Exists ( - 1 ) , "faid -1 should not exist" )
ex ( ! s . Exists ( 0 ) , "faid 0 should not exist" )
_ , e := s . Get ( - 1 )
recordMustNotExist ( t , e , "faid -1 should not exist" )
_ , e = s . Get ( 0 )
recordMustNotExist ( t , e , "faid 0 should not exist" )
noActions := func ( fid , faid int ) {
/*sfid, */ sfaid := /*strconv.Itoa(fid), */ strconv . Itoa ( faid )
count := s . Count ( )
exf ( count == 0 , "count should be %d not %d" , 0 , count )
count = s . CountInForum ( fid )
exf ( count == 0 , "count in %d should be %d not %d" , fid , 0 , count )
exf ( ! s . Exists ( faid ) , "faid %d should not exist" , faid )
2021-04-08 00:14:24 +00:00
_ , e := s . Get ( faid )
2021-04-07 14:23:11 +00:00
recordMustNotExist ( t , e , "faid " + sfaid + " should not exist" )
2021-04-08 00:14:24 +00:00
//exf(fa == nil, "fa should be nil not %+v", fa)
2021-04-07 14:23:11 +00:00
fas , e := s . GetInForum ( fid )
//recordMustNotExist(t, e, "fid "+sfid+" should not have any actions")
expectNilErr ( t , e ) // TODO: Why does this not return ErrNoRows?
exf ( len ( fas ) == 0 , "len(fas) should be %d not %d" , 0 , len ( fas ) )
fas , e = s . GetAll ( )
//recordMustNotExist(t, e, "there should not be any actions")
expectNilErr ( t , e ) // TODO: Why does this not return ErrNoRows?
exf ( len ( fas ) == 0 , "len(fas) should be %d not %d" , 0 , len ( fas ) )
fas , e = s . GetNewTopicActions ( fid )
//recordMustNotExist(t, e, "fid "+sfid+" should not have any new topic actions")
expectNilErr ( t , e ) // TODO: Why does this not return ErrNoRows?
exf ( len ( fas ) == 0 , "len(fas) should be %d not %d" , 0 , len ( fas ) )
}
noActions ( 1 , 1 )
fid , e := c . Forums . Create ( "Forum Action Test" , "Forum Action Test" , true , "" )
expectNilErr ( t , e )
noActions ( fid , 1 )
faid , e := c . ForumActionStore . Add ( & c . ForumAction {
Forum : fid ,
RunOnTopicCreation : false ,
RunDaysAfterTopicCreation : 1 ,
RunDaysAfterTopicLastReply : 0 ,
Action : c . ForumActionLock ,
Extra : "" ,
} )
expectNilErr ( t , e )
exf ( faid == 1 , "faid should be %d not %d" , 1 , faid )
count = s . Count ( )
exf ( count == 1 , "count should be %d not %d" , 1 , count )
count = s . CountInForum ( fid )
exf ( count == 1 , "count in %d should be %d not %d" , fid , 1 , count )
exf ( s . Exists ( faid ) , "faid %d should exist" , faid )
fa , e := s . Get ( faid )
expectNilErr ( t , e )
exf ( fa . ID == faid , "fa.ID should be %d not %d" , faid , fa . ID )
exf ( fa . Forum == fid , "fa.Forum should be %d not %d" , fid , fa . Forum )
exf ( fa . RunOnTopicCreation == false , "fa.RunOnTopicCreation should be false" )
exf ( fa . RunDaysAfterTopicCreation == 1 , "fa.RunDaysAfterTopicCreation should be %d not %d" , 1 , fa . RunDaysAfterTopicCreation )
exf ( fa . RunDaysAfterTopicLastReply == 0 , "fa.RunDaysAfterTopicLastReply should be %d not %d" , 0 , fa . RunDaysAfterTopicLastReply )
exf ( fa . Action == c . ForumActionLock , "fa.Action should be %d not %d" , c . ForumActionLock , fa . Action )
exf ( fa . Extra == "" , "fa.Extra should be '%s' not '%s'" , "" , fa . Extra )
tid , e := c . Topics . Create ( fid , "Forum Action Topic" , "Forum Action Topic" , 1 , "" )
expectNilErr ( t , e )
topic , e := c . Topics . Get ( tid )
expectNilErr ( t , e )
2021-04-08 00:14:24 +00:00
ex ( ! topic . IsClosed , "topic.IsClosed should be false" )
2021-04-07 14:23:11 +00:00
dayAgo := time . Now ( ) . AddDate ( 0 , 0 , - 5 )
expectNilErr ( t , topic . TestSetCreatedAt ( dayAgo ) )
expectNilErr ( t , fa . Run ( ) )
topic , e = c . Topics . Get ( tid )
expectNilErr ( t , e )
ex ( topic . IsClosed , "topic.IsClosed should be true" )
/ * _ , e = c . Rstore . Create ( topic , "Forum Action Reply" , "" , 1 )
expectNilErr ( t , e ) * /
2021-04-08 00:14:24 +00:00
tid , e = c . Topics . Create ( fid , "Forum Action Topic 2" , "Forum Action Topic 2" , 1 , "" )
expectNilErr ( t , e )
topic , e = c . Topics . Get ( tid )
expectNilErr ( t , e )
ex ( ! topic . IsClosed , "topic.IsClosed should be false" )
expectNilErr ( t , fa . Run ( ) )
topic , e = c . Topics . Get ( tid )
expectNilErr ( t , e )
ex ( ! topic . IsClosed , "topic.IsClosed should be false" )
2021-04-07 14:23:11 +00:00
_ = tid
expectNilErr ( t , s . Delete ( faid ) )
noActions ( fid , faid )
2021-05-02 08:47:19 +00:00
// TODO: Bulk lock tests
faid , e = c . ForumActionStore . Add ( & c . ForumAction {
Forum : fid ,
RunOnTopicCreation : false ,
RunDaysAfterTopicCreation : 2 ,
RunDaysAfterTopicLastReply : 0 ,
Action : c . ForumActionLock ,
Extra : "" ,
} )
expectNilErr ( t , e )
var l [ ] int
addTopic := func ( ) {
tid , e = c . Topics . Create ( fid , "Forum Action Topic 2" , "Forum Action Topic 2" , 1 , "" )
expectNilErr ( t , e )
topic , e := c . Topics . Get ( tid )
expectNilErr ( t , e )
ex ( ! topic . IsClosed , "topic.IsClosed should be false" )
dayAgo := time . Now ( ) . AddDate ( 0 , 0 , - 5 )
expectNilErr ( t , topic . TestSetCreatedAt ( dayAgo ) )
l = append ( l , tid )
}
lTest := func ( ) {
for _ , ll := range l {
to , e := c . Topics . Get ( ll )
expectNilErr ( t , e )
ex ( to . IsClosed , "to.IsClosed should be true" )
}
l = nil
}
addTopic ( )
addTopic ( )
addTopic ( )
addTopic ( )
addTopic ( )
addTopic ( )
addTopic ( )
addTopic ( )
addTopic ( )
addTopic ( )
expectNilErr ( t , fa . Run ( ) )
lTest ( )
// TODO: Create a method on the *ForumAction to get the count of topics which it could be run on and add a test to verify the count is as expected.
addTopic ( )
addTopic ( )
addTopic ( )
addTopic ( )
addTopic ( )
addTopic ( )
addTopic ( )
addTopic ( )
addTopic ( )
addTopic ( )
addTopic ( )
expectNilErr ( t , fa . Run ( ) )
lTest ( )
// TODO: Create a method on the *ForumAction to get the count of topics which it could be run on and add a test to verify the count is as expected.
2021-04-07 14:23:11 +00:00
}
2021-03-24 12:48:36 +00:00
func TestTopicList ( t * testing . T ) {
ex , exf := exp ( t ) , expf ( t )
fid , err := c . Forums . Create ( "Test Forum" , "Desc for test forum" , true , "" )
expectNilErr ( t , err )
tint := c . TopicList . ( c . TopicListIntTest )
testPagi := func ( p c . Paginator , pageList [ ] int , page , lastPage int ) {
exf ( len ( p . PageList ) == len ( pageList ) , "len(pagi.PageList) should be %d not %d" , len ( pageList ) , len ( p . PageList ) )
for i , page := range pageList {
exf ( p . PageList [ i ] == page , "pagi.PageList[%d] should be %d not %d" , i , page , p . PageList [ i ] )
}
exf ( p . Page == page , "pagi.Page should be %d not %d" , page , p . Page )
exf ( p . LastPage == lastPage , "pagi.LastPage should be %d not %d" , lastPage , p . LastPage )
}
test := func ( topicList [ ] * c . TopicsRow , pagi c . Paginator , listLen int , pagi2 c . Paginator , tid1 int ) {
exf ( len ( topicList ) == listLen , "len(topicList) should be %d not %d" , listLen , len ( topicList ) )
if len ( topicList ) > 0 {
topic := topicList [ 0 ]
exf ( topic . ID == tid1 , "topic.ID should be %d not %d" , tid1 , topic . ID )
}
testPagi ( pagi , pagi2 . PageList , pagi2 . Page , pagi2 . LastPage )
}
noTopics := func ( topicList [ ] * c . TopicsRow , pagi c . Paginator ) {
exf ( len ( topicList ) == 0 , "len(topicList) should be 0 not %d" , len ( topicList ) )
testPagi ( pagi , [ ] int { } , 1 , 1 )
}
noTopicsOnPage2 := func ( topicList [ ] * c . TopicsRow , pagi c . Paginator ) {
exf ( len ( topicList ) == 0 , "len(topicList) should be 0 not %d" , len ( topicList ) )
testPagi ( pagi , [ ] int { 1 } , 2 , 1 )
}
forum , err := c . Forums . Get ( fid )
expectNilErr ( t , err )
isAdmin , isMod , isBanned := false , false , false
gid , err := c . Groups . Create ( "Topic List Test" , "Test" , isAdmin , isMod , isBanned )
expectNilErr ( t , err )
ex ( c . Groups . Exists ( gid ) , "The group we just made doesn't exist" )
fp , err := c . FPStore . GetCopy ( fid , gid )
if err == sql . ErrNoRows {
fp = * c . BlankForumPerms ( )
} else if err != nil {
expectNilErr ( t , err )
}
fp . ViewTopic = true
forum , err = c . Forums . Get ( fid )
expectNilErr ( t , err )
expectNilErr ( t , forum . SetPerms ( & fp , "custom" , gid ) )
g , err := c . Groups . Get ( gid )
expectNilErr ( t , err )
noTopicsTests := func ( ) {
rr := func ( page , orderby int ) {
topicList , forumList , pagi , err := c . TopicList . GetListByGroup ( g , page , orderby , [ ] int { fid } )
expectNilErr ( t , err )
noTopics ( topicList , pagi )
exf ( len ( forumList ) == 1 , "len(forumList) should be 1 not %d" , len ( forumList ) )
}
rr ( 1 , 0 )
rr ( 2 , 0 )
rr ( 1 , 1 )
rr ( 2 , 1 )
topicList , pagi , err := c . TopicList . GetListByForum ( forum , 1 , 0 )
expectNilErr ( t , err )
noTopics ( topicList , pagi )
topicList , pagi , err = tint . RawGetListByForum ( forum , 1 , 0 )
expectNilErr ( t , err )
noTopics ( topicList , pagi )
topicList , forumList , pagi , err := c . TopicList . GetList ( 1 , 0 , [ ] int { fid } )
expectNilErr ( t , err )
noTopics ( topicList , pagi )
exf ( len ( forumList ) == 1 , "len(forumList) should be 1 not %d" , len ( forumList ) )
topicList , forumList , pagi , err = c . TopicList . GetListByCanSee ( [ ] int { fid } , 1 , 0 , [ ] int { fid } )
expectNilErr ( t , err )
noTopics ( topicList , pagi )
exf ( len ( forumList ) == 1 , "len(forumList) should be 1 not %d" , len ( forumList ) )
topicList , forumList , pagi , err = c . TopicList . GetListByCanSee ( [ ] int { } , 1 , 0 , [ ] int { fid } )
expectNilErr ( t , err )
noTopics ( topicList , pagi )
// TODO: Why is there a discrepency between this and GetList()?
exf ( len ( forumList ) == 0 , "len(forumList) should be 0 not %d" , len ( forumList ) )
}
noTopicsTests ( )
tid , err := c . Topics . Create ( fid , "New Topic" , "New Topic Body" , 1 , "" )
expectNilErr ( t , err )
topicList , forumList , pagi , err := c . TopicList . GetListByGroup ( g , 1 , 0 , [ ] int { fid } )
expectNilErr ( t , err )
test ( topicList , pagi , 1 , c . Paginator { [ ] int { 1 } , 1 , 1 } , tid )
exf ( len ( forumList ) == 1 , "len(forumList) should be 1 not %d" , len ( forumList ) )
topicList , forumList , pagi , err = c . TopicList . GetListByGroup ( g , 2 , 0 , [ ] int { fid } )
expectNilErr ( t , err )
noTopics ( topicList , pagi )
exf ( len ( forumList ) == 1 , "len(forumList) should be 1 not %d" , len ( forumList ) )
topicList , forumList , pagi , err = c . TopicList . GetListByGroup ( g , 1 , 1 , [ ] int { fid } )
expectNilErr ( t , err )
test ( topicList , pagi , 1 , c . Paginator { [ ] int { 1 } , 1 , 1 } , tid )
exf ( len ( forumList ) == 1 , "len(forumList) should be 1 not %d" , len ( forumList ) )
topicList , forumList , pagi , err = c . TopicList . GetListByGroup ( g , 2 , 1 , [ ] int { fid } )
expectNilErr ( t , err )
noTopicsOnPage2 ( topicList , pagi )
exf ( len ( forumList ) == 1 , "len(forumList) should be 1 not %d" , len ( forumList ) )
topicList , pagi , err = tint . RawGetListByForum ( forum , 1 , 0 )
expectNilErr ( t , err )
test ( topicList , pagi , 1 , c . Paginator { [ ] int { 1 } , 1 , 1 } , tid )
topicList , pagi , err = tint . RawGetListByForum ( forum , 0 , 0 )
expectNilErr ( t , err )
test ( topicList , pagi , 1 , c . Paginator { [ ] int { 1 } , 1 , 1 } , tid )
expectNilErr ( t , tint . Tick ( ) )
forum , err = c . Forums . Get ( fid )
expectNilErr ( t , err )
topicList , pagi , err = c . TopicList . GetListByForum ( forum , 1 , 0 )
expectNilErr ( t , err )
test ( topicList , pagi , 1 , c . Paginator { [ ] int { 1 } , 1 , 1 } , tid )
2020-06-17 22:03:36 +00:00
2021-03-24 12:48:36 +00:00
topicList , forumList , pagi , err = c . TopicList . GetList ( 1 , 0 , [ ] int { fid } )
expectNilErr ( t , err )
test ( topicList , pagi , 1 , c . Paginator { [ ] int { 1 } , 1 , 1 } , tid )
exf ( len ( forumList ) == 1 , "len(forumList) should be 1 not %d" , len ( forumList ) )
topicList , forumList , pagi , err = c . TopicList . GetListByCanSee ( [ ] int { fid } , 1 , 0 , [ ] int { fid } )
expectNilErr ( t , err )
test ( topicList , pagi , 1 , c . Paginator { [ ] int { 1 } , 1 , 1 } , tid )
exf ( len ( forumList ) == 1 , "len(forumList) should be 1 not %d" , len ( forumList ) )
topicList , forumList , pagi , err = c . TopicList . GetListByCanSee ( [ ] int { } , 1 , 0 , [ ] int { fid } )
expectNilErr ( t , err )
noTopics ( topicList , pagi )
exf ( len ( forumList ) == 0 , "len(forumList) should be 0 not %d" , len ( forumList ) )
topic , err := c . Topics . Get ( tid )
expectNilErr ( t , err )
expectNilErr ( t , topic . Delete ( ) )
forum , err = c . Forums . Get ( fid )
expectNilErr ( t , err )
noTopicsTests ( )
// TODO: More tests
_ = ex
}
func TestUtils ( t * testing . T ) {
2021-02-28 02:23:05 +00:00
ee := func ( email , eemail string ) {
cemail := c . CanonEmail ( email )
expectf ( t , cemail == eemail , "%s should be %s" , cemail , eemail )
}
2021-03-24 12:48:36 +00:00
ee ( "test@example.com" , "test@example.com" )
ee ( "test.test@example.com" , "test.test@example.com" )
2021-02-28 02:23:05 +00:00
ee ( "" , "" )
ee ( "ddd" , "ddd" )
ee ( "test.test@gmail.com" , "testtest@gmail.com" )
ee ( "TEST.test@gmail.com" , "testtest@gmail.com" )
ee ( "test.TEST.test@gmail.com" , "testtesttest@gmail.com" )
ee ( "test..TEST.test@gmail.com" , "testtesttest@gmail.com" )
2021-03-24 12:48:36 +00:00
ee ( "TEST.test@example.com" , "test.test@example.com" )
ee ( "test.TEST.test@example.com" , "test.test.test@example.com" )
// TODO: Exotic unicode email types? Are there those?
2020-06-17 22:03:36 +00:00
2020-06-18 02:39:21 +00:00
// TODO: More utils.go tests
}
func TestWeakPassword ( t * testing . T ) {
2021-03-24 12:48:36 +00:00
ex := exp ( t )
/ * weakPass := func ( password , name , email string ) func ( error , string , ... interface { } ) {
err := c . WeakPassword ( password , name , email )
2020-06-17 22:03:36 +00:00
return func ( expectErr error , m string , p ... interface { } ) {
2021-03-24 12:48:36 +00:00
m = fmt . Sprintf ( "pass=%s, user=%s, email=%s " , password , name , email ) + m
2020-06-17 22:03:36 +00:00
expect ( t , err == expectErr , fmt . Sprintf ( m , p ... ) )
}
} * /
nilErrStr := func ( e error ) error {
if e == nil {
e = errors . New ( "nil" )
}
return e
}
2021-03-24 12:48:36 +00:00
weakPass := func ( password , name , email string ) func ( error ) {
err := c . WeakPassword ( password , name , email )
2020-06-17 22:03:36 +00:00
e := nilErrStr ( err )
2021-03-24 12:48:36 +00:00
m := fmt . Sprintf ( "pass=%s, user=%s, email=%s " , password , name , email )
2020-06-17 22:03:36 +00:00
return func ( expectErr error ) {
ee := nilErrStr ( expectErr )
2021-03-24 12:48:36 +00:00
ex ( err == expectErr , m + fmt . Sprintf ( "err should be '%s' not '%s'" , ee , e ) )
2020-06-17 22:03:36 +00:00
}
}
//weakPass("test", "test", "test@example.com")(c.ErrWeakPasswordContains,"err should be ErrWeakPasswordContains not '%s'")
weakPass ( "" , "draw" , "test@example.com" ) ( c . ErrWeakPasswordNone )
weakPass ( "test" , "draw" , "test@example.com" ) ( c . ErrWeakPasswordShort )
weakPass ( "testtest" , "draw" , "test@example.com" ) ( c . ErrWeakPasswordContains )
weakPass ( "testdraw" , "draw" , "test@example.com" ) ( c . ErrWeakPasswordNameInPass )
weakPass ( "test@example.com" , "draw" , "test@example.com" ) ( c . ErrWeakPasswordEmailInPass )
weakPass ( "meet@example.com2" , "draw" , "" ) ( c . ErrWeakPasswordNoUpper )
weakPass ( "Meet@example.com2" , "draw" , "" ) ( nil )
weakPass ( "test2" , "draw" , "test@example.com" ) ( c . ErrWeakPasswordShort )
weakPass ( "test22222222" , "draw" , "test@example.com" ) ( c . ErrWeakPasswordContains )
weakPass ( "superman" , "draw" , "test@example.com" ) ( c . ErrWeakPasswordCommon )
2020-06-18 03:37:05 +00:00
weakPass ( "Superman" , "draw" , "test@example.com" ) ( c . ErrWeakPasswordCommon )
weakPass ( "Superma2" , "draw" , "test@example.com" ) ( nil )
weakPass ( "superman2" , "draw" , "test@example.com" ) ( c . ErrWeakPasswordCommon )
weakPass ( "Superman2" , "draw" , "test@example.com" ) ( c . ErrWeakPasswordCommon )
weakPass ( "superman22" , "draw" , "test@example.com" ) ( c . ErrWeakPasswordNoUpper )
2020-06-17 22:03:36 +00:00
weakPass ( "K\\@<^s}1" , "draw" , "test@example.com" ) ( nil )
weakPass ( "K\\@<^s}r" , "draw" , "test@example.com" ) ( c . ErrWeakPasswordNoNumbers )
weakPass ( "k\\@<^s}1" , "draw" , "test@example.com" ) ( c . ErrWeakPasswordNoUpper )
weakPass ( "aaaaaaaa" , "draw" , "test@example.com" ) ( c . ErrWeakPasswordNoUpper )
weakPass ( "aA1aA1aA1" , "draw" , "test@example.com" ) ( c . ErrWeakPasswordUniqueChars )
weakPass ( "abababab" , "draw" , "test@example.com" ) ( c . ErrWeakPasswordNoUpper )
weakPass ( "11111111111111111111" , "draw" , "test@example.com" ) ( c . ErrWeakPasswordNoUpper )
weakPass ( "aaaaaaaaaaAAAAAAAAAA" , "draw" , "test@example.com" ) ( c . ErrWeakPasswordUniqueChars )
weakPass ( "-:u/nMxb,A!n=B;H\\sjM" , "draw" , "test@example.com" ) ( nil )
2020-06-16 02:07:21 +00:00
}
2017-07-12 11:05:18 +00:00
func TestAuth ( t * testing . T ) {
2021-03-24 12:48:36 +00:00
ex := exp ( t )
2017-09-03 04:50:31 +00:00
// bcrypt likes doing stupid things, so this test will probably fail
2018-05-28 06:27:12 +00:00
realPassword := "Madame Cassandra's Mystic Orb"
2017-11-13 05:22:37 +00:00
t . Logf ( "Set realPassword to '%s'" , realPassword )
2018-05-28 06:27:12 +00:00
t . Log ( "Hashing the real password with bcrypt" )
2019-04-19 08:20:10 +00:00
hashedPassword , _ , err := c . BcryptGeneratePassword ( realPassword )
2018-05-28 06:27:12 +00:00
if err != nil {
t . Error ( err )
}
passwordTest ( t , realPassword , hashedPassword )
// TODO: Peek at the prefix to verify this is a bcrypt hash
2017-09-03 04:50:31 +00:00
t . Log ( "Hashing the real password" )
2019-04-19 08:20:10 +00:00
hashedPassword2 , _ , err := c . GeneratePassword ( realPassword )
2017-09-03 04:50:31 +00:00
if err != nil {
t . Error ( err )
}
2018-05-28 06:27:12 +00:00
passwordTest ( t , realPassword , hashedPassword2 )
// TODO: Peek at the prefix to verify this is a bcrypt hash
2019-04-19 08:20:10 +00:00
_ , err , _ = c . Auth . Authenticate ( "None" , "password" )
2021-03-24 12:48:36 +00:00
errmsg := "Name None shouldn't exist"
2018-05-28 06:27:12 +00:00
if err != nil {
errmsg += "\n" + err . Error ( )
}
2021-03-24 12:48:36 +00:00
ex ( err == c . ErrNoUserByName , errmsg )
2018-05-28 06:27:12 +00:00
2019-04-19 08:20:10 +00:00
uid , err , _ := c . Auth . Authenticate ( "Admin" , "password" )
2018-05-28 06:27:12 +00:00
expectNilErr ( t , err )
2021-02-28 02:23:05 +00:00
expectf ( t , uid == 1 , "Default admin uid should be 1 not %d" , uid )
2018-05-28 06:27:12 +00:00
2019-04-19 08:20:10 +00:00
_ , err , _ = c . Auth . Authenticate ( "Sam" , "ReallyBadPassword" )
2021-03-24 12:48:36 +00:00
errmsg = "Name Sam shouldn't exist"
2018-05-28 06:27:12 +00:00
if err != nil {
errmsg += "\n" + err . Error ( )
}
2021-03-24 12:48:36 +00:00
ex ( err == c . ErrNoUserByName , errmsg )
2018-05-28 06:27:12 +00:00
2019-04-19 08:20:10 +00:00
admin , err := c . Users . Get ( 1 )
2018-05-28 06:27:12 +00:00
expectNilErr ( t , err )
// TODO: Move this into the user store tests to provide better coverage? E.g. To see if the installer and the user creator initialise the field differently
2021-03-24 12:48:36 +00:00
ex ( admin . Session == "" , "Admin session should be blank" )
2017-09-03 04:50:31 +00:00
2019-04-19 08:20:10 +00:00
session , err := c . Auth . CreateSession ( 1 )
2018-05-28 06:27:12 +00:00
expectNilErr ( t , err )
2021-03-24 12:48:36 +00:00
ex ( session != "" , "Admin session shouldn't be blank" )
2018-05-28 06:27:12 +00:00
// TODO: Test the actual length set in the setting in addition to this "too short" test
// TODO: We might be able to push up this minimum requirement
2021-03-24 12:48:36 +00:00
ex ( len ( session ) > 10 , "Admin session shouldn't be too short" )
ex ( admin . Session != session , "Old session should not match new one" )
2019-04-19 08:20:10 +00:00
admin , err = c . Users . Get ( 1 )
2018-05-28 06:27:12 +00:00
expectNilErr ( t , err )
2021-03-24 12:48:36 +00:00
ex ( admin . Session == session , "Sessions should match" )
2018-05-28 06:27:12 +00:00
2018-10-04 02:58:01 +00:00
// TODO: Create a user with a unicode password and see if we can login as them
2018-05-28 06:27:12 +00:00
// TODO: Tests for SessionCheck, GetCookies, and ForceLogout
2020-04-02 22:57:46 +00:00
// TODO: Tests for MFA Verification
2018-05-28 06:27:12 +00:00
}
// TODO: Vary the salts? Keep in mind that some algorithms store the salt in the hash therefore the salt string may be blank
2020-02-09 10:00:08 +00:00
func passwordTest ( t * testing . T , realPassword , hashedPassword string ) {
2018-05-28 06:27:12 +00:00
if len ( hashedPassword ) < 10 {
t . Error ( "Hash too short" )
}
salt := ""
password := realPassword
2017-11-13 05:22:37 +00:00
t . Logf ( "Testing password '%s'" , password )
t . Logf ( "Testing salt '%s'" , salt )
2019-04-19 08:20:10 +00:00
err := c . CheckPassword ( hashedPassword , password , salt )
if err == c . ErrMismatchedHashAndPassword {
2017-09-03 04:50:31 +00:00
t . Error ( "The two don't match" )
2019-04-19 08:20:10 +00:00
} else if err == c . ErrPasswordTooLong {
2017-09-03 04:50:31 +00:00
t . Error ( "CheckPassword thinks the password is too long" )
} else if err != nil {
t . Error ( err )
}
password = "hahaha"
2017-11-13 05:22:37 +00:00
t . Logf ( "Testing password '%s'" , password )
t . Logf ( "Testing salt '%s'" , salt )
2019-04-19 08:20:10 +00:00
err = c . CheckPassword ( hashedPassword , password , salt )
if err == c . ErrPasswordTooLong {
2017-09-03 04:50:31 +00:00
t . Error ( "CheckPassword thinks the password is too long" )
} else if err == nil {
t . Error ( "The two shouldn't match!" )
}
password = "Madame Cassandra's Mystic"
2017-11-13 05:22:37 +00:00
t . Logf ( "Testing password '%s'" , password )
t . Logf ( "Testing salt '%s'" , salt )
2019-04-19 08:20:10 +00:00
err = c . CheckPassword ( hashedPassword , password , salt )
expect ( t , err != c . ErrPasswordTooLong , "CheckPassword thinks the password is too long" )
2017-11-13 05:22:37 +00:00
expect ( t , err != nil , "The two shouldn't match!" )
2017-07-12 11:05:18 +00:00
}
2018-06-26 02:30:29 +00:00
2020-07-15 06:59:47 +00:00
func TestUserPrivacy ( t * testing . T ) {
pu , u := c . BlankUser ( ) , & c . GuestUser
pu . ID = 1
2021-05-02 08:47:19 +00:00
ex , exf := exp ( t ) , expf ( t )
ex ( ! pu . Privacy . NoPresence , "pu.Privacy.NoPresence should be false" )
ex ( ! u . Privacy . NoPresence , "u.Privacy.NoPresence should be false" )
2020-07-15 06:59:47 +00:00
var msg string
test := func ( expects bool , level int ) {
pu . Privacy . ShowComments = level
val := c . PrivacyCommentsShow ( pu , u )
var bit string
if ! expects {
bit = " not"
val = ! val
}
2021-05-02 08:47:19 +00:00
exf ( val , "%s should%s be able to see comments on level %d" , msg , bit , level )
2020-07-15 06:59:47 +00:00
}
// 0 = default, 1 = public, 2 = registered, 3 = friends, 4 = self, 5 = disabled
msg = "guest users"
test ( true , 0 )
test ( true , 1 )
test ( false , 2 )
test ( false , 3 )
test ( false , 4 )
test ( false , 5 )
u = c . BlankUser ( )
msg = "blank users"
test ( true , 0 )
test ( true , 1 )
test ( false , 2 )
//test(false,3)
test ( false , 4 )
test ( false , 5 )
u . Loggedin = true
msg = "registered users"
test ( true , 0 )
test ( true , 1 )
test ( true , 2 )
test ( false , 3 )
test ( false , 4 )
test ( false , 5 )
u . IsBanned = true
msg = "banned users"
test ( true , 0 )
test ( true , 1 )
test ( true , 2 )
test ( false , 3 )
test ( false , 4 )
test ( false , 5 )
u . IsBanned = false
u . IsMod = true
msg = "mods"
test ( true , 0 )
test ( true , 1 )
test ( true , 2 )
test ( false , 3 )
test ( false , 4 )
test ( false , 5 )
u . IsMod = false
u . IsSuperMod = true
msg = "super mods"
test ( true , 0 )
test ( true , 1 )
test ( true , 2 )
test ( false , 3 )
test ( false , 4 )
test ( false , 5 )
u . IsSuperMod = false
u . IsAdmin = true
msg = "admins"
test ( true , 0 )
test ( true , 1 )
test ( true , 2 )
test ( false , 3 )
test ( false , 4 )
test ( false , 5 )
u . IsAdmin = false
u . IsSuperAdmin = true
msg = "super admins"
test ( true , 0 )
test ( true , 1 )
test ( true , 2 )
test ( false , 3 )
test ( false , 4 )
test ( false , 5 )
u . IsSuperAdmin = false
u . ID = 1
test ( true , 0 )
test ( true , 1 )
test ( true , 2 )
test ( true , 3 )
test ( true , 4 )
test ( false , 5 )
}
2018-06-26 04:54:20 +00:00
type METri struct {
Name string // Optional, this is here for tests involving invisible characters so we know what's going in
Msg string
Expects string
}
2018-10-08 04:38:51 +00:00
type METriList struct {
Items [ ] METri
}
2019-10-06 00:34:09 +00:00
func ( l * METriList ) Add ( args ... string ) {
2018-06-26 04:54:20 +00:00
if len ( args ) < 2 {
panic ( "need 2 or more args" )
}
if len ( args ) > 2 {
2019-10-06 00:34:09 +00:00
l . Items = append ( l . Items , METri { args [ 0 ] , args [ 1 ] , args [ 2 ] } )
2018-10-08 04:38:51 +00:00
} else {
2019-10-06 00:34:09 +00:00
l . Items = append ( l . Items , METri { "" , args [ 0 ] , args [ 1 ] } )
2018-06-26 04:54:20 +00:00
}
}
2018-10-04 09:01:07 +00:00
type CountTest struct {
Name string
Msg string
Expects int
}
type CountTestList struct {
Items [ ] CountTest
}
2020-02-09 10:00:08 +00:00
func ( l * CountTestList ) Add ( name , msg string , expects int ) {
2019-10-06 00:34:09 +00:00
l . Items = append ( l . Items , CountTest { name , msg , expects } )
2018-10-04 09:01:07 +00:00
}
func TestWordCount ( t * testing . T ) {
2019-10-06 00:34:09 +00:00
l := & CountTestList { nil }
l . Add ( "blank" , "" , 0 )
l . Add ( "single-letter" , "h" , 1 )
l . Add ( "single-kana" , "お" , 1 )
l . Add ( "single-letter-words" , "h h" , 2 )
l . Add ( "two-letter" , "h" , 1 )
l . Add ( "two-kana" , "おは" , 1 )
l . Add ( "two-letter-words" , "hh hh" , 2 )
l . Add ( "" , "h,h" , 2 )
l . Add ( "" , "h,,h" , 2 )
l . Add ( "" , "h, h" , 2 )
l . Add ( "" , " h, h" , 2 )
l . Add ( "" , "h, h " , 2 )
l . Add ( "" , " h, h " , 2 )
l . Add ( "" , "h, h" , 2 )
l . Add ( "" , "h\nh" , 2 )
l . Add ( "" , "h\"h" , 2 )
l . Add ( "" , "h[r]h" , 3 )
l . Add ( "" , "お,お" , 2 )
l . Add ( "" , "お、お" , 2 )
l . Add ( "" , "お\nお" , 2 )
l . Add ( "" , "お”お" , 2 )
l . Add ( "" , "お「あ」お" , 3 )
for _ , item := range l . Items {
2019-04-19 08:20:10 +00:00
res := c . WordCount ( item . Msg )
2018-10-04 09:01:07 +00:00
if res != item . Expects {
if item . Name != "" {
t . Error ( "Name: " , item . Name )
}
t . Error ( "Testing string '" + item . Msg + "'" )
t . Error ( "Bad output:" , res )
t . Error ( "Expected:" , item . Expects )
}
}
}
2021-05-02 08:47:19 +00:00
func TestTick ( t * testing . T ) {
expectNilErr ( t , c . StartupTasks ( ) )
expectNilErr ( t , c . Dailies ( ) )
2021-05-05 09:23:58 +00:00
expectNilErr ( t , c . Tasks . HalfSec . Run ( ) )
expectNilErr ( t , c . Tasks . Sec . Run ( ) )
expectNilErr ( t , c . Tasks . FifteenMin . Run ( ) )
expectNilErr ( t , c . Tasks . Hour . Run ( ) )
expectNilErr ( t , c . Tasks . Day . Run ( ) )
thumbChan := make ( chan bool )
expectNilErr ( t , tickLoop ( thumbChan ) )
expectNilErr ( t , c . CTickLoop . HalfSecf ( ) )
expectNilErr ( t , c . CTickLoop . Secf ( ) )
expectNilErr ( t , c . CTickLoop . FifteenMinf ( ) )
expectNilErr ( t , c . CTickLoop . Hourf ( ) )
expectNilErr ( t , c . CTickLoop . Dayf ( ) )
2021-05-02 08:47:19 +00:00
}
2021-05-08 06:34:46 +00:00
func TestWSHub ( t * testing . T ) {
ex , exf , h := exp ( t ) , expf ( t ) , & c . WsHub
exf ( h . GuestCount ( ) == 0 , "GuestCount should be %d not %d" , 0 , h . GuestCount ( ) )
exf ( h . UserCount ( ) == 0 , "UserCount should be %d not %d" , 0 , h . UserCount ( ) )
ex ( ! h . HasUser ( - 1 ) , "HasUser(-1) should be false" )
ex ( ! h . HasUser ( 0 ) , "HasUser(0) should be false" )
ex ( ! h . HasUser ( 1 ) , "HasUser(1) should be false" )
uid , e := c . Users . Create ( "WsHub Test" , "WsHub Test" , "" , 1 , true )
expectNilErr ( t , e )
exf ( ! h . HasUser ( uid ) , "HasUser(%d) should be false" , uid )
exf ( len ( h . AllUsers ( ) ) == 0 , "len(AllUsers()) should be %d not %d" , 0 , len ( h . AllUsers ( ) ) )
f := func ( uid , guestCount , userCount , allUserListLen int , hasUser bool ) {
exf ( h . GuestCount ( ) == guestCount , "GuestCount should be %d not %d" , guestCount , h . GuestCount ( ) )
exf ( h . UserCount ( ) == userCount , "UserCount should be %d not %d" , userCount , h . UserCount ( ) )
exf ( len ( h . AllUsers ( ) ) == allUserListLen , "len(AllUsers()) should be %d not %d" , allUserListLen , len ( h . AllUsers ( ) ) )
if hasUser {
exf ( h . HasUser ( uid ) , "HasUser(%d) should be true" , uid )
} else {
exf ( ! h . HasUser ( uid ) , "HasUser(%d) should be false" , uid )
}
}
u , e := c . Users . Get ( uid )
expectNilErr ( t , e )
wsUser , e := h . AddConn ( u , nil )
expectNilErr ( t , e )
f ( uid , 0 , 1 , 1 , true )
uid , e = c . Users . Create ( "WsHub Test 2" , "WsHub Test 2" , "" , 1 , true )
expectNilErr ( t , e )
u2 , e := c . Users . Get ( uid )
expectNilErr ( t , e )
wsUser2 , e := h . AddConn ( u2 , nil )
expectNilErr ( t , e )
f ( uid , 0 , 2 , 2 , true )
h . RemoveConn ( wsUser2 , nil )
f ( uid , 0 , 1 , 1 , false )
h . RemoveConn ( wsUser2 , nil )
f ( uid , 0 , 1 , 1 , false )
h . RemoveConn ( wsUser , nil )
f ( uid , 0 , 0 , 0 , false )
countSockets := func ( wsUser * c . WSUser , expect int ) {
exf ( wsUser . CountSockets ( ) == expect , "CountSockets() should be %d not %d" , expect , wsUser . CountSockets ( ) )
}
wsUser2 , e = h . AddConn ( u2 , nil )
expectNilErr ( t , e )
f ( uid , 0 , 1 , 1 , true )
countSockets ( wsUser2 , 1 )
wsUser2 . RemoveSocket ( nil )
f ( uid , 0 , 1 , 1 , true )
countSockets ( wsUser2 , 0 )
h . RemoveConn ( wsUser2 , nil )
f ( uid , 0 , 0 , 0 , false )
countSockets ( wsUser2 , 0 )
wsUser2 , e = h . AddConn ( u2 , nil )
expectNilErr ( t , e )
f ( uid , 0 , 1 , 1 , true )
countSockets ( wsUser2 , 1 )
expectNilErr ( t , wsUser2 . Ping ( ) )
f ( uid , 0 , 1 , 1 , true )
countSockets ( wsUser2 , 0 )
h . RemoveConn ( wsUser2 , nil )
f ( uid , 0 , 0 , 0 , false )
countSockets ( wsUser2 , 0 )
// TODO: Add more tests
}