Core functionality for new aiterp.net servers
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
2.7 KiB

7 years ago
  1. package auth
  2. import (
  3. "errors"
  4. "strings"
  5. "testing"
  6. "git.aiterp.net/gisle/wrouter/generate"
  7. )
  8. var ErrExists = errors.New("auth: user exists")
  9. var ErrLogin = errors.New("auth: login failed")
  10. type testAuther struct {
  11. FullName string
  12. users []*User
  13. passwords map[string]string
  14. }
  15. func (ta *testAuther) ID() string {
  16. return strings.ToLower(ta.FullName)
  17. }
  18. func (ta *testAuther) Name() string {
  19. return ta.FullName
  20. }
  21. func (ta *testAuther) Exists(username string) bool {
  22. for _, user := range ta.users {
  23. if user.Name == username {
  24. return true
  25. }
  26. }
  27. return false
  28. }
  29. func (ta *testAuther) Find(userid string) *User {
  30. for _, user := range ta.users {
  31. if user.ID == userid {
  32. return user
  33. }
  34. }
  35. return nil
  36. }
  37. func (ta *testAuther) Login(username, password string) (*User, error) {
  38. for _, user := range ta.users {
  39. if user.Name == username && password == ta.passwords[user.ID] {
  40. return user, nil
  41. }
  42. }
  43. return nil, ErrLogin
  44. }
  45. func (ta *testAuther) Register(username, password string, data map[string]string) (*User, error) {
  46. if ta.Exists(username) {
  47. return nil, ErrExists
  48. }
  49. if ta.passwords == nil {
  50. ta.passwords = make(map[string]string)
  51. }
  52. id := generate.ID()
  53. ta.passwords[id] = password
  54. user := NewUser(ta, id, username, "member", data)
  55. ta.users = append(ta.users, user)
  56. return user, nil
  57. }
  58. func TestList(t *testing.T) {
  59. ta1 := testAuther{FullName: "Auth1"}
  60. ta2 := testAuther{FullName: "Auth2"}
  61. Register(&ta1)
  62. Register(&ta2)
  63. if ta1.ID() != "auth1" {
  64. t.Errorf("ta1.ID() = %s", ta1.ID())
  65. t.Fail()
  66. }
  67. if ta2.ID() != "auth2" {
  68. t.Errorf("ta2.ID() = %s", ta2.ID())
  69. t.Fail()
  70. }
  71. t.Run("Find", func(t *testing.T) {
  72. fa1 := FindAuthenticator("auth1")
  73. fa2 := FindAuthenticator("auth2")
  74. if &ta1 != fa1 {
  75. t.Errorf("%s != %s", ta1.ID(), fa1.ID())
  76. t.Fail()
  77. }
  78. if &ta2 != fa2 {
  79. t.Errorf("%s != %s", ta2.ID(), fa2.ID())
  80. t.Fail()
  81. }
  82. })
  83. t.Run("Register", func(t *testing.T) {
  84. user, err := ta1.Register("Test", "CakesAndStuff", nil)
  85. if err != nil || user.Name != "Test" {
  86. t.Logf("err = %v; name = \"%s\"", err, user.Name)
  87. t.Fail()
  88. }
  89. if !ta1.Exists("Test") {
  90. t.Log("Registered user does not exist")
  91. t.Fail()
  92. }
  93. user2, err := ta1.Register("Test", "CakesAndStuff", nil)
  94. if err == nil || user2 != nil {
  95. t.Logf("err = %s; name = \"%s\"", err, user2.Name)
  96. t.Fail()
  97. }
  98. })
  99. t.Run("Login", func(t *testing.T) {
  100. user, err := ta1.Login("Test", "CakesAndStuff")
  101. if err != nil || user.Name != "Test" {
  102. t.Logf("err = %v; name = \"%s\"", err, user.Name)
  103. t.Fail()
  104. }
  105. user2, err := ta1.Login("Test", "WrongPassword")
  106. if err == nil || user2 != nil {
  107. t.Logf("err = %v; name = \"%s\"", err, user.Name)
  108. t.Fail()
  109. }
  110. })
  111. }