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.

69 lines
1.7 KiB

7 years ago
7 years ago
7 years ago
  1. package auth
  2. import "testing"
  3. func TestSession(t *testing.T) {
  4. auther := testAuther{FullName: "Test64"}
  5. Register(&auther)
  6. user, err := auther.Register("Tester1401", "1234", nil)
  7. if err != nil {
  8. t.Error("Failed to register test user:", err)
  9. t.Fail()
  10. return
  11. }
  12. sessions := []*Session{OpenSession(user), OpenSession(user), OpenSession(user)}
  13. ids := []string{sessions[0].ID, sessions[1].ID, sessions[2].ID}
  14. t.Run("Find", func(t *testing.T) {
  15. for i, id := range ids {
  16. found := FindSession(id)
  17. if found != sessions[i] {
  18. t.Errorf("Find(\"%s\") == %+v", id, found)
  19. t.Fail()
  20. }
  21. }
  22. })
  23. t.Run("Auth", func(t *testing.T) {
  24. for _, id := range ids {
  25. found := FindSession(id)
  26. foundUser, err := FindUser(found.UserID)
  27. if foundUser == nil {
  28. t.Error("User", found.UserID, "not found", err)
  29. t.Fail()
  30. }
  31. if foundUser != nil && foundUser != user {
  32. t.Error("User", found.UserID, "is not correct", foundUser)
  33. t.Fail()
  34. }
  35. }
  36. })
  37. t.Run("Close", func(t *testing.T) {
  38. CloseSession(ids[2])
  39. if FindSession(ids[0]) == nil || FindSession(ids[1]) == nil || FindSession(ids[2]) != nil {
  40. t.Errorf("Find(\"%s\") == %+v", ids[0], FindSession(ids[0]))
  41. t.Errorf("Find(\"%s\") == %+v", ids[1], FindSession(ids[1]))
  42. t.Errorf("Find(\"%s\") == %+v", ids[2], FindSession(ids[2]))
  43. t.Fail()
  44. }
  45. })
  46. t.Run("Clear", func(t *testing.T) {
  47. ClearSessions(user)
  48. if FindSession(ids[0]) != nil || FindSession(ids[1]) != nil || FindSession(ids[2]) != nil {
  49. t.Errorf("Find(\"%s\") == %+v", ids[0], FindSession(ids[0]))
  50. t.Errorf("Find(\"%s\") == %+v", ids[1], FindSession(ids[1]))
  51. t.Errorf("Find(\"%s\") == %+v", ids[2], FindSession(ids[2]))
  52. t.Fail()
  53. }
  54. })
  55. }