mirror_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package models
  2. import (
  3. "testing"
  4. . "github.com/smartystreets/goconvey/convey"
  5. )
  6. func Test_parseRemoteUpdateOutput(t *testing.T) {
  7. Convey("Parse mirror remote update output", t, func() {
  8. testCases := []struct {
  9. output string
  10. results []*mirrorSyncResult
  11. }{
  12. {
  13. `
  14. From https://gitlab.com/yoginth/upsteam
  15. * [new branch] develop -> develop
  16. b0bb24f..1d85a4f master -> master
  17. - [deleted] (none) -> bugfix
  18. `,
  19. []*mirrorSyncResult{
  20. {"develop", GIT_SHORT_EMPTY_SHA, ""},
  21. {"master", "b0bb24f", "1d85a4f"},
  22. {"bugfix", "", GIT_SHORT_EMPTY_SHA},
  23. },
  24. },
  25. }
  26. for _, tc := range testCases {
  27. results := parseRemoteUpdateOutput(tc.output)
  28. So(len(results), ShouldEqual, len(tc.results))
  29. for i := range tc.results {
  30. So(tc.results[i].refName, ShouldEqual, results[i].refName)
  31. So(tc.results[i].oldCommitID, ShouldEqual, results[i].oldCommitID)
  32. So(tc.results[i].newCommitID, ShouldEqual, results[i].newCommitID)
  33. }
  34. }
  35. })
  36. }
  37. func Test_findPasswordInMirrorAddress(t *testing.T) {
  38. Convey("Find password portion in mirror address", t, func() {
  39. testCases := []struct {
  40. addr string
  41. start, end int
  42. found bool
  43. password string
  44. }{
  45. {"http://localhost:3000/user/repo.git", -1, -1, false, ""},
  46. {"http://user@localhost:3000/user/repo.git", -1, -1, false, ""},
  47. {"http://user:@localhost:3000/user/repo.git", -1, -1, false, ""},
  48. {"http://user:password@localhost:3000/user/repo.git", 12, 20, true, "password"},
  49. {"http://username:my%3Asecure%3Bpassword@localhost:3000/user/repo.git", 16, 38, true, "my%3Asecure%3Bpassword"},
  50. {"http://username:my%40secure%23password@localhost:3000/user/repo.git", 16, 38, true, "my%40secure%23password"},
  51. {"http://username:@@localhost:3000/user/repo.git", 16, 17, true, "@"},
  52. }
  53. for _, tc := range testCases {
  54. start, end, found := findPasswordInMirrorAddress(tc.addr)
  55. So(start, ShouldEqual, tc.start)
  56. So(end, ShouldEqual, tc.end)
  57. So(found, ShouldEqual, tc.found)
  58. if found {
  59. So(tc.addr[start:end], ShouldEqual, tc.password)
  60. }
  61. }
  62. })
  63. }
  64. func Test_unescapeMirrorCredentials(t *testing.T) {
  65. Convey("Escape credentials in mirror address", t, func() {
  66. testCases := []string{
  67. "http://localhost:3000/user/repo.git", "http://localhost:3000/user/repo.git",
  68. "http://user@localhost:3000/user/repo.git", "http://user@localhost:3000/user/repo.git",
  69. "http://user:@localhost:3000/user/repo.git", "http://user:@localhost:3000/user/repo.git",
  70. "http://user:password@localhost:3000/user/repo.git", "http://user:password@localhost:3000/user/repo.git",
  71. "http://user:my%3Asecure%3Bpassword@localhost:3000/user/repo.git", "http://user:my:secure;password@localhost:3000/user/repo.git",
  72. "http://user:my%40secure%23password@localhost:3000/user/repo.git", "http://user:my@secure#password@localhost:3000/user/repo.git",
  73. }
  74. for i := 0; i < len(testCases); i += 2 {
  75. So(unescapeMirrorCredentials(testCases[i]), ShouldEqual, testCases[i+1])
  76. }
  77. })
  78. }
  79. func Test_escapeMirrorCredentials(t *testing.T) {
  80. Convey("Escape credentials in mirror address", t, func() {
  81. testCases := []string{
  82. "http://localhost:3000/user/repo.git", "http://localhost:3000/user/repo.git",
  83. "http://user@localhost:3000/user/repo.git", "http://user@localhost:3000/user/repo.git",
  84. "http://user:@localhost:3000/user/repo.git", "http://user:@localhost:3000/user/repo.git",
  85. "http://user:password@localhost:3000/user/repo.git", "http://user:password@localhost:3000/user/repo.git",
  86. "http://user:my:secure;password@localhost:3000/user/repo.git", "http://user:my%3Asecure%3Bpassword@localhost:3000/user/repo.git",
  87. "http://user:my@secure#password@localhost:3000/user/repo.git", "http://user:my%40secure%23password@localhost:3000/user/repo.git",
  88. }
  89. for i := 0; i < len(testCases); i += 2 {
  90. So(escapeMirrorCredentials(testCases[i]), ShouldEqual, testCases[i+1])
  91. }
  92. })
  93. }