config_test.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package lib
  2. import (
  3. "errors"
  4. "os"
  5. "path/filepath"
  6. "testing"
  7. "github.com/TecharoHQ/anubis"
  8. "github.com/TecharoHQ/anubis/internal/thoth/thothmock"
  9. "github.com/TecharoHQ/anubis/lib/policy"
  10. )
  11. func TestInvalidChallengeMethod(t *testing.T) {
  12. if _, err := LoadPoliciesOrDefault(t.Context(), "testdata/invalid-challenge-method.yaml", 4); !errors.Is(err, policy.ErrChallengeRuleHasWrongAlgorithm) {
  13. t.Fatalf("wanted error %v but got %v", policy.ErrChallengeRuleHasWrongAlgorithm, err)
  14. }
  15. }
  16. func TestBadConfigs(t *testing.T) {
  17. finfos, err := os.ReadDir("policy/config/testdata/bad")
  18. if err != nil {
  19. t.Fatal(err)
  20. }
  21. for _, st := range finfos {
  22. st := st
  23. t.Run(st.Name(), func(t *testing.T) {
  24. if _, err := LoadPoliciesOrDefault(t.Context(), filepath.Join("policy", "config", "testdata", "good", st.Name()), anubis.DefaultDifficulty); err == nil {
  25. t.Fatal(err)
  26. } else {
  27. t.Log(err)
  28. }
  29. })
  30. }
  31. }
  32. func TestGoodConfigs(t *testing.T) {
  33. finfos, err := os.ReadDir("policy/config/testdata/good")
  34. if err != nil {
  35. t.Fatal(err)
  36. }
  37. for _, st := range finfos {
  38. st := st
  39. t.Run(st.Name(), func(t *testing.T) {
  40. t.Run("with-thoth", func(t *testing.T) {
  41. ctx := thothmock.WithMockThoth(t)
  42. if _, err := LoadPoliciesOrDefault(ctx, filepath.Join("policy", "config", "testdata", "good", st.Name()), anubis.DefaultDifficulty); err != nil {
  43. t.Fatal(err)
  44. }
  45. })
  46. t.Run("without-thoth", func(t *testing.T) {
  47. if _, err := LoadPoliciesOrDefault(t.Context(), filepath.Join("policy", "config", "testdata", "good", st.Name()), anubis.DefaultDifficulty); err != nil {
  48. t.Fatal(err)
  49. }
  50. })
  51. })
  52. }
  53. }