Exemplo n.º 1
0
        public void Create_DataCorrect_RepoCreated()
        {
            var user    = UserFaker.Create();
            var browser = new Browser(new DefaultNancyBootstrapper());

            var repoTitle = "testRepo" + Rand.SmallInt();

            var result = browser.Post("/api/v1/repository/create", with => {
                with.HttpRequest();
                with.Query("api_token", Jwt.FromUserId(user.id));
                with.Query("title", repoTitle);
                with.Query("repo_url", "randomUrl" + Rand.SmallInt());
                with.Query("service_type", RepoServiceType.GitHub.ToString());
            }).Result;

            Assert.AreEqual(HttpStatusCode.Created, result.StatusCode);

            var json = JObject.Parse(result.Body.AsString());

            var guid = json["data"]["repository"].Value <string>("guid") ?? "";

            var createdRepository = RepoRepository.FindByGuid(guid);

            Assert.NotNull(createdRepository);
            Assert.AreEqual(repoTitle, createdRepository.title);
            Assert.AreEqual(
                user.guid, json["data"]["repository"]["creator"].Value <string>("guid") ?? ""
                );
        }
Exemplo n.º 2
0
        public RepoController()
        {
            Get("/api/v1/repository/get", _ => {
                var errors = ValidationProcessor.Process(Request, new IValidatorRule[] {
                    new ExistsInTable("repo_guid", "repositories", "guid"),
                });
                if (errors.Count > 0)
                {
                    return(HttpResponse.Errors(errors));
                }

                return(HttpResponse.Item("repository", new RepoTransformer().Transform(
                                             RepoRepository.FindByGuid((string)Request.Query["repo_guid"])
                                             )));
            });

            Get("/api/v1/repository/meta/get", _ => {
                var errors = ValidationProcessor.Process(Request, new IValidatorRule[] {
                    new ExistsInTable("repo_guid", "repositories", "guid"),
                });
                if (errors.Count > 0)
                {
                    return(HttpResponse.Errors(errors));
                }

                var sponsorLinks = new JObject();

                var repo = RepoRepository.FindByGuid((string)Request.Query["repo_guid"]);

                if (repo.service_type == RepoServiceType.GitHub)
                {
                    try {
                        var splitUrl = repo.repo_url.Split("/");
                        var response = new HttpClient().GetAsync(
                            $"https://raw.githubusercontent.com/{splitUrl[3]}/{splitUrl[4]}/master/.github/FUNDING.yml"
                            ).Result.Content.ReadAsStringAsync().Result;
                        var yamlObject = (Dictionary <object, object>) new DeserializerBuilder().Build()
                                         .Deserialize(new StringReader(response));
                        sponsorLinks["github"]          = yamlObject["github"]?.ToString();
                        sponsorLinks["patreon"]         = yamlObject["patreon"]?.ToString();
                        sponsorLinks["open_collective"] = yamlObject["open_collective"]?.ToString();
                    }
                    catch (Exception e) {
                        SentrySdk.CaptureException(e);
                    }
                }

                return(HttpResponse.Data(new JObject()
                {
                    ["repository"] = new RepoTransformer().Transform(repo)
                                     ["meta"] = new JObject()
                    {
                        ["sponsor_links"] = sponsorLinks
                    }
                }));
            });
        }
Exemplo n.º 3
0
        public void Delete_DataCorrect_RepositoryDeleted()
        {
            var browser = new Browser(new DefaultNancyBootstrapper());

            var repo = RepoFaker.Create();

            var result = browser.Delete("/api/v1/repository/delete", with => {
                with.HttpRequest();
                with.Query("api_token", Jwt.FromUserId(UserFaker.Create().id));
                with.Query("repo_guid", repo.guid);
            }).Result;

            Assert.AreEqual(HttpStatusCode.OK, result.StatusCode);

            var json = JObject.Parse(result.Body.AsString());

            Assert.AreEqual(repo.guid, json["data"]["repository"].Value <string>("guid"));

            Assert.IsNull(RepoRepository.FindByGuid(repo.guid));
        }
Exemplo n.º 4
0
        public RepoCrudController()
        {
            Post("/api/v1/repository/create", _ => {
                var errors = ValidationProcessor.Process(Request, new IValidatorRule[] {
                    new ShouldHaveParameters(new[] { "title", "repo_url", "service_type" }),
                    new ShouldBeCorrectEnumValue("service_type", typeof(RepoServiceType)),
                    new ShouldNotExistInTable("repo_url", "repositories")
                });
                if (errors.Count > 0)
                {
                    return(HttpResponse.Errors(errors));
                }

                var me = UserRepository.Find(CurrentRequest.UserId);

                var repository = RepoRepository.CreateAndGet(
                    me, (string)Request.Query["title"], (string)Request.Query["repo_url"],
                    (RepoServiceType)GetRequestEnum("service_type", typeof(RepoServiceType))
                    );

                return(HttpResponse.Item(
                           "repository", new RepoTransformer().Transform(repository), HttpStatusCode.Created
                           ));
            });

            Patch("/api/v1/repository/edit", _ => {
                var rules = new List <IValidatorRule>()
                {
                    new ExistsInTable("repo_guid", "repositories", "guid")
                };

                if (Request.Query["repo_url"])
                {
                    rules.Add(new ShouldNotExistInTable("repo_url", "repositories"));
                }

                var errors = ValidationProcessor.Process(Request, rules);
                if (errors.Count > 0)
                {
                    return(HttpResponse.Errors(errors));
                }

                var repo = RepoRepository.FindByGuid((string)Request.Query["repo_guid"]);

                repo = RepoRepository.UpdateAndRefresh(repo, new JObject()
                {
                    ["title"]    = (string)Request.Query["title"],
                    ["repo_url"] = (string)Request.Query["repo_url"]
                });

                return(HttpResponse.Item("repository", new RepoTransformer().Transform(repo)));
            });

            Delete("/api/v1/repository/delete", _ => {
                var errors = ValidationProcessor.Process(Request, new IValidatorRule[] {
                    new ExistsInTable("repo_guid", "repositories", "guid"),
                });
                if (errors.Count > 0)
                {
                    return(HttpResponse.Errors(errors));
                }

                var repo = RepoRepository.FindByGuid((string)Request.Query["repo_guid"]);
                repo.Delete();

                return(HttpResponse.Item("repository", new RepoTransformer().Transform(repo)));
            });
        }