@@ -198,3 +198,76 @@ async def test_database_access_permissions(
198198 cookies = {"ds_actor" : auth_response .cookies ["ds_actor" ]}
199199 databases = await ds .client .get ("/.json" , cookies = cookies )
200200 assert set (databases .json ()["databases" ].keys ()) == expected_databases
201+
202+
203+ @pytest .mark .asyncio
204+ async def test_github_enterprise_host (tmpdir , httpx_mock ):
205+ """Test that GitHub Enterprise host configuration works correctly"""
206+ # Mock GitHub Enterprise endpoints
207+ enterprise_host = "github.example.com"
208+
209+ httpx_mock .add_response (
210+ url = f"https://{ enterprise_host } /login/oauth/access_token" ,
211+ method = "POST" ,
212+ content = b"access_token=enterprise_access_token" ,
213+ )
214+
215+ httpx_mock .add_response (
216+ url = f"https://api.{ enterprise_host } /user" ,
217+ json = {
218+ "id" : 456 ,
219+ "name" : "Enterprise User" ,
220+ "login" : "enterpriseuser" ,
221+ "email" : "enterprise@example.com" ,
222+ },
223+ )
224+
225+ httpx_mock .add_response (
226+ url = re .compile (
227+ rf"^https://api\.{ re .escape (enterprise_host )} /orgs/enterprise-org/memberships/.*"
228+ ),
229+ json = {"state" : "active" , "role" : "member" },
230+ )
231+
232+ # Create Datasette instance with GitHub Enterprise configuration
233+ filepath = str (tmpdir / "test.db" )
234+ ds = Datasette (
235+ [filepath ],
236+ metadata = {
237+ "plugins" : {
238+ "datasette-auth-github" : {
239+ "client_id" : "enterprise_client_id" ,
240+ "client_secret" : "enterprise_client_secret" ,
241+ "host" : enterprise_host ,
242+ "load_orgs" : ["enterprise-org" ],
243+ }
244+ }
245+ },
246+ )
247+
248+ def create_tables (conn ):
249+ sqlite_utils .Database (conn )["example" ].insert ({"name" : "example" })
250+
251+ await ds .get_database ().execute_write_fn (create_tables , block = True )
252+
253+ # Test that the auth start URL uses the enterprise host
254+ response = await ds .client .get ("/-/github-auth-start" , follow_redirects = False )
255+ expected_url = f"https://{ enterprise_host } /login/oauth/authorize?scope=read:org&client_id=enterprise_client_id"
256+ assert expected_url == response .headers ["location" ]
257+
258+ # Test that the auth callback uses the enterprise host for API calls
259+ response = await ds .client .get (
260+ "/-/github-auth-callback?code=enterprise-code" ,
261+ follow_redirects = False ,
262+ )
263+
264+ actor = ds .unsign (response .cookies ["ds_actor" ], "actor" )["a" ]
265+ assert {
266+ "id" : "github:456" ,
267+ "display" : "enterpriseuser" ,
268+ "gh_id" : "456" ,
269+ "gh_name" : "Enterprise User" ,
270+ "gh_login" : "enterpriseuser" ,
271+ "gh_email" : "enterprise@example.com" ,
272+ "gh_orgs" : ["enterprise-org" ],
273+ }.items () <= actor .items ()
0 commit comments