@@ -6,6 +6,7 @@ defmodule OCI.PlugTest do
66 use ExUnit.Case , async: true
77 import Plug.Conn
88 import Plug.Test
9+ import OCI.PlugTest.Helpers
910
1011 alias OCI.Registry
1112
@@ -27,6 +28,12 @@ defmodule OCI.PlugTest do
2728 % { conn: conn }
2829 end
2930
31+ defp plug_opts ( ) do
32+ { :ok , tmp_path } = Temp . path ( )
33+ registry = Registry . init ( storage: OCI.Storage.Local . init ( path: tmp_path ) )
34+ OCI.Plug . init ( registry: registry )
35+ end
36+
3037 describe "GET /" do
3138 test "returns 200 for base endpoint" , % { conn: conn } do
3239 conn = conn |> get ( "/" )
@@ -56,112 +63,4 @@ defmodule OCI.PlugTest do
5663 assert String . starts_with? ( location , "/v2/big-org/big-team/big-project/blobs/uploads/" )
5764 end
5865 end
59-
60- defp get ( conn , path , query_params \\ nil ) do
61- conn
62- |> Plug.Adapters.Test.Conn . conn ( :get , path , query_params )
63- |> OCI.Plug . call ( conn . assigns . oci_opts )
64- end
65-
66- defp post ( conn , path , body \\ nil ) do
67- conn
68- |> Plug.Adapters.Test.Conn . conn ( :post , path , body )
69- |> OCI.Plug . call ( conn . assigns . oci_opts )
70- end
71-
72- defp put ( conn , path , body \\ nil ) do
73- conn
74- |> Plug.Adapters.Test.Conn . conn ( :put , path , body )
75- |> OCI.Plug . call ( conn . assigns . oci_opts )
76- end
77-
78- defp head ( conn , path ) do
79- conn
80- |> Plug.Adapters.Test.Conn . conn ( :head , path , nil )
81- |> OCI.Plug . call ( conn . assigns . oci_opts )
82- end
83-
84- defp delete ( conn , path ) do
85- conn
86- |> Plug.Adapters.Test.Conn . conn ( :delete , path , nil )
87- |> OCI.Plug . call ( conn . assigns . oci_opts )
88- end
89-
90- defp patch ( conn , path , body ) do
91- conn
92- |> Plug.Adapters.Test.Conn . conn ( :patch , path , body )
93- |> OCI.Plug . call ( conn . assigns . oci_opts )
94- end
95-
96- defp plug_opts ( ) do
97- { :ok , tmp_path } = Temp . path ( )
98- registry = Registry . init ( storage: OCI.Storage.Local . init ( path: tmp_path ) )
99- OCI.Plug . init ( registry: registry )
100- end
101-
102- defp digest ( data ) do
103- digest = OCI.Registry . sha256 ( data )
104- "sha256:#{ digest } "
105- end
106-
107- defp initiate_blob_upload ( % Plug.Conn { } = conn , repo ) do
108- conn =
109- conn
110- |> post ( "/#{ repo } /blobs/uploads/" )
111-
112- assert conn . status == 202
113- [ location ] = get_resp_header ( conn , "location" )
114- uuid = location |> String . split ( "/" ) |> List . last ( )
115- { conn , uuid }
116- end
117-
118- # Helper function to upload a chunk to a blob upload
119- defp upload_chunk ( % Plug.Conn { } = conn , repo , uuid , chunk , start_range \\ 0 ) do
120- end_range = start_range + String . length ( chunk ) - 1
121-
122- conn =
123- conn
124- |> put_req_header ( "content-type" , "application/octet-stream" )
125- |> put_req_header ( "content-range" , "#{ start_range } -#{ end_range } " )
126- |> put_req_header ( "content-length" , "#{ String . length ( chunk ) } " )
127- |> patch ( "/#{ repo } /blobs/uploads/#{ uuid } " , chunk )
128-
129- assert conn . status == 202
130- assert [ location ] = get_resp_header ( conn , "location" )
131- assert String . starts_with? ( location , "/v2/#{ repo } /blobs/uploads/" )
132- assert [ range ] = get_resp_header ( conn , "range" )
133- assert range == "#{ start_range } -#{ end_range } "
134- { conn , end_range }
135- end
136-
137- # Helper function to complete a blob upload
138- defp complete_blob_upload ( % Plug.Conn { } = conn , repo , uuid , digest , final_chunk \\ "" ) do
139- conn =
140- conn
141- |> put_req_header ( "content-type" , "application/octet-stream" )
142- |> put_req_header ( "content-length" , "#{ String . length ( final_chunk ) } " )
143- |> put ( "/#{ repo } /blobs/uploads/#{ uuid } ?digest=#{ digest } " , final_chunk )
144-
145- assert conn . status == 201
146- assert [ location ] = get_resp_header ( conn , "location" )
147- assert String . starts_with? ( location , "/v2/#{ repo } /blobs/" )
148- assert String . ends_with? ( location , digest )
149- conn
150- end
151-
152- defp basic_auth ( conn , username , password ) do
153- put_req_header ( conn , "authorization" , "Basic #{ Base . encode64 ( "#{ username } :#{ password } " ) } " )
154- end
155-
156- defp override_registry_setting ( conn , setting , value ) do
157- registry = % { conn . assigns . oci_opts . registry | setting => value }
158-
159- % {
160- conn
161- | assigns: % {
162- conn . assigns
163- | oci_opts: % { conn . assigns . oci_opts | registry: registry }
164- }
165- }
166- end
16766end
0 commit comments