Encrypted messaging app
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.

106 lines
2.1 KiB

  1. package Users_test
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "net/http"
  7. "testing"
  8. "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models"
  9. "git.tovijaeschke.xyz/tovi/Envelope/Backend/Tests"
  10. )
  11. func Test_SearchUsers(t *testing.T) {
  12. client, ts, err := Tests.InitTestEnv()
  13. if err != nil {
  14. t.Errorf("Expected nil, recieved %s", err.Error())
  15. return
  16. }
  17. u2, err := Tests.InitTestCreateUser("abcd")
  18. req, _ := http.NewRequest(
  19. "GET",
  20. fmt.Sprintf("%s/api/v1/auth/users?username=%s", ts.URL, u2.Username),
  21. nil,
  22. )
  23. resp, err := client.Do(req)
  24. if err != nil {
  25. t.Errorf("Expected nil, recieved %s", err.Error())
  26. return
  27. }
  28. if resp.StatusCode != http.StatusOK {
  29. t.Errorf("Expected %d, recieved %d", http.StatusOK, resp.StatusCode)
  30. return
  31. }
  32. requestBody, err := ioutil.ReadAll(resp.Body)
  33. if err != nil {
  34. t.Errorf("Expected %d, recieved %d", http.StatusOK, resp.StatusCode)
  35. return
  36. }
  37. var user Models.User
  38. json.Unmarshal(requestBody, &user)
  39. if user.Username != "abcd" {
  40. t.Errorf("Expected abcd, recieved %s", user.Username)
  41. return
  42. }
  43. if user.Password != "" {
  44. t.Errorf("Expected \"\", recieved %s", user.Password)
  45. return
  46. }
  47. if user.AsymmetricPrivateKey != "" {
  48. t.Errorf("Expected \"\", recieved %s", user.AsymmetricPrivateKey)
  49. return
  50. }
  51. }
  52. func Test_SearchUsersPartialMatchFails(t *testing.T) {
  53. client, ts, err := Tests.InitTestEnv()
  54. if err != nil {
  55. t.Errorf("Expected nil, recieved %s", err.Error())
  56. return
  57. }
  58. _, err = Tests.InitTestCreateUser("abcd")
  59. req, _ := http.NewRequest(
  60. "GET",
  61. fmt.Sprintf("%s/api/v1/auth/users?username=%s", ts.URL, "abc"),
  62. nil,
  63. )
  64. resp, err := client.Do(req)
  65. if err != nil {
  66. t.Errorf("Expected nil, recieved %s", err.Error())
  67. return
  68. }
  69. if resp.StatusCode != http.StatusNotFound {
  70. t.Errorf("Expected %d, recieved %d", http.StatusOK, resp.StatusCode)
  71. return
  72. }
  73. requestBody, err := ioutil.ReadAll(resp.Body)
  74. if err != nil {
  75. t.Errorf("Expected %d, recieved %d", http.StatusOK, resp.StatusCode)
  76. return
  77. }
  78. var user interface{}
  79. json.Unmarshal(requestBody, &user)
  80. if user != nil {
  81. t.Errorf("Expected nil, recieved %+v", user)
  82. return
  83. }
  84. }