user_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. package main
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "io/ioutil"
  6. "net/http"
  7. "os"
  8. "sync"
  9. "testing"
  10. "time"
  11. controller "github.com/gravitl/netmaker/controllers"
  12. "github.com/gravitl/netmaker/models"
  13. "github.com/gravitl/netmaker/mongoconn"
  14. "github.com/stretchr/testify/assert"
  15. )
  16. type AuthorizationResponse struct {
  17. Username string
  18. AuthToken string
  19. }
  20. type goodResponse struct {
  21. Code int
  22. Message string
  23. Response AuthorizationResponse
  24. }
  25. type badResponse struct {
  26. Code int
  27. Message string
  28. }
  29. //assumption: starting with empty database
  30. func TestMain(m *testing.M) {
  31. mongoconn.ConnectDatabase()
  32. var waitgroup sync.WaitGroup
  33. waitgroup.Add(1)
  34. go controller.HandleRESTRequests(&waitgroup)
  35. //wait for http server to start
  36. time.Sleep(time.Second * 1)
  37. os.Exit(m.Run())
  38. }
  39. func TestUsers(t *testing.T) {
  40. t.Run("check that admin user does not exist", func(t *testing.T) {
  41. response := checkAdminExists(t)
  42. assert.Equal(t, false, response)
  43. })
  44. t.Run("add admin user", func(t *testing.T) {
  45. var admin, user models.User
  46. admin.UserName = "admin"
  47. admin.Password = "password"
  48. //payload := map[string]string{"username": "admin", "password": "admin"}
  49. payload, _ := json.Marshal(admin)
  50. request, err := http.NewRequest(http.MethodPost, "http://localhost:8081/users/createadmin", bytes.NewBuffer(payload))
  51. if err != nil {
  52. t.Error(err)
  53. }
  54. request.Header.Set("Authorization", "Bearer secretkey")
  55. request.Header.Set("Content-Type", "application/json")
  56. client := &http.Client{}
  57. response, err := client.Do(request)
  58. if err != nil {
  59. t.Error("error calling createadmin", err)
  60. }
  61. defer response.Body.Close()
  62. body, _ := ioutil.ReadAll(response.Body)
  63. _ = json.Unmarshal(body, &user)
  64. assert.Equal(t, admin.UserName, user.UserName)
  65. assert.Equal(t, true, user.IsAdmin)
  66. assert.Equal(t, http.StatusOK, response.StatusCode)
  67. adminExists := checkAdminExists(t)
  68. assert.Equal(t, true, adminExists)
  69. })
  70. t.Run("GetUser", func(t *testing.T) {
  71. t.Skip()
  72. //ensure admin exists
  73. if !checkAdminExists(t) {
  74. t.Error("admin account does not exist")
  75. return
  76. }
  77. //authenticate
  78. var admin models.User
  79. admin.UserName = "admin"
  80. admin.Password = "admin"
  81. payload, _ := json.Marshal(admin)
  82. request, err := http.NewRequest(http.MethodPut, "http://localhost:8081/users/authenticate", bytes.NewBuffer(payload))
  83. if err != nil {
  84. t.Error(err)
  85. }
  86. request.Header.Set("Content-Type", "application/json")
  87. client := &http.Client{}
  88. response, err := client.Do(request)
  89. if err != nil {
  90. t.Error("error calling authenticate", err)
  91. }
  92. defer response.Body.Close()
  93. body := models.User{}
  94. json.NewDecoder(response.Body).Decode(&body)
  95. t.Log(body)
  96. assert.Equal(t, http.StatusOK, response.StatusCode)
  97. assert.Equal(t, "admin", body.UserName)
  98. request, err = http.NewRequest(http.MethodGet, "http://localhost:8081/users/admim", nil)
  99. if err != nil {
  100. t.Error(err)
  101. }
  102. request.Header.Set("Authorization", "Bearer secretkey")
  103. client = &http.Client{}
  104. response, err = client.Do(request)
  105. if err != nil {
  106. t.Error(err)
  107. }
  108. defer response.Body.Close()
  109. body = models.User{}
  110. json.NewDecoder(response.Body).Decode(&body)
  111. t.Log(body)
  112. assert.Equal(t, http.StatusOK, response.StatusCode)
  113. assert.Equal(t, "admin", body.UserName)
  114. })
  115. t.Run("Update User", func(t *testing.T) {
  116. t.Skip()
  117. var admin, user models.User
  118. admin.UserName = "admin"
  119. admin.Password = "admin"
  120. //payload := map[string]string{"username": "admin", "password": "admin"}
  121. payload, _ := json.Marshal(admin)
  122. request, err := http.NewRequest(http.MethodPut, "http://localhost:8081/users/admin", bytes.NewBuffer(payload))
  123. if err != nil {
  124. t.Error(err)
  125. }
  126. request.Header.Set("Authorization", "Bearer secretkey")
  127. request.Header.Set("Content-Type", "application/json")
  128. client := &http.Client{}
  129. response, err := client.Do(request)
  130. if err != nil {
  131. t.Error("error calling createadmin", err)
  132. }
  133. defer response.Body.Close()
  134. body, _ := ioutil.ReadAll(response.Body)
  135. t.Log(string(body))
  136. _ = json.Unmarshal(body, &user)
  137. assert.Equal(t, admin.UserName, user.UserName)
  138. assert.Equal(t, true, user.IsAdmin)
  139. assert.Equal(t, http.StatusOK, response.StatusCode)
  140. })
  141. t.Run("Authenticate User", func(t *testing.T) {
  142. var admin models.User
  143. admin.UserName = "admin"
  144. admin.Password = "password"
  145. payload, _ := json.Marshal(admin)
  146. request, err := http.NewRequest(http.MethodPost, "http://localhost:8081/users/authenticate", bytes.NewBuffer(payload))
  147. if err != nil {
  148. t.Error(err)
  149. }
  150. request.Header.Set("Authorization", "Bearer secretkey")
  151. request.Header.Set("Content-Type", "application/json")
  152. client := &http.Client{}
  153. response, err := client.Do(request)
  154. if err != nil {
  155. t.Error(err)
  156. }
  157. defer response.Body.Close()
  158. message := goodResponse{}
  159. json.NewDecoder(response.Body).Decode(&message)
  160. assert.Equal(t, http.StatusOK, response.StatusCode)
  161. assert.Equal(t, "W1R3: Device admin Authorized", message.Message)
  162. })
  163. t.Run("empty test", func(t *testing.T) {
  164. assert.Equal(t, true, true)
  165. })
  166. }
  167. func checkAdminExists(t *testing.T) bool {
  168. response, err := http.Get("http://localhost:8081/users/hasadmin")
  169. if err != nil {
  170. t.Fatal("error calling users/hasadmin", err)
  171. }
  172. defer response.Body.Close()
  173. var body bool
  174. json.NewDecoder(response.Body).Decode(&body)
  175. return body
  176. //body, _ := ioutil.ReadAll(response.Body)
  177. //vif body {
  178. // return true
  179. //}
  180. //return false
  181. }