internal_test.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. package api
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "net/http/httptest"
  7. "testing"
  8. "github.com/drakkan/sftpgo/dataprovider"
  9. "github.com/go-chi/chi"
  10. )
  11. const (
  12. invalidURL = "http://foo\x7f.com/"
  13. inactiveURL = "http://127.0.0.1:12345"
  14. )
  15. func TestGetRespStatus(t *testing.T) {
  16. var err error
  17. err = &dataprovider.MethodDisabledError{}
  18. respStatus := getRespStatus(err)
  19. if respStatus != http.StatusForbidden {
  20. t.Errorf("wrong resp status extected: %d got: %d", http.StatusForbidden, respStatus)
  21. }
  22. err = fmt.Errorf("generic error")
  23. respStatus = getRespStatus(err)
  24. if respStatus != http.StatusInternalServerError {
  25. t.Errorf("wrong resp status extected: %d got: %d", http.StatusInternalServerError, respStatus)
  26. }
  27. }
  28. func TestCheckResponse(t *testing.T) {
  29. err := checkResponse(http.StatusOK, http.StatusCreated)
  30. if err == nil {
  31. t.Errorf("check must fail")
  32. }
  33. err = checkResponse(http.StatusBadRequest, http.StatusBadRequest)
  34. if err != nil {
  35. t.Errorf("test must succeed, error: %v", err)
  36. }
  37. }
  38. func TestCheckUser(t *testing.T) {
  39. expected := dataprovider.User{}
  40. actual := dataprovider.User{}
  41. actual.Password = "password"
  42. err := checkUser(expected, actual)
  43. if err == nil {
  44. t.Errorf("actual password must be nil")
  45. }
  46. actual.Password = ""
  47. actual.PublicKeys = []string{"pub key"}
  48. err = checkUser(expected, actual)
  49. if err == nil {
  50. t.Errorf("actual public key must be nil")
  51. }
  52. actual.PublicKeys = []string{}
  53. err = checkUser(expected, actual)
  54. if err == nil {
  55. t.Errorf("actual ID must be > 0")
  56. }
  57. expected.ID = 1
  58. actual.ID = 2
  59. err = checkUser(expected, actual)
  60. if err == nil {
  61. t.Errorf("actual ID must be equal to expected ID")
  62. }
  63. expected.ID = 2
  64. actual.ID = 2
  65. expected.Permissions = []string{dataprovider.PermCreateDirs, dataprovider.PermDelete, dataprovider.PermDownload}
  66. actual.Permissions = []string{dataprovider.PermCreateDirs, dataprovider.PermCreateSymlinks}
  67. err = checkUser(expected, actual)
  68. if err == nil {
  69. t.Errorf("Permissions are not equal")
  70. }
  71. expected.Permissions = append(expected.Permissions, dataprovider.PermRename)
  72. err = checkUser(expected, actual)
  73. if err == nil {
  74. t.Errorf("Permissions are not equal")
  75. }
  76. }
  77. func TestCompareUserFields(t *testing.T) {
  78. expected := dataprovider.User{}
  79. actual := dataprovider.User{}
  80. expected.Username = "test"
  81. err := compareEqualsUserFields(expected, actual)
  82. if err == nil {
  83. t.Errorf("Username does not match")
  84. }
  85. expected.Username = ""
  86. expected.HomeDir = "homedir"
  87. err = compareEqualsUserFields(expected, actual)
  88. if err == nil {
  89. t.Errorf("HomeDir does not match")
  90. }
  91. expected.HomeDir = ""
  92. expected.UID = 1
  93. err = compareEqualsUserFields(expected, actual)
  94. if err == nil {
  95. t.Errorf("UID does not match")
  96. }
  97. expected.UID = 0
  98. expected.GID = 1
  99. err = compareEqualsUserFields(expected, actual)
  100. if err == nil {
  101. t.Errorf("GID does not match")
  102. }
  103. expected.GID = 0
  104. expected.MaxSessions = 2
  105. err = compareEqualsUserFields(expected, actual)
  106. if err == nil {
  107. t.Errorf("MaxSessions do not match")
  108. }
  109. expected.MaxSessions = 0
  110. expected.QuotaSize = 4096
  111. err = compareEqualsUserFields(expected, actual)
  112. if err == nil {
  113. t.Errorf("QuotaSize does not match")
  114. }
  115. expected.QuotaSize = 0
  116. expected.QuotaFiles = 2
  117. err = compareEqualsUserFields(expected, actual)
  118. if err == nil {
  119. t.Errorf("QuotaFiles do not match")
  120. }
  121. expected.QuotaFiles = 0
  122. expected.Permissions = []string{dataprovider.PermCreateDirs}
  123. err = compareEqualsUserFields(expected, actual)
  124. if err == nil {
  125. t.Errorf("Permissions are not equal")
  126. }
  127. expected.Permissions = nil
  128. expected.UploadBandwidth = 64
  129. err = compareEqualsUserFields(expected, actual)
  130. if err == nil {
  131. t.Errorf("UploadBandwidth does not match")
  132. }
  133. expected.UploadBandwidth = 0
  134. expected.DownloadBandwidth = 128
  135. err = compareEqualsUserFields(expected, actual)
  136. if err == nil {
  137. t.Errorf("DownloadBandwidth does not match")
  138. }
  139. }
  140. func TestApiCallsWithBadURL(t *testing.T) {
  141. oldBaseURL := httpBaseURL
  142. SetBaseURL(invalidURL)
  143. u := dataprovider.User{}
  144. _, _, err := UpdateUser(u, http.StatusBadRequest)
  145. if err == nil {
  146. t.Errorf("request with invalid URL must fail")
  147. }
  148. _, err = RemoveUser(u, http.StatusNotFound)
  149. if err == nil {
  150. t.Errorf("request with invalid URL must fail")
  151. }
  152. _, _, err = GetUsers(1, 0, "", http.StatusBadRequest)
  153. if err == nil {
  154. t.Errorf("request with invalid URL must fail")
  155. }
  156. _, err = CloseSFTPConnection("non_existent_id", http.StatusNotFound)
  157. if err == nil {
  158. t.Errorf("request with invalid URL must fail")
  159. }
  160. SetBaseURL(oldBaseURL)
  161. }
  162. func TestApiCallToNotListeningServer(t *testing.T) {
  163. oldBaseURL := httpBaseURL
  164. SetBaseURL(inactiveURL)
  165. u := dataprovider.User{}
  166. _, _, err := AddUser(u, http.StatusBadRequest)
  167. if err == nil {
  168. t.Errorf("request to an inactive URL must fail")
  169. }
  170. _, _, err = UpdateUser(u, http.StatusNotFound)
  171. if err == nil {
  172. t.Errorf("request to an inactive URL must fail")
  173. }
  174. _, err = RemoveUser(u, http.StatusNotFound)
  175. if err == nil {
  176. t.Errorf("request to an inactive URL must fail")
  177. }
  178. _, _, err = GetUserByID(-1, http.StatusNotFound)
  179. if err == nil {
  180. t.Errorf("request to an inactive URL must fail")
  181. }
  182. _, _, err = GetUsers(100, 0, "", http.StatusOK)
  183. if err == nil {
  184. t.Errorf("request to an inactive URL must fail")
  185. }
  186. _, _, err = GetQuotaScans(http.StatusOK)
  187. if err == nil {
  188. t.Errorf("request to an inactive URL must fail")
  189. }
  190. _, err = StartQuotaScan(u, http.StatusNotFound)
  191. if err == nil {
  192. t.Errorf("request to an inactive URL must fail")
  193. }
  194. _, _, err = GetSFTPConnections(http.StatusOK)
  195. if err == nil {
  196. t.Errorf("request to an inactive URL must fail")
  197. }
  198. _, err = CloseSFTPConnection("non_existent_id", http.StatusNotFound)
  199. if err == nil {
  200. t.Errorf("request to an inactive URL must fail")
  201. }
  202. _, _, err = GetVersion(http.StatusOK)
  203. if err == nil {
  204. t.Errorf("request to an inactive URL must fail")
  205. }
  206. SetBaseURL(oldBaseURL)
  207. }
  208. func TestCloseSFTPConnectionHandler(t *testing.T) {
  209. req, _ := http.NewRequest(http.MethodDelete, activeConnectionsPath+"/connectionID", nil)
  210. rctx := chi.NewRouteContext()
  211. rctx.URLParams.Add("connectionID", "")
  212. req = req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, rctx))
  213. rr := httptest.NewRecorder()
  214. handleCloseSFTPConnection(rr, req)
  215. if rr.Code != http.StatusBadRequest {
  216. t.Errorf("Expected response code 400. Got %d", rr.Code)
  217. }
  218. }