From a8db7084487a3f9cd4a1e3c15a9b2cfb6cc63c7d Mon Sep 17 00:00:00 2001 From: nelsonic Date: Mon, 2 May 2022 23:12:35 +0100 Subject: [PATCH] add remote_repo_create/3 function with TestDouble fixes #9 --- README.md | 23 +++- config/test.exs | 5 +- lib/gogs.ex | 10 +- lib/gogs_helpers.ex | 2 +- lib/httpoison_mock.ex | 241 ++++++++++++++++++++++---------------- test/gogs_test.exs | 6 +- test/gogshelpers_test.exs | 8 ++ 7 files changed, 178 insertions(+), 117 deletions(-) create mode 100644 test/gogshelpers_test.exs diff --git a/README.md b/README.md index c145bb4..9c9fdf0 100644 --- a/README.md +++ b/README.md @@ -4,8 +4,8 @@ Interface with ***`gogs`*** from **`Elixir`**. -![GitHub Workflow Status](https://img.shields.io/github/workflow/status/dwyl/gogs/Elixir%20CI?label=build&style=flat-square) -[![codecov.io](https://img.shields.io/codecov/c/github/dwyl/gogs/master.svg?style=flat-square)](http://codecov.io/github/dwyl/auth?branch=main) +[![GitHub Workflow Status](https://img.shields.io/github/workflow/status/dwyl/gogs/Elixir%20CI?label=build&style=flat-square)](https://github.com/dwyl/gogs/actions/workflows/ci.yml) +[![codecov.io](https://img.shields.io/codecov/c/github/dwyl/gogs/master.svg?style=flat-square)](http://codecov.io/github/dwyl/gogs?branch=main) [![Hex.pm](https://img.shields.io/hexpm/v/gogs?color=brightgreen&style=flat-square)](https://hex.pm/packages/auth) [![Libraries.io dependency status](https://img.shields.io/librariesio/release/hex/gogs?logoColor=brightgreen&style=flat-square)](https://libraries.io/hex/gogs) [![docs](https://img.shields.io/badge/docs-maintained-brightgreen?style=flat-square)](https://hexdocs.pm/gogs/api-reference.html) @@ -17,9 +17,26 @@ Interface with ***`gogs`*** from **`Elixir`**. +## Why? + +We needed a way of _easily_ to interact +with our **`Gogs`** (GitHub Backup) **Server** +from our **`Elixir`** / **`Phoenix`** Applications. +This package is that interface. + +## What? + + +## Who? + + +## _How_? + + + ## Installation -If [available in Hex](https://hex.pm/docs/publish), +Install the package from [hex.pm](https://hex.pm/docs/publish), the package can be installed by adding `gogs` to your list of dependencies in `mix.exs`: diff --git a/config/test.exs b/config/test.exs index 8ae837d..f47f406 100644 --- a/config/test.exs +++ b/config/test.exs @@ -1,6 +1,5 @@ import Config +# set this to false if you want to hit the actual endpoints during development: config :gogs, - # client_id: "d6fca75c63daa014c187", - # client_secret: "8eeb143935d1a505692aaef856db9b4da8245f3c", - httpoison_mock: false + httpoison_mock: true diff --git a/lib/gogs.ex b/lib/gogs.ex index 3fdec99..1924914 100644 --- a/lib/gogs.ex +++ b/lib/gogs.ex @@ -73,8 +73,6 @@ defmodule Gogs do {:ok, str_key_map} = Jason.decode(body) {:ok, Useful.atomize_map_keys(str_key_map)} end - - # https://stackoverflow.com/questions/31990134 end @doc """ @@ -82,6 +80,7 @@ defmodule Gogs do """ @spec post(String.t(), map) :: {:ok, map} | {:error, any} def post(url, params \\ %{}) do + # IO.inspect(url, label: url) body = Jason.encode!(params) headers = [ {"Accept", "application/json"}, @@ -102,10 +101,11 @@ defmodule Gogs do """ def remote_repo_create(org_name, repo_name, private \\ false) do url = @api_base_url <> "org/#{org_name}/repos" - IO.inspect(url, label: "remote repo url") + # IO.inspect(url, label: "remote_repo_create url") params = %{ name: repo_name, - private: private + private: private, + description: repo_name } post(url, params) end @@ -115,7 +115,7 @@ defmodule Gogs do returns the path of the _local_ copy of the repository. """ def clone(git_repo_url) do - IO.inspect("git clone #{git_repo_url}") + # IO.inspect("git clone #{git_repo_url}") case Git.clone(git_repo_url) do {:ok, %Git.Repository{path: path}} -> # IO.inspect(path) diff --git a/lib/gogs_helpers.ex b/lib/gogs_helpers.ex index 5254263..d4f1863 100644 --- a/lib/gogs_helpers.ex +++ b/lib/gogs_helpers.ex @@ -3,7 +3,7 @@ defmodule GogsHelpers do `api_base_url/0` returns the `Gogs` Server REST API url for API requests. ## Examples - iex> Gogs.api_base_url() + iex> GogsHelpers.api_base_url() "https://gogs-server.fly.dev/api/v1/" """ def api_base_url do diff --git a/lib/httpoison_mock.ex b/lib/httpoison_mock.ex index 6aceebf..39ceae8 100644 --- a/lib/httpoison_mock.ex +++ b/lib/httpoison_mock.ex @@ -6,117 +6,156 @@ defmodule Gogs.HTTPoisonMock do and return things in the way we expect, so that we can check the pipeline in ElixirAuthGithub.github_auth """ + @remote_repo_create_response_body %{ + clone_url: "https://gogs-server.fly.dev/myorg/replacethis.git", + created_at: "0001-01-01T00:00:00Z", + default_branch: "", + description: "replacethis", + empty: false, + fork: false, + forks_count: 0, + full_name: "myorg/replacethis", + html_url: "https://gogs-server.fly.dev/myorg/replacethis", + id: 17, + mirror: false, + name: "test-repo450", + open_issues_count: 0, + owner: %{ + avatar_url: "https://gogs-server.fly.dev/avatars/2", + email: "", + full_name: "", + id: 2, + login: "myorg", + username: "myorg" + }, + parent: nil, + permissions: %{admin: true, pull: true, push: true}, + private: false, + size: 0, + ssh_url: "ssh://git@gogs-server.fly.dev:10022/myorg/replacethis.git", + stars_count: 0, + updated_at: "0001-01-01T00:00:00Z", + watchers_count: 0, + website: "" + } + + # make a valid response body for testing + def make_repo_create_post_response_body(repo_name) do + Map.merge(@remote_repo_create_response_body, %{ + clone_url: "https://gogs-server.fly.dev/myorg/#{repo_name}.git", + description: repo_name, + full_name: "myorg/#{repo_name}", + html_url: "https://gogs-server.fly.dev/myorg/#{repo_name}", + ssh_url: "ssh://git@gogs-server.fly.dev:10022/myorg/#{repo_name}.git", + }) + end @doc """ - `get/3` stubs the HTTPoison get! function when parameters match test vars. + `post/3` stubs the HTTPoison post function when parameters match test vars. + feel free refactor this if you can make it pretty. """ - @valid_body %{ - access_token: "12345", - login: "test_user", - name: "Testy McTestface", - email: "test@gmail.com", - avatar_url: "https://avatars3.githubusercontent.com/u/10835816", - id: "19" - } + def post("https://gogs-server.fly.dev/api/v1/org/myorg/repos", body, _headers) do + # IO.inspect("Gogs.HTTPoisonMock.post/3 called!") + body_map = Jason.decode!(body) |> Useful.atomize_map_keys() + response_body = + make_repo_create_post_response_body(body_map.name) + |> Jason.encode!() + {:ok, %{body: response_body}} + end - @body_email_nil %{ - access_token: "12345", - login: "test_user", - name: "Testy McTestface", - email: nil, - avatar_url: "https://avatars3.githubusercontent.com/u/10835816", - id: "28" - } + - @emails [ - %{ - "email" => "octocat@github.com", - "verified" => true, - "primary" => false, - "visibility" => "private" - }, - %{ - "email" => "private_email@gmail.com", - "verified" => true, - "primary" => true, - "visibility" => "private" - } - ] - - def get!(url, headers \\ [], options \\ []) - - def get!( - "https://api.github.com/user", - [ - {"User-Agent", "ElixirAuthGithub"}, - {"Authorization", "token 123"} - ], - _options - ) do - %{body: "{\"error\": \"test error\"}"} - end + # @body_email_nil %{ + # access_token: "12345", + # login: "test_user", + # name: "Testy McTestface", + # email: nil, + # avatar_url: "https://avatars3.githubusercontent.com/u/10835816", + # id: "28" + # } - def get!( - "https://api.github.com/user", - [ - {"User-Agent", "ElixirAuthGithub"}, - {"Authorization", "token 42"} - ], - _options - ) do - %{body: Jason.encode!(@body_email_nil)} - end + # @emails [ + # %{ + # "email" => "octocat@github.com", + # "verified" => true, + # "primary" => false, + # "visibility" => "private" + # }, + # %{ + # "email" => "private_email@gmail.com", + # "verified" => true, + # "primary" => true, + # "visibility" => "private" + # } + # ] - # user emails - def get!( - "https://api.github.com/user/emails", - [ - {"User-Agent", "ElixirAuthGithub"}, - {"Authorization", "token 42"} - ], - _options - ) do - %{body: Jason.encode!(@emails)} - end + # def get!(url, headers \\ [], options \\ []) - def get!(_url, _headers, _options) do - %{body: Jason.encode!(@valid_body)} - end + # def get!( + # "https://api.github.com/user", + # [ + # {"User-Agent", "ElixirAuthGithub"}, + # {"Authorization", "token 123"} + # ], + # _options + # ) do + # %{body: "{\"error\": \"test error\"}"} + # end - @doc """ - `post/3` stubs the HTTPoison post! function when parameters match test vars. - """ - def post!(url, body, headers \\ [], options \\ []) - - def post!( - "https://github.com/login/oauth/access_token?client_id=TEST_ID&client_secret=TEST_SECRET&code=1234", - _body, - _headers, - _options - ) do - %{body: "error=error"} - end + # def get!( + # "https://api.github.com/user", + # [ + # {"User-Agent", "ElixirAuthGithub"}, + # {"Authorization", "token 42"} + # ], + # _options + # ) do + # %{body: Jason.encode!(@body_email_nil)} + # end - def post!( - "https://github.com/login/oauth/access_token?client_id=TEST_ID&client_secret=TEST_SECRET&code=123", - _body, - _headers, - _options - ) do - %{body: "access_token=123"} - end + # # user emails + # def get!( + # "https://api.github.com/user/emails", + # [ + # {"User-Agent", "ElixirAuthGithub"}, + # {"Authorization", "token 42"} + # ], + # _options + # ) do + # %{body: Jason.encode!(@emails)} + # end - def post!( - "https://github.com/login/oauth/access_token?client_id=TEST_ID&client_secret=TEST_SECRET&code=42", - _body, - _headers, - _options - ) do - %{body: "access_token=42"} - end + # def get!(_url, _headers, _options) do + # %{body: Jason.encode!(@valid_body)} + # end - # for some reason GitHub's Post returns a URI encoded string - def post!(_url, _body, _headers, _options) do - %{body: URI.encode_query(@valid_body)} - end + # @doc """ + # `post/3` stubs the HTTPoison post! function when parameters match test vars. + # """ + # def post!(url, body, headers \\ [], options \\ []) + + # def post!( + # "https://github.com/login/oauth/access_token?client_id=TEST_ID&client_secret=TEST_SECRET&code=1234", + # _body, + # _headers, + # _options + # ) do + # %{body: "error=error"} + # end + + # def post!( + # "https://github.com/login/oauth/access_token?client_id=TEST_ID&client_secret=TEST_SECRET&code=123", + # _body, + # _headers, + # _options + # ) do + # %{body: "access_token=123"} + # end + + + + # # for some reason GitHub's Post returns a URI encoded string + # def post!(_url, _body, _headers, _options) do + # %{body: URI.encode_query(@valid_body)} + # end end diff --git a/test/gogs_test.exs b/test/gogs_test.exs index 2c1a428..13778a2 100644 --- a/test/gogs_test.exs +++ b/test/gogs_test.exs @@ -30,10 +30,8 @@ defmodule GogsTest do org_name = "myorg" repo_name = "test-repo" <> Integer.to_string(System.unique_integer([:positive])) response = Gogs.remote_repo_create(org_name, repo_name, false) - IO.inspect(response) - - - assert true == true + mock_response = Gogs.HTTPoisonMock.make_repo_create_post_response_body(repo_name) + assert response == {:ok, mock_response} end diff --git a/test/gogshelpers_test.exs b/test/gogshelpers_test.exs new file mode 100644 index 0000000..048f759 --- /dev/null +++ b/test/gogshelpers_test.exs @@ -0,0 +1,8 @@ +defmodule GogsHelpersTest do + use ExUnit.Case + doctest GogsHelpers + + test "GogsHelpers.api_base_url/0 returns the API URL for the Gogs Server" do + assert GogsHelpers.api_base_url() == "https://gogs-server.fly.dev/api/v1/" + end +end