user_test.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. package main
  2. import (
  3. "encoding/json"
  4. "io/ioutil"
  5. "net/http"
  6. "testing"
  7. "github.com/gravitl/netmaker/models"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func TestAdminCreation(t *testing.T) {
  11. var admin models.UserAuthParams
  12. var user models.User
  13. admin.UserName = "admin"
  14. admin.Password = "password"
  15. t.Run("AdminCreationSuccess", func(t *testing.T) {
  16. if adminExists(t) {
  17. deleteAdmin(t)
  18. }
  19. response, err := api(t, admin, http.MethodPost, "http://localhost:8081/api/users/adm/createadmin", "")
  20. assert.Nil(t, err, err)
  21. defer response.Body.Close()
  22. err = json.NewDecoder(response.Body).Decode(&user)
  23. assert.Nil(t, err, err)
  24. assert.Equal(t, admin.UserName, user.UserName)
  25. assert.Equal(t, true, user.IsAdmin)
  26. assert.Equal(t, http.StatusOK, response.StatusCode)
  27. assert.True(t, adminExists(t), "Admin creation failed")
  28. message, _ := ioutil.ReadAll(response.Body)
  29. t.Log(string(message))
  30. })
  31. t.Run("AdminCreationFailure", func(t *testing.T) {
  32. if !adminExists(t) {
  33. addAdmin(t)
  34. }
  35. response, err := api(t, admin, http.MethodPost, "http://localhost:8081/api/users/adm/createadmin", "")
  36. assert.Nil(t, err, err)
  37. defer response.Body.Close()
  38. var message models.ErrorResponse
  39. err = json.NewDecoder(response.Body).Decode(&message)
  40. t.Log(message)
  41. assert.Nil(t, err, err)
  42. assert.Equal(t, http.StatusUnauthorized, response.StatusCode)
  43. assert.Equal(t, http.StatusUnauthorized, message.Code)
  44. assert.Equal(t, "W1R3: Admin already exists! ", message.Message)
  45. data, _ := ioutil.ReadAll(response.Body)
  46. t.Log(string(data))
  47. })
  48. }
  49. func TestGetUser(t *testing.T) {
  50. if !adminExists(t) {
  51. t.Log("no admin - creating")
  52. addAdmin(t)
  53. } else {
  54. t.Log("admin exists")
  55. }
  56. t.Run("GetUserWithValidToken", func(t *testing.T) {
  57. token, err := authenticate(t)
  58. assert.Nil(t, err, err)
  59. response, err := api(t, "", http.MethodGet, "http://localhost:8081/api/users/admin", token)
  60. t.Log(response)
  61. assert.Nil(t, err, err)
  62. defer response.Body.Close()
  63. var user models.User
  64. json.NewDecoder(response.Body).Decode(&user)
  65. assert.Equal(t, http.StatusOK, response.StatusCode)
  66. assert.Equal(t, "admin", user.UserName)
  67. assert.Equal(t, true, user.IsAdmin)
  68. })
  69. t.Run("GetUserWithInvalidToken", func(t *testing.T) {
  70. response, err := api(t, "", http.MethodGet, "http://localhost:8081/api/users/admin", "secretkey")
  71. assert.Nil(t, err, err)
  72. defer response.Body.Close()
  73. t.Log(response.Body)
  74. })
  75. }
  76. func TestUpdateUser(t *testing.T) {
  77. if !adminExists(t) {
  78. addAdmin(t)
  79. }
  80. token, err := authenticate(t)
  81. assert.Nil(t, err, err)
  82. var admin models.UserAuthParams
  83. var user models.User
  84. var message models.ErrorResponse
  85. t.Run("UpdateWrongToken", func(t *testing.T) {
  86. admin.UserName = "admin"
  87. admin.Password = "admin"
  88. response, err := api(t, admin, http.MethodPut, "http://localhost:8081/api/users/admin", "secretkey")
  89. assert.Nil(t, err, err)
  90. defer response.Body.Close()
  91. err = json.NewDecoder(response.Body).Decode(&message)
  92. assert.Nil(t, err, err)
  93. assert.Equal(t, "W1R3: Error Verifying Auth Token.", message.Message)
  94. assert.Equal(t, http.StatusUnauthorized, response.StatusCode)
  95. })
  96. t.Run("UpdateSuccess", func(t *testing.T) {
  97. admin.UserName = "admin"
  98. admin.Password = "password"
  99. response, err := api(t, admin, http.MethodPut, "http://localhost:8081/api/users/admin", token)
  100. assert.Nil(t, err, err)
  101. defer response.Body.Close()
  102. err = json.NewDecoder(response.Body).Decode(&user)
  103. assert.Nil(t, err, err)
  104. assert.Equal(t, admin.UserName, user.UserName)
  105. assert.Equal(t, true, user.IsAdmin)
  106. assert.Equal(t, http.StatusOK, response.StatusCode)
  107. })
  108. }
  109. func TestDeleteUser(t *testing.T) {
  110. if !adminExists(t) {
  111. t.Log("Creating Admin")
  112. addAdmin(t)
  113. }
  114. token, err := authenticate(t)
  115. assert.Nil(t, err, err)
  116. t.Run("DeleteUser-InvalidCredentials", func(t *testing.T) {
  117. response, err := api(t, "", http.MethodDelete, "http://localhost:8081/api/users/admin", "badcredentials")
  118. assert.Nil(t, err, err)
  119. assert.Equal(t, http.StatusUnauthorized, response.StatusCode)
  120. var message models.ErrorResponse
  121. json.NewDecoder(response.Body).Decode(&message)
  122. assert.Equal(t, "W1R3: Error Verifying Auth Token.", message.Message)
  123. assert.Equal(t, http.StatusUnauthorized, response.StatusCode)
  124. })
  125. t.Run("DeleteUser-ValidCredentials", func(t *testing.T) {
  126. response, err := api(t, "", http.MethodDelete, "http://localhost:8081/api/users/admin", token)
  127. assert.Nil(t, err, err)
  128. var body string
  129. json.NewDecoder(response.Body).Decode(&body)
  130. assert.Equal(t, "admin deleted.", body)
  131. assert.Equal(t, http.StatusOK, response.StatusCode)
  132. })
  133. t.Run("DeleteUser-NonExistantAdmin", func(t *testing.T) {
  134. response, err := api(t, "", http.MethodDelete, "http://localhost:8081/api/users/admin", token)
  135. assert.Nil(t, err, err)
  136. assert.Equal(t, http.StatusBadRequest, response.StatusCode)
  137. var message models.ErrorResponse
  138. defer response.Body.Close()
  139. json.NewDecoder(response.Body).Decode(&message)
  140. assert.Equal(t, http.StatusBadRequest, message.Code)
  141. assert.Equal(t, "Delete unsuccessful.", message.Message)
  142. })
  143. }
  144. func TestAuthenticateUser(t *testing.T) {
  145. cases := []AuthorizeTestCase{
  146. AuthorizeTestCase{
  147. testname: "Invalid User",
  148. name: "invaliduser",
  149. password: "password",
  150. code: http.StatusBadRequest,
  151. tokenExpected: false,
  152. errMessage: "W1R3: User invaliduser not found.",
  153. },
  154. AuthorizeTestCase{
  155. testname: "empty user",
  156. name: "",
  157. password: "password",
  158. code: http.StatusBadRequest,
  159. tokenExpected: false,
  160. errMessage: "W1R3: Username can't be empty",
  161. },
  162. AuthorizeTestCase{
  163. testname: "empty password",
  164. name: "admin",
  165. password: "",
  166. code: http.StatusBadRequest,
  167. tokenExpected: false,
  168. errMessage: "W1R3: Password can't be empty",
  169. },
  170. AuthorizeTestCase{
  171. testname: "Invalid Password",
  172. name: "admin",
  173. password: "xxxxxxx",
  174. code: http.StatusUnauthorized,
  175. tokenExpected: false,
  176. errMessage: "W1R3: Wrong Password.",
  177. },
  178. AuthorizeTestCase{
  179. testname: "Valid User",
  180. name: "admin",
  181. password: "password",
  182. code: http.StatusOK,
  183. tokenExpected: true,
  184. errMessage: "W1R3: Device Admin Authorized",
  185. },
  186. }
  187. if !adminExists(t) {
  188. addAdmin(t)
  189. }
  190. for _, tc := range cases {
  191. t.Run(tc.testname, func(t *testing.T) {
  192. var admin models.User
  193. admin.UserName = tc.name
  194. admin.Password = tc.password
  195. response, err := api(t, admin, http.MethodPost, "http://localhost:8081/api/users/adm/authenticate", "secretkey")
  196. assert.Nil(t, err, err)
  197. defer response.Body.Close()
  198. if tc.tokenExpected {
  199. var body Success
  200. err = json.NewDecoder(response.Body).Decode(&body)
  201. assert.Nil(t, err, err)
  202. assert.NotEmpty(t, body.Response.AuthToken, "token not returned")
  203. assert.Equal(t, "W1R3: Device admin Authorized", body.Message)
  204. } else {
  205. var bad models.ErrorResponse
  206. json.NewDecoder(response.Body).Decode(&bad)
  207. assert.Equal(t, tc.errMessage, bad.Message)
  208. }
  209. assert.Equal(t, tc.code, response.StatusCode)
  210. })
  211. }
  212. }