userapi_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package main
  2. import (
  3. "encoding/json"
  4. "io/ioutil"
  5. "net/http"
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. "wkla.no-ip.biz/gogs/Willie/MsgService/MessageService/model"
  9. )
  10. func TestGetUsers(t *testing.T) {
  11. url := baseSslURL + restEndpoints + "users"
  12. resp, err := getGetRequest(url, AdminUser)
  13. if err != nil {
  14. t.Errorf("Error getting response. %v", err)
  15. return
  16. }
  17. defer resp.Body.Close()
  18. if resp.StatusCode != http.StatusOK {
  19. t.Errorf("Wrong statuscode: %d", resp.StatusCode)
  20. return
  21. }
  22. bodyText, err := ioutil.ReadAll(resp.Body)
  23. var users []model.User
  24. err = json.Unmarshal(bodyText, &users)
  25. if err != nil {
  26. t.Errorf("Error ummarshalling response. %v", err)
  27. return
  28. }
  29. t.Logf("response: url: %s, response: %v", url, users)
  30. if len(users) != 3 {
  31. t.Errorf("predefined user count not correct: %d", len(users))
  32. return
  33. }
  34. t.Log("Get users OK.")
  35. }
  36. func TestPostUser(t *testing.T) {
  37. url := baseSslURL + restEndpoints + "users"
  38. newUser := model.User{
  39. Name: "newuser",
  40. Password: "newuser",
  41. Admin: false,
  42. Guest: false,
  43. Roles: []string{"admin", "edit", "read"},
  44. }
  45. basicUser := BasicUser{name: newUser.Name, pwd: newUser.Password}
  46. payload, err := json.Marshal(newUser)
  47. if err != nil {
  48. t.Errorf("Error marshall new user. %v", err)
  49. return
  50. }
  51. resp, err := getPostRequest(url, AdminUser, payload)
  52. if err != nil {
  53. t.Errorf("Error getting response. %v", err)
  54. return
  55. }
  56. defer resp.Body.Close()
  57. bodyText, err := ioutil.ReadAll(resp.Body)
  58. s := string(bodyText)
  59. assert.Equal(t, http.StatusCreated, resp.StatusCode, "Wrong statuscode: %d, %s", resp.StatusCode, s)
  60. var user model.User
  61. err = json.Unmarshal(bodyText, &user)
  62. if err != nil {
  63. t.Errorf("Error ummarshalling response. %v", err)
  64. return
  65. }
  66. t.Logf("response: url: %s, response: %v", url, user)
  67. assert.Equal(t, newUser.Name, user.Name, "user name not identically")
  68. assert.Equal(t, "#####", user.Password, "password not identically")
  69. assert.Equal(t, newUser.Admin, user.Admin, "admin not identically")
  70. assert.Equal(t, newUser.Guest, user.Guest, "guest not identically")
  71. assert.Contains(t, user.Roles, "admin", "roles not inserted")
  72. assert.Contains(t, user.Roles, "edit", "roles not inserted")
  73. assert.Contains(t, user.Roles, "read", "roles not inserted")
  74. url = baseSslURL + restEndpoints + "users/" + basicUser.name
  75. resp, err = getGetRequest(url, basicUser)
  76. if err != nil {
  77. t.Errorf("Error getting response. %v", err)
  78. return
  79. }
  80. defer resp.Body.Close()
  81. if resp.StatusCode != http.StatusOK {
  82. t.Errorf("Wrong statuscode: %d", resp.StatusCode)
  83. return
  84. }
  85. resp, err = getDeleteRequest(url, AdminUser)
  86. if err != nil {
  87. t.Errorf("Error getting response. %v", err)
  88. return
  89. }
  90. defer resp.Body.Close()
  91. if resp.StatusCode != http.StatusOK {
  92. t.Errorf("Wrong statuscode: %d", resp.StatusCode)
  93. return
  94. }
  95. t.Log("Get users OK.")
  96. }