geoipchecker_test.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package thoth_test
  2. import (
  3. "fmt"
  4. "net/http/httptest"
  5. "testing"
  6. "github.com/TecharoHQ/anubis/lib/policy/checker"
  7. "github.com/TecharoHQ/anubis/lib/thoth"
  8. )
  9. var _ checker.Impl = &thoth.GeoIPChecker{}
  10. func TestGeoIPChecker(t *testing.T) {
  11. cli := loadSecrets(t)
  12. asnc := cli.GeoIPCheckerFor([]string{"us"})
  13. for _, cs := range []struct {
  14. ipAddress string
  15. wantMatch bool
  16. wantError bool
  17. }{
  18. {
  19. ipAddress: "1.1.1.1",
  20. wantMatch: true,
  21. wantError: false,
  22. },
  23. {
  24. ipAddress: "2.2.2.2",
  25. wantMatch: false,
  26. wantError: false,
  27. },
  28. {
  29. ipAddress: "taco",
  30. wantMatch: false,
  31. wantError: false,
  32. },
  33. {
  34. ipAddress: "127.0.0.1",
  35. wantMatch: false,
  36. wantError: false,
  37. },
  38. } {
  39. t.Run(fmt.Sprintf("%v", cs), func(t *testing.T) {
  40. req := httptest.NewRequest("GET", "/", nil)
  41. req.Header.Set("X-Real-Ip", cs.ipAddress)
  42. match, err := asnc.Check(req)
  43. if match != cs.wantMatch {
  44. t.Errorf("Wanted match: %v, got: %v", cs.wantMatch, match)
  45. }
  46. switch {
  47. case err != nil && !cs.wantError:
  48. t.Errorf("Did not want error but got: %v", err)
  49. case err == nil && cs.wantError:
  50. t.Error("Wanted error but got none")
  51. }
  52. })
  53. }
  54. }