|
| 1 | +package sync |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/gin-gonic/gin" |
| 10 | + "github.com/golang/mock/gomock" |
| 11 | + "github.com/hanjunlee/gitploy/ent" |
| 12 | + "github.com/hanjunlee/gitploy/internal/server/api/v1/sync/mock" |
| 13 | + "github.com/hanjunlee/gitploy/internal/server/global" |
| 14 | + "github.com/hanjunlee/gitploy/vo" |
| 15 | +) |
| 16 | + |
| 17 | +func TestSyncher_Sync(t *testing.T) { |
| 18 | + ctx := gomock.Any() |
| 19 | + |
| 20 | + t.Run("Synchronize with remote repositories", func(t *testing.T) { |
| 21 | + ctrl := gomock.NewController(t) |
| 22 | + m := mock.NewMockInteractor(ctrl) |
| 23 | + |
| 24 | + t.Log("List remote repositories") |
| 25 | + m. |
| 26 | + EXPECT(). |
| 27 | + ListRemoteRepos(ctx, gomock.AssignableToTypeOf(&ent.User{})). |
| 28 | + Return([]*vo.RemoteRepo{ |
| 29 | + { |
| 30 | + ID: "1", |
| 31 | + Namespace: "octocat", |
| 32 | + Name: "HelloWorld", |
| 33 | + }, |
| 34 | + { |
| 35 | + ID: "1", |
| 36 | + Namespace: "cat", |
| 37 | + Name: "GoodBye", |
| 38 | + }, |
| 39 | + }, nil) |
| 40 | + |
| 41 | + t.Log("Only octocat is trusted namespace.") |
| 42 | + m. |
| 43 | + EXPECT(). |
| 44 | + IsEntryOrg(ctx, gomock.Any()). |
| 45 | + DoAndReturn(func(ctx context.Context, namespace string) bool { |
| 46 | + return namespace == "octocat" |
| 47 | + }). |
| 48 | + AnyTimes() |
| 49 | + |
| 50 | + t.Log("Sync with HelloWorld only.") |
| 51 | + m. |
| 52 | + EXPECT(). |
| 53 | + SyncRemoteRepo(ctx, gomock.Any(), gomock.Eq(&vo.RemoteRepo{ |
| 54 | + ID: "1", |
| 55 | + Namespace: "octocat", |
| 56 | + Name: "HelloWorld", |
| 57 | + })). |
| 58 | + Return(nil) |
| 59 | + |
| 60 | + t.Log("Delete staled perms.") |
| 61 | + m. |
| 62 | + EXPECT(). |
| 63 | + DeletePermsOfUserLessThanUpdatedAt(ctx, gomock.Any(), gomock.Any()). |
| 64 | + Return(0, nil) |
| 65 | + |
| 66 | + gin.SetMode(gin.ReleaseMode) |
| 67 | + router := gin.New() |
| 68 | + |
| 69 | + s := NewSyncher(m) |
| 70 | + router.POST("/sync", func(c *gin.Context) { |
| 71 | + c.Set(global.KeyUser, &ent.User{}) |
| 72 | + }, s.Sync) |
| 73 | + |
| 74 | + req, _ := http.NewRequest("POST", "/sync", nil) |
| 75 | + w := httptest.NewRecorder() |
| 76 | + |
| 77 | + router.ServeHTTP(w, req) |
| 78 | + if w.Code != http.StatusOK { |
| 79 | + t.Fatalf("Sync = %v, wanted %v", w.Code, http.StatusOK) |
| 80 | + } |
| 81 | + }) |
| 82 | +} |
0 commit comments