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.

126 lines
2.5 KiB

  1. package handler_test
  2. import (
  3. "net/http/httptest"
  4. "net/url"
  5. "strconv"
  6. "testing"
  7. "git.hoogi.eu/snafu/go-blog/handler"
  8. "git.hoogi.eu/snafu/go-blog/models"
  9. )
  10. func TestUserInviteWorkflow(t *testing.T) {
  11. setup(t)
  12. defer teardown()
  13. ui := &models.UserInvite{
  14. DisplayName: "Homer Simpson",
  15. Email: "homer@example.com",
  16. Username: "homer",
  17. IsAdmin: false,
  18. }
  19. inviteID, hash, err := doAdminCreateUserInviteRequest(rAdminUser, ui)
  20. if err != nil {
  21. t.Fatal(err)
  22. }
  23. if len(hash) == 0 {
  24. t.Error("no hash received")
  25. }
  26. inviteID, hash, err = doAdminResendUserInviteRequest(rAdminUser, inviteID)
  27. if err != nil {
  28. t.Fatal(err)
  29. }
  30. if len(hash) == 0 {
  31. t.Error("no hash received")
  32. }
  33. err = doAdminRemoveUserInviteRequest(rAdminUser, inviteID)
  34. if err != nil {
  35. t.Fatal(err)
  36. }
  37. _, _, err = doAdminResendUserInviteRequest(rAdminUser, inviteID)
  38. if err == nil {
  39. t.Errorf("removed user invitation is still there, inviteID %d", inviteID)
  40. }
  41. }
  42. func doAdminCreateUserInviteRequest(user reqUser, ui *models.UserInvite) (int, string, error) {
  43. values := url.Values{}
  44. addValue(values, "displayname", ui.DisplayName)
  45. addValue(values, "username", ui.Username)
  46. addValue(values, "email", ui.Email)
  47. addCheckboxValue(values, "admin", ui.IsAdmin)
  48. r := request{
  49. url: "/admin/user-invite/new",
  50. user: user,
  51. method: "POST",
  52. values: values,
  53. }
  54. rw := httptest.NewRecorder()
  55. tpl := handler.AdminUserInviteNewPostHandler(ctx, rw, r.buildRequest())
  56. if tpl.Err != nil {
  57. return -1, "", tpl.Err
  58. }
  59. return tpl.Data["inviteID"].(int), tpl.Data["hash"].(string), nil
  60. }
  61. func doAdminResendUserInviteRequest(user reqUser, inviteID int) (int, string, error) {
  62. r := request{
  63. url: "/admin/user-invite/resend/" + strconv.Itoa(inviteID),
  64. user: user,
  65. method: "POST",
  66. pathVar: []pathVar{
  67. pathVar{
  68. key: "inviteID",
  69. value: strconv.Itoa(inviteID),
  70. },
  71. },
  72. }
  73. rw := httptest.NewRecorder()
  74. tpl := handler.AdminUserInviteResendPostHandler(ctx, rw, r.buildRequest())
  75. if tpl.Err != nil {
  76. return -1, "", tpl.Err
  77. }
  78. return tpl.Data["inviteID"].(int), tpl.Data["hash"].(string), nil
  79. }
  80. func doAdminRemoveUserInviteRequest(user reqUser, inviteID int) error {
  81. r := request{
  82. url: "/admin/user-invite/delete/" + strconv.Itoa(inviteID),
  83. user: user,
  84. method: "POST",
  85. pathVar: []pathVar{
  86. pathVar{
  87. key: "inviteID",
  88. value: strconv.Itoa(inviteID),
  89. },
  90. },
  91. }
  92. rw := httptest.NewRecorder()
  93. tpl := handler.AdminUserInviteDeletePostHandler(ctx, rw, r.buildRequest())
  94. if tpl.Err != nil {
  95. return tpl.Err
  96. }
  97. return nil
  98. }