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.

123 lines
2.4 KiB

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