policy_test.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package policy
  2. import (
  3. "os"
  4. "path/filepath"
  5. "testing"
  6. "github.com/TecharoHQ/anubis"
  7. "github.com/TecharoHQ/anubis/data"
  8. "github.com/TecharoHQ/anubis/lib/thoth/thothmock"
  9. )
  10. func TestDefaultPolicyMustParse(t *testing.T) {
  11. ctx := thothmock.WithMockThoth(t)
  12. fin, err := data.BotPolicies.Open("botPolicies.yaml")
  13. if err != nil {
  14. t.Fatal(err)
  15. }
  16. defer fin.Close()
  17. if _, err := ParseConfig(ctx, fin, "botPolicies.yaml", anubis.DefaultDifficulty, "info"); err != nil {
  18. t.Fatalf("can't parse config: %v", err)
  19. }
  20. }
  21. func TestGoodConfigs(t *testing.T) {
  22. finfos, err := os.ReadDir("../config/testdata/good")
  23. if err != nil {
  24. t.Fatal(err)
  25. }
  26. for _, st := range finfos {
  27. st := st
  28. t.Run(st.Name(), func(t *testing.T) {
  29. t.Run("with-thoth", func(t *testing.T) {
  30. fin, err := os.Open(filepath.Join("..", "config", "testdata", "good", st.Name()))
  31. if err != nil {
  32. t.Fatal(err)
  33. }
  34. defer fin.Close()
  35. ctx := thothmock.WithMockThoth(t)
  36. if _, err := ParseConfig(ctx, fin, fin.Name(), anubis.DefaultDifficulty, "info"); err != nil {
  37. t.Fatal(err)
  38. }
  39. })
  40. t.Run("without-thoth", func(t *testing.T) {
  41. fin, err := os.Open(filepath.Join("..", "config", "testdata", "good", st.Name()))
  42. if err != nil {
  43. t.Fatal(err)
  44. }
  45. defer fin.Close()
  46. if _, err := ParseConfig(t.Context(), fin, fin.Name(), anubis.DefaultDifficulty, "info"); err != nil {
  47. t.Fatal(err)
  48. }
  49. })
  50. })
  51. }
  52. }
  53. func TestBadConfigs(t *testing.T) {
  54. ctx := thothmock.WithMockThoth(t)
  55. finfos, err := os.ReadDir("../config/testdata/bad")
  56. if err != nil {
  57. t.Fatal(err)
  58. }
  59. for _, st := range finfos {
  60. st := st
  61. t.Run(st.Name(), func(t *testing.T) {
  62. fin, err := os.Open(filepath.Join("..", "config", "testdata", "bad", st.Name()))
  63. if err != nil {
  64. t.Fatal(err)
  65. }
  66. defer fin.Close()
  67. if _, err := ParseConfig(ctx, fin, fin.Name(), anubis.DefaultDifficulty, "info"); err == nil {
  68. t.Fatal(err)
  69. } else {
  70. t.Log(err)
  71. }
  72. })
  73. }
  74. }