Skip to content

Commit

Permalink
Create hex_repo functions (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
cgerling authored May 7, 2023
1 parent af45089 commit 92adcbf
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 3 deletions.
54 changes: 53 additions & 1 deletion src/hex_repo.erl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
get_names/1,
get_versions/1,
get_package/2,
get_tarball/3
get_tarball/3,
get_docs/3,
get_public_key/1
]).

%%====================================================================
Expand Down Expand Up @@ -101,6 +103,47 @@ get_tarball(Config, Name, Version) ->
Other
end.

%% @doc
%% Gets docs tarball from the repository.
%%
%% Examples:
%%
%% ```
%% > {ok, {200, _, Docs}} = hex_repo:get_tarball(hex_core:default_config(), <<"package1">>, <<"1.0.0">>),
%% > {ok, [{"index.html", <<"<!doctype>">>}, ...]} = hex_tarball:unpack_docs(Docs, memory)
%% '''
get_docs(Config, Name, Version) ->
ReqHeaders = make_headers(Config),

case get(Config, docs_url(Config, Name, Version), ReqHeaders) of
{ok, {200, RespHeaders, Docs}} ->
{ok, {200, RespHeaders, Docs}};

Other ->
Other
end.

%% @doc
%% Gets the public key from the repository.
%%
%% Examples:
%%
%% ```
%% > hex_repo:get_public_key(hex_core:default_config())
%% {ok, {200, _, PublicKey}}
%% '''
get_public_key(Config) ->
ReqHeaders = make_headers(Config),
URI = build_url(Config, <<"public_key">>),

case get(Config, URI, ReqHeaders) of
{ok, {200, RespHeaders, PublicKey}} ->
{ok, {200, RespHeaders, PublicKey}};

Other ->
Other
end.

%%====================================================================
%% Internal functions
%%====================================================================
Expand Down Expand Up @@ -162,6 +205,11 @@ tarball_url(Config, Name, Version) ->
Filename = tarball_filename(Name, Version),
build_url(Config, <<"tarballs/", Filename/binary>>).

%% @private
docs_url(Config, Name, Version) ->
Filename = docs_filename(Name, Version),
build_url(Config, <<"docs/", Filename/binary>>).

%% @private
build_url(#{repo_url := URI, repo_organization := Org}, Path) when is_binary(Org) ->
<<URI/binary, "/repos/", Org/binary, "/", Path/binary>>;
Expand All @@ -174,6 +222,10 @@ build_url(Config, Path) ->
tarball_filename(Name, Version) ->
<<Name/binary, "-", Version/binary, ".tar">>.

%% @private
docs_filename(Name, Version) ->
<<Name/binary, "-", Version/binary, ".tar.gz">>.

%% @private
make_headers(Config) ->
maps:fold(fun set_header/3, #{}, Config).
Expand Down
28 changes: 27 additions & 1 deletion test/hex_repo_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,15 @@ suite() ->
[{require, {ssl_certs, test_pub}}].

all() ->
[get_names_test, get_versions_test, get_package_test, get_tarball_test, repo_org_not_set].
[
get_names_test,
get_versions_test,
get_package_test,
get_tarball_test,
get_docs_test,
get_public_key_test,
repo_org_not_set
].

get_names_test(_Config) ->
{ok, {200, _, #{repository := <<"hexpm">>, packages := Packages}}} = hex_repo:get_names(?CONFIG),
Expand Down Expand Up @@ -50,6 +58,24 @@ get_tarball_test(_Config) ->
{ok, {403, _, _}} = hex_repo:get_tarball(?CONFIG, <<"ecto">>, <<"9.9.9">>),
ok.

get_docs_test(_Config) ->
{ok, {200, #{<<"etag">> := ETag}, Docs}} = hex_repo:get_docs(?CONFIG, <<"ecto">>, <<"1.0.0">>),
{ok, _} = hex_tarball:unpack_docs(Docs, memory),

{ok, {304, _, _}} = hex_repo:get_docs(maps:put(http_etag, ETag, ?CONFIG), <<"ecto">>, <<"1.0.0">>),

{ok, {403, _, _}} = hex_repo:get_docs(?CONFIG, <<"ecto">>, <<"9.9.9">>),
ok.

get_public_key_test(_Config) ->
{ok, {200, #{<<"etag">> := ETag}, PublicKey}} = hex_repo:get_public_key(?CONFIG),
[{'SubjectPublicKeyInfo', _, not_encrypted}] = public_key:pem_decode(PublicKey),

{ok, {304, _, _}} = hex_repo:get_public_key(maps:put(http_etag, ETag, ?CONFIG)),

{ok, {403, _, _}} = hex_repo:get_public_key(maps:put(repo_url, <<"https://repo.test/not_found">>, ?CONFIG)),
ok.

repo_org_not_set(_Config) ->
Config = maps:remove(repo_organization, ?CONFIG),
{ok, {200, _, #{repository := <<"hexpm">>, releases := Releases}}} = hex_repo:get_package(Config, <<"ecto">>),
Expand Down
15 changes: 14 additions & 1 deletion test/support/hex_http_test.erl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
-define(TEST_REPO_URL, "https://repo.test").
-define(TEST_API_URL, "https://api.test").
-define(PRIVATE_KEY, ct:get_config({ssl_certs, test_priv})).
-define(PUBLIC_KEY, ct:get_config({ssl_certs, test_pub}).
-define(PUBLIC_KEY, ct:get_config({ssl_certs, test_pub})).

%%====================================================================
%% API functions
Expand Down Expand Up @@ -92,6 +92,19 @@ fixture(get, <<?TEST_REPO_URL, "/tarballs/ecto-1.0.0.tar">>, _, _) ->
{ok, #{tarball := Tarball}} = hex_tarball:create(Metadata, []),
{ok, {200, Headers, Tarball}};

fixture(get, <<?TEST_REPO_URL, "/docs/ecto-1.0.0.tar.gz">>, _, _) ->
Headers = #{
<<"etag">> => <<"\"dummy\"">>
},
{ok, Docs} = hex_tarball:create_docs([]),
{ok, {200, Headers, Docs}};

fixture(get, <<?TEST_REPO_URL, "/public_key">>, _, _) ->
Headers = #{
<<"etag">> => <<"\"dummy\"">>
},
{ok, {200, Headers, ?PUBLIC_KEY}};

fixture(get, <<?TEST_REPO_URL, _/binary>>, _, _) ->
{ok, {403, #{}, <<"not found">>}};

Expand Down

0 comments on commit 92adcbf

Please sign in to comment.