internal_test.go 5.6 KB

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