-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #624 from systemli/Add-Support-for-Bluesky
✨Add Support for Bluesky
- Loading branch information
Showing
12 changed files
with
415 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query' | ||
import { render, screen } from '@testing-library/react' | ||
import sign from 'jwt-encode' | ||
import BlueskyCard from './BlueskyCard' | ||
import { MemoryRouter } from 'react-router' | ||
import { AuthProvider } from '../../contexts/AuthContext' | ||
import { Ticker } from '../../api/Ticker' | ||
import userEvent from '@testing-library/user-event' | ||
|
||
const token = sign({ id: 1, email: '[email protected]', roles: ['user'], exp: new Date().getTime() / 1000 + 600 }, 'secret') | ||
|
||
describe('BlueSkyCard', () => { | ||
beforeAll(() => { | ||
localStorage.setItem('token', token) | ||
}) | ||
|
||
const ticker = ({ active, connected, handle = '', appKey = '' }: { active: boolean; connected: boolean; handle?: string; appKey?: string }) => { | ||
return { | ||
id: 1, | ||
bluesky: { | ||
active: active, | ||
connected: connected, | ||
handle: handle, | ||
appKey: appKey, | ||
}, | ||
} as Ticker | ||
} | ||
|
||
beforeEach(() => { | ||
fetchMock.resetMocks() | ||
}) | ||
|
||
function setup(ticker: Ticker) { | ||
const client = new QueryClient({ | ||
defaultOptions: { | ||
queries: { | ||
retry: false, | ||
}, | ||
}, | ||
}) | ||
return render( | ||
<QueryClientProvider client={client}> | ||
<MemoryRouter> | ||
<AuthProvider> | ||
<BlueskyCard ticker={ticker} /> | ||
</AuthProvider> | ||
</MemoryRouter> | ||
</QueryClientProvider> | ||
) | ||
} | ||
|
||
it('should render the component', () => { | ||
setup(ticker({ active: false, connected: false })) | ||
|
||
expect(screen.getByText('Bluesky')).toBeInTheDocument() | ||
expect(screen.getByText('You are not connected with Bluesky.')).toBeInTheDocument() | ||
expect(screen.getByRole('button', { name: 'Configure' })).toBeInTheDocument() | ||
}) | ||
|
||
it('should render the component when connected and active', async () => { | ||
setup(ticker({ active: true, connected: true, handle: 'handle.bsky.social' })) | ||
|
||
expect(screen.getByText('Bluesky')).toBeInTheDocument() | ||
expect(screen.getByText('You are connected with Bluesky.')).toBeInTheDocument() | ||
expect(screen.getByRole('link', { name: 'handle.bsky.social' })).toBeInTheDocument() | ||
expect(screen.getByRole('button', { name: 'Configure' })).toBeInTheDocument() | ||
expect(screen.getByRole('button', { name: 'Disconnect' })).toBeInTheDocument() | ||
expect(screen.getByRole('button', { name: 'Disable' })).toBeInTheDocument() | ||
|
||
fetchMock.mockResponseOnce(JSON.stringify({ status: 'success' })) | ||
|
||
await userEvent.click(screen.getByRole('button', { name: 'Disable' })) | ||
|
||
expect(fetchMock).toHaveBeenCalledTimes(1) | ||
expect(fetchMock).toHaveBeenCalledWith('http://localhost:8080/v1/admin/tickers/1/bluesky', { | ||
body: JSON.stringify({ active: false }), | ||
headers: { | ||
Accept: 'application/json', | ||
'Content-Type': 'application/json', | ||
Authorization: 'Bearer ' + token, | ||
}, | ||
method: 'put', | ||
}) | ||
|
||
fetchMock.mockResponseOnce(JSON.stringify({ status: 'success' })) | ||
|
||
await userEvent.click(screen.getByRole('button', { name: 'Disconnect' })) | ||
|
||
expect(fetchMock).toHaveBeenCalledTimes(2) | ||
expect(fetchMock).toHaveBeenCalledWith('http://localhost:8080/v1/admin/tickers/1/bluesky', { | ||
headers: { | ||
Accept: 'application/json', | ||
'Content-Type': 'application/json', | ||
Authorization: 'Bearer ' + token, | ||
}, | ||
method: 'delete', | ||
}) | ||
|
||
await userEvent.click(screen.getByRole('button', { name: 'Configure' })) | ||
|
||
expect(screen.getByRole('dialog')).toBeInTheDocument() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { Box, Button, Card, CardActions, CardContent, Divider, Link, Stack, Typography } from '@mui/material' | ||
import { Ticker, useTickerApi } from '../../api/Ticker' | ||
import useAuth from '../../contexts/useAuth' | ||
import { FC, useCallback, useState } from 'react' | ||
import { useQueryClient } from '@tanstack/react-query' | ||
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' | ||
import { faBluesky } from '@fortawesome/free-brands-svg-icons' | ||
import { faGear, faPause, faPlay, faTrash } from '@fortawesome/free-solid-svg-icons' | ||
import BlueskyModalForm from './BlueskyModalForm' | ||
|
||
interface Props { | ||
ticker: Ticker | ||
} | ||
|
||
const BlueskyCard: FC<Props> = ({ ticker }) => { | ||
const { token } = useAuth() | ||
const { deleteTickerBluesky, putTickerBluesky } = useTickerApi(token) | ||
const [open, setOpen] = useState<boolean>(false) | ||
|
||
const queryClient = useQueryClient() | ||
|
||
const bluesky = ticker.bluesky | ||
|
||
const handleDisconnect = useCallback(() => { | ||
deleteTickerBluesky(ticker).finally(() => { | ||
queryClient.invalidateQueries({ queryKey: ['ticker', ticker.id] }) | ||
}) | ||
}, [deleteTickerBluesky, queryClient, ticker]) | ||
|
||
const handleToggle = useCallback(() => { | ||
putTickerBluesky({ active: !bluesky.active }, ticker).finally(() => { | ||
queryClient.invalidateQueries({ queryKey: ['ticker', ticker.id] }) | ||
}) | ||
}, [bluesky.active, putTickerBluesky, queryClient, ticker]) | ||
|
||
const profileLink = ( | ||
<Link href={'https://bsky.app/profile/' + bluesky.handle} rel="noreferrer" target="_blank"> | ||
{bluesky.handle} | ||
</Link> | ||
) | ||
|
||
return ( | ||
<Card> | ||
<CardContent> | ||
<Stack alignItems="center" direction="row" justifyContent="space-between"> | ||
<Typography component="h5" variant="h5"> | ||
<FontAwesomeIcon icon={faBluesky} /> Bluesky | ||
</Typography> | ||
<Button onClick={() => setOpen(true)} size="small" startIcon={<FontAwesomeIcon icon={faGear} />}> | ||
Configure | ||
</Button> | ||
</Stack> | ||
</CardContent> | ||
<Divider variant="middle" /> | ||
<CardContent> | ||
{bluesky.connected ? ( | ||
<Box> | ||
<Typography variant="body2">You are connected with Bluesky.</Typography> | ||
<Typography variant="body2">Your Profile: {profileLink}</Typography> | ||
</Box> | ||
) : ( | ||
<Box> | ||
<Typography variant="body2">You are not connected with Bluesky.</Typography> | ||
<Typography variant="body2">New messages will not be published to your account and old messages can not be deleted anymore.</Typography> | ||
</Box> | ||
)} | ||
</CardContent> | ||
{bluesky.connected ? ( | ||
<CardActions> | ||
{bluesky.active ? ( | ||
<Button onClick={handleToggle} size="small" startIcon={<FontAwesomeIcon icon={faPause} />}> | ||
Disable | ||
</Button> | ||
) : ( | ||
<Button onClick={handleToggle} size="small" startIcon={<FontAwesomeIcon icon={faPlay} />}> | ||
Enable | ||
</Button> | ||
)} | ||
<Button onClick={handleDisconnect} size="small" startIcon={<FontAwesomeIcon icon={faTrash} />}> | ||
Disconnect | ||
</Button> | ||
</CardActions> | ||
) : null} | ||
<BlueskyModalForm open={open} onClose={() => setOpen(false)} ticker={ticker} /> | ||
</Card> | ||
) | ||
} | ||
|
||
export default BlueskyCard |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import sign from 'jwt-encode' | ||
import { Ticker } from '../../api/Ticker' | ||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query' | ||
import { render, screen } from '@testing-library/react' | ||
import { MemoryRouter } from 'react-router' | ||
import { AuthProvider } from '../../contexts/AuthContext' | ||
import BlueskyForm from './BlueskyForm' | ||
import userEvent from '@testing-library/user-event' | ||
|
||
const token = sign({ id: 1, email: '[email protected]', roles: ['user'], exp: new Date().getTime() / 1000 + 600 }, 'secret') | ||
|
||
describe('BlueskyForm', () => { | ||
beforeAll(() => { | ||
localStorage.setItem('token', token) | ||
}) | ||
|
||
const ticker = ({ active, connected, handle = '', appKey = '' }: { active: boolean; connected: boolean; handle?: string; appKey?: string }) => { | ||
return { | ||
id: 1, | ||
bluesky: { | ||
active: active, | ||
connected: connected, | ||
handle: handle, | ||
appKey: appKey, | ||
}, | ||
} as Ticker | ||
} | ||
|
||
const callback = vi.fn() | ||
|
||
beforeEach(() => { | ||
fetchMock.resetMocks() | ||
}) | ||
|
||
function setup(ticker: Ticker) { | ||
const client = new QueryClient({ | ||
defaultOptions: { | ||
queries: { | ||
retry: false, | ||
}, | ||
}, | ||
}) | ||
return render( | ||
<QueryClientProvider client={client}> | ||
<MemoryRouter> | ||
<AuthProvider> | ||
<div> | ||
<BlueskyForm callback={callback} ticker={ticker} /> | ||
<input name="Submit" type="submit" value="Submit" form="configureBluesky" /> | ||
</div> | ||
</AuthProvider> | ||
</MemoryRouter> | ||
</QueryClientProvider> | ||
) | ||
} | ||
|
||
it('should render the component', async () => { | ||
setup(ticker({ active: false, connected: false })) | ||
|
||
expect(screen.getByText('You need to create a application password in Bluesky.')).toBeInTheDocument() | ||
expect(screen.getByRole('checkbox', { name: 'Active' })).toBeInTheDocument() | ||
expect(screen.getByRole('textbox', { name: 'Handle' })).toBeInTheDocument() | ||
expect(screen.getByLabelText('Application Password *')).toBeInTheDocument() | ||
expect(screen.getByRole('button', { name: 'Submit' })).toBeInTheDocument() | ||
|
||
await userEvent.click(screen.getByRole('checkbox', { name: 'Active' })) | ||
await userEvent.type(screen.getByRole('textbox', { name: 'Handle' }), 'handle.bsky.social') | ||
await userEvent.type(screen.getByLabelText('Application Password *'), 'password') | ||
|
||
fetchMock.mockResponseOnce(JSON.stringify({ status: 'success' })) | ||
|
||
await userEvent.click(screen.getByRole('button', { name: 'Submit' })) | ||
|
||
expect(callback).toHaveBeenCalledTimes(1) | ||
expect(fetchMock).toHaveBeenCalledTimes(1) | ||
expect(fetchMock).toHaveBeenCalledWith('http://localhost:8080/v1/admin/tickers/1/bluesky', { | ||
body: '{"active":true,"handle":"handle.bsky.social","appKey":"password"}', | ||
headers: { | ||
Accept: 'application/json', | ||
Authorization: 'Bearer ' + token, | ||
'Content-Type': 'application/json', | ||
}, | ||
method: 'put', | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.