@@ -5,6 +5,7 @@ import { actionsForEventCategories } from "../r2/helpers";
55import { endEventLoop } from "./helpers/end-event-loop" ;
66import { mockAccountId , mockApiToken } from "./helpers/mock-account-id" ;
77import { mockConsoleMethods } from "./helpers/mock-console" ;
8+ import { mockConfirm } from "./helpers/mock-dialogs" ;
89import { useMockIsTTY } from "./helpers/mock-istty" ;
910import { createFetchResult , msw , mswR2handlers } from "./helpers/msw" ;
1011import { runInTempDir } from "./helpers/run-in-tmp" ;
@@ -97,6 +98,7 @@ describe("r2", () => {
9798 wrangler r2 bucket delete <name> Delete an R2 bucket
9899 wrangler r2 bucket sippy Manage Sippy incremental migration on an R2 bucket
99100 wrangler r2 bucket notification Manage event notification rules for an R2 bucket
101+ wrangler r2 bucket dev-url Manage public access via the r2.dev URL for an R2 bucket
100102
101103 GLOBAL FLAGS
102104 -j, --experimental-json-config Experimental: support wrangler.json [boolean]
@@ -131,6 +133,7 @@ describe("r2", () => {
131133 wrangler r2 bucket delete <name> Delete an R2 bucket
132134 wrangler r2 bucket sippy Manage Sippy incremental migration on an R2 bucket
133135 wrangler r2 bucket notification Manage event notification rules for an R2 bucket
136+ wrangler r2 bucket dev-url Manage public access via the r2.dev URL for an R2 bucket
134137
135138 GLOBAL FLAGS
136139 -j, --experimental-json-config Experimental: support wrangler.json [boolean]
@@ -1467,6 +1470,139 @@ describe("r2", () => {
14671470 } ) ;
14681471 } ) ;
14691472 } ) ;
1473+
1474+ describe ( "dev-url" , ( ) => {
1475+ const { setIsTTY } = useMockIsTTY ( ) ;
1476+ mockAccountId ( ) ;
1477+ mockApiToken ( ) ;
1478+ describe ( "get" , ( ) => {
1479+ it ( "should retrieve the r2.dev URL of a bucket when public access is enabled" , async ( ) => {
1480+ const bucketName = "my-bucket" ;
1481+ const domainInfo = {
1482+ bucketId : "bucket-id-123" ,
1483+ domain : "pub-bucket-id-123.r2.dev" ,
1484+ enabled : true ,
1485+ } ;
1486+ msw . use (
1487+ http . get (
1488+ "*/accounts/:accountId/r2/buckets/:bucketName/domains/managed" ,
1489+ async ( { params } ) => {
1490+ const { accountId, bucketName : bucketParam } = params ;
1491+ expect ( accountId ) . toEqual ( "some-account-id" ) ;
1492+ expect ( bucketParam ) . toEqual ( bucketName ) ;
1493+ return HttpResponse . json ( createFetchResult ( { ...domainInfo } ) ) ;
1494+ } ,
1495+ { once : true }
1496+ )
1497+ ) ;
1498+ await runWrangler ( `r2 bucket dev-url get ${ bucketName } ` ) ;
1499+ expect ( std . out ) . toMatchInlineSnapshot ( `
1500+ "Public access is enabled at 'https://pub-bucket-id-123.r2.dev'."
1501+ ` ) ;
1502+ } ) ;
1503+
1504+ it ( "should show that public access is disabled when it is disabled" , async ( ) => {
1505+ const bucketName = "my-bucket" ;
1506+ const domainInfo = {
1507+ bucketId : "bucket-id-123" ,
1508+ domain : "pub-bucket-id-123.r2.dev" ,
1509+ enabled : false ,
1510+ } ;
1511+ msw . use (
1512+ http . get (
1513+ "*/accounts/:accountId/r2/buckets/:bucketName/domains/managed" ,
1514+ async ( { params } ) => {
1515+ const { accountId, bucketName : bucketParam } = params ;
1516+ expect ( accountId ) . toEqual ( "some-account-id" ) ;
1517+ expect ( bucketParam ) . toEqual ( bucketName ) ;
1518+ return HttpResponse . json ( createFetchResult ( { ...domainInfo } ) ) ;
1519+ } ,
1520+ { once : true }
1521+ )
1522+ ) ;
1523+ await runWrangler ( `r2 bucket dev-url get ${ bucketName } ` ) ;
1524+ expect ( std . out ) . toMatchInlineSnapshot ( `
1525+ "Public access via the r2.dev URL is disabled."
1526+ ` ) ;
1527+ } ) ;
1528+ } ) ;
1529+
1530+ describe ( "enable" , ( ) => {
1531+ it ( "should enable public access" , async ( ) => {
1532+ const bucketName = "my-bucket" ;
1533+ const domainInfo = {
1534+ bucketId : "bucket-id-123" ,
1535+ domain : "pub-bucket-id-123.r2.dev" ,
1536+ enabled : true ,
1537+ } ;
1538+
1539+ setIsTTY ( true ) ;
1540+ mockConfirm ( {
1541+ text :
1542+ `Are you sure you enable public access for bucket '${ bucketName } '? ` +
1543+ `The contents of your bucket will be made publicly available at its r2.dev URL` ,
1544+ result : true ,
1545+ } ) ;
1546+ msw . use (
1547+ http . put (
1548+ "*/accounts/:accountId/r2/buckets/:bucketName/domains/managed" ,
1549+ async ( { request, params } ) => {
1550+ const { accountId, bucketName : bucketParam } = params ;
1551+ expect ( accountId ) . toEqual ( "some-account-id" ) ;
1552+ expect ( bucketParam ) . toEqual ( bucketName ) ;
1553+ const requestBody = await request . json ( ) ;
1554+ expect ( requestBody ) . toEqual ( { enabled : true } ) ;
1555+ return HttpResponse . json ( createFetchResult ( { ...domainInfo } ) ) ;
1556+ } ,
1557+ { once : true }
1558+ )
1559+ ) ;
1560+ await runWrangler ( `r2 bucket dev-url enable ${ bucketName } ` ) ;
1561+ expect ( std . out ) . toMatchInlineSnapshot ( `
1562+ "Enabling public access for bucket 'my-bucket'...
1563+ ✨ Public access enabled at 'https://pub-bucket-id-123.r2.dev'."
1564+ ` ) ;
1565+ } ) ;
1566+ } ) ;
1567+
1568+ describe ( "disable" , ( ) => {
1569+ it ( "should disable public access" , async ( ) => {
1570+ const bucketName = "my-bucket" ;
1571+ const domainInfo = {
1572+ bucketId : "bucket-id-123" ,
1573+ domain : "pub-bucket-id-123.r2.dev" ,
1574+ enabled : false ,
1575+ } ;
1576+
1577+ setIsTTY ( true ) ;
1578+ mockConfirm ( {
1579+ text :
1580+ `Are you sure you disable public access for bucket '${ bucketName } '? ` +
1581+ `The contents of your bucket will no longer be publicly available at its r2.dev URL` ,
1582+ result : true ,
1583+ } ) ;
1584+ msw . use (
1585+ http . put (
1586+ "*/accounts/:accountId/r2/buckets/:bucketName/domains/managed" ,
1587+ async ( { request, params } ) => {
1588+ const { accountId, bucketName : bucketParam } = params ;
1589+ expect ( accountId ) . toEqual ( "some-account-id" ) ;
1590+ expect ( bucketParam ) . toEqual ( bucketName ) ;
1591+ const requestBody = await request . json ( ) ;
1592+ expect ( requestBody ) . toEqual ( { enabled : false } ) ;
1593+ return HttpResponse . json ( createFetchResult ( { ...domainInfo } ) ) ;
1594+ } ,
1595+ { once : true }
1596+ )
1597+ ) ;
1598+ await runWrangler ( `r2 bucket dev-url disable ${ bucketName } ` ) ;
1599+ expect ( std . out ) . toMatchInlineSnapshot ( `
1600+ "Disabling public access for bucket 'my-bucket'...
1601+ Public access disabled at 'https://pub-bucket-id-123.r2.dev'."
1602+ ` ) ;
1603+ } ) ;
1604+ } ) ;
1605+ } ) ;
14701606 } ) ;
14711607
14721608 describe ( "r2 object" , ( ) => {
0 commit comments