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.

146 lines
3.0 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
  1. package auth
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "net/http/cookiejar"
  6. "net/http/httptest"
  7. "net/url"
  8. "strings"
  9. "testing"
  10. "time"
  11. )
  12. type handlerStruct struct{}
  13. func (hs *handlerStruct) ServeHTTP(w http.ResponseWriter, req *http.Request) {
  14. req.ParseForm() // Router does this in non-tests
  15. if strings.HasPrefix(req.URL.Path, "/auth") {
  16. Handler.Handle("/auth", w, req, nil)
  17. return
  18. }
  19. }
  20. func TestHandler(t *testing.T) {
  21. cookieJar, err := cookiejar.New(nil)
  22. if err != nil {
  23. t.Error("Cookie Jar:", err)
  24. t.Fail()
  25. return
  26. }
  27. server := httptest.NewServer(&handlerStruct{})
  28. url2, _ := url.Parse(server.URL)
  29. client := &http.Client{Jar: cookieJar}
  30. auther := testAuther{FullName: "Test"}
  31. Register(&auther)
  32. form := url.Values{}
  33. form.Set("method", "test")
  34. form.Set("username", "Test")
  35. form.Set("password", "stuff'nthings")
  36. form2 := url.Values{}
  37. form2.Set("method", "test")
  38. form3 := url.Values{}
  39. form3.Set("method", "test")
  40. form3.Set("username", "Test2")
  41. form3.Set("password", "stuff'nthings")
  42. t.Run("Register", func(t *testing.T) {
  43. resp, err := client.PostForm(server.URL+"/auth/register", form)
  44. if err != nil {
  45. t.Error("Request:", err)
  46. t.Fail()
  47. }
  48. if resp.StatusCode != 200 {
  49. t.Error("Expected 200, got", resp.Status)
  50. t.Fail()
  51. }
  52. respSession := Session{}
  53. json.NewDecoder(resp.Body).Decode(&respSession)
  54. if respSession.UserID == "" {
  55. t.Errorf("No user ID in session")
  56. t.Fail()
  57. }
  58. if time.Since(respSession.Time) > time.Second {
  59. t.Error("Session time is too low", time.Since(respSession.Time))
  60. t.Fail()
  61. }
  62. })
  63. t.Run("Login", func(t *testing.T) {
  64. resp, err := client.PostForm(server.URL+"/auth/login", form)
  65. if err != nil {
  66. t.Error("Request:", err)
  67. t.Fail()
  68. }
  69. if resp.StatusCode != 200 {
  70. t.Error("Expected 200, got", resp.Status)
  71. t.Fail()
  72. }
  73. if len(resp.Cookies()) == 0 || len(client.Jar.Cookies(url2)) == 0 {
  74. t.Error("No cookies set")
  75. t.Fail()
  76. }
  77. respSession := Session{}
  78. json.NewDecoder(resp.Body).Decode(&respSession)
  79. if respSession.UserID == "" {
  80. t.Errorf("No user ID in session")
  81. t.Fail()
  82. }
  83. })
  84. // TODO: Move to router test
  85. /* t.Run("Status", func(t *testing.T) {
  86. resp, err := client.Get(server.URL + "/auth/status?method=test")
  87. if err != nil {
  88. t.Error("Request:", err)
  89. t.Fail()
  90. }
  91. if resp.StatusCode != 200 {
  92. t.Error("Expected 200, got", resp.Status)
  93. t.Fail()
  94. }
  95. respSession := Session{}
  96. json.NewDecoder(resp.Body).Decode(&respSession)
  97. if respSession.UserID == "" {
  98. t.Errorf("No user ID in session")
  99. t.Fail()
  100. }
  101. }) */
  102. t.Run("Login_Fail", func(t *testing.T) {
  103. resp, err := client.PostForm(server.URL+"/auth/login", form3)
  104. if err != nil {
  105. t.Error("Request:", err)
  106. t.Fail()
  107. }
  108. if resp.StatusCode != 401 {
  109. t.Error("Expected 401, got", resp.Status)
  110. t.Fail()
  111. }
  112. respSession := Session{}
  113. json.NewDecoder(resp.Body).Decode(&respSession)
  114. if respSession.UserID != "" {
  115. t.Errorf("A user ID in supposedly empty session")
  116. t.Fail()
  117. }
  118. })
  119. }