Skip to content

Commit

Permalink
Merge pull request #958 from stakwork/setup/complete-e2e
Browse files Browse the repository at this point in the history
Setup/complete e2e
  • Loading branch information
Rassl authored Feb 26, 2024
2 parents f9519f0 + 60374c7 commit 3e47acc
Show file tree
Hide file tree
Showing 19 changed files with 331 additions and 61 deletions.
56 changes: 53 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,53 @@ jobs:
cypress-run:
runs-on: ubuntu-latest
steps:
- name: Enable docker.host.internal for Ubuntu
run: |
pwd && sudo bash -c 'echo "172.17.0.1 host.docker.internal" >> /etc/hosts'
- name: Checkout
uses: actions/checkout@v4

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version-file: '.nvmrc'
cache: 'yarn'
node-version-file: ".nvmrc"
cache: "yarn"
- name: Clone Stack
run: |
git clone -b feat/second-brain-compose https://github.com/stakwork/sphinx-stack.git stack
- name: Give Permissions to Sphinx Nav Fiber
run: chmod 777 -R relay

- name: Give Permissions to Stack
working-directory: ./stack
run: |
chmod 777 ./bitcoind;
chmod 777 -R ./relay;
chmod 777 -R ./lnd;
chmod 777 -R ./proxy;
chmod 777 -R ./cln;
- name: Check for NODES
uses: nick-fields/retry@v2
with:
timeout_minutes: 10
max_attempts: 3
command: |
GITACTION_ENV=gitactionenv docker-compose -f ./stack/alts/navfiber.yml --project-dir ./stack up -d;
sleep 240;
docker ps
docker logs meme.sphinx
docker logs dave.sphinx
docker wait stack_relaysetup_1
cat stack/relay/NODES.json;
- name: Copy Node.json
uses: canastro/copy-file-action@master
with:
source: "stack/relay/NODES.json"
target: "relay/nodes.json"

- name: Install
run: yarn --immutable
Expand All @@ -26,7 +65,7 @@ jobs:
install-command: yarn --immutable
browser: chrome
start: yarn run start-e2e
wait-on: 'http://localhost:3000' # Waits for above
wait-on: "http://localhost:3000" # Waits for above
wait-on-timeout: 120 # Waits for 2 minutes
# Records to Cypress Dashboard
record: true
Expand All @@ -38,5 +77,16 @@ jobs:
run: yarn run cy-comp
continue-on-error: false

- name: Upload Cypress logs
if: failure()
uses: actions/upload-artifact@v2
with:
name: cypress-logs
path: cypress/videos

- name: Check the coverage value
run: yarn test-coverage

- name: Stop Stack
working-directory: ./stack
run: docker-compose down
27 changes: 27 additions & 0 deletions cypress/e2e/admin/signin.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
describe('Admin Login', () => {
it('Admin uses the enable function', () => {
cy.visit('http://localhost:3000', {
onBeforeLoad(win) {
win['CYPRESS_USER'] = 'alice'
},
})
let title = `Testing NavFiber`
cy.wait(30000)
cy.get('div[data-testid="settings-modal"]').click()
// cy.get('[data-testid="setting-label"]').should('have.text', 'Settings')
cy.get('[data-testid="setting-label"]').should('have.text', 'Admin Settings')
// .invoke('text')
// .then((value) => {
// console.log(value)
// })
cy.get('#cy-about-title-id').click()
cy.get('#cy-about-title-id').type('{selectAll}')
cy.get('#cy-about-title-id').type(title)

cy.get('#add-node-submit-cta').click()
cy.wait(200)
cy.get('div[data-testid="close-modal"]').click()

cy.get('.title').should('have.text', title)
})
})
26 changes: 26 additions & 0 deletions cypress/support/e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,36 @@
// ***********************************************************

// Import commands.js using ES2015 syntax:
import nodes from '../../relay/nodes.json'
import './commands'

// Alternatively you can use CommonJS syntax:
// require('./commands')

// coverage
import '@cypress/code-coverage/support'

// Add alice as the Second Brain tribe admin
async function setAdmin() {
let user
for (let i = 0; i < nodes.length; i++) {
if (nodes[i].alias === 'alice') {
user = nodes[i]
break
}
}
try {
await fetch('http://localhost:8444/api/set_admin_pubkey', {
method: 'POST',
headers: { 'x-admin-token': 'navfiber_e2e_testing', 'Content-Type': 'application/json' },
body: JSON.stringify({
pubkey: user.pubkey,
name: user.alias,
}),
})
} catch (error) {
console.error(JSON.stringify(error))
}
}

setAdmin()
4 changes: 3 additions & 1 deletion cypress/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
"compilerOptions": {
"target": "es5",
"lib": ["es5", "dom"],
"types": ["cypress", "node"]
"types": ["cypress", "node"],
"resolveJsonModule": true,
"esModuleInterop": true
},
"include": ["**/*.ts"]
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
},
"scripts": {
"start": "vite --port 3000",
"start-e2e": "VITE_APP_IS_E2E=true vite --port 3000",
"start-e2e": "VITE_APP_IS_E2E=true VITE_APP_API_URL=http://localhost:8444/api vite --host --port 3000",
"dev-tribe": "vite --port 3004",
"build": "tsc && vite build",
"build-docker": "tsc && vite build",
Expand Down
1 change: 1 addition & 0 deletions relay/nodes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
2 changes: 1 addition & 1 deletion src/components/App/SideBar/Show/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { useMemo, useState } from 'react'
import styled from 'styled-components'
import { useGraphData } from '~/components/DataRetriever'
import { Avatar } from '~/components/common/Avatar'
import { Flex } from '~/components/common/Flex'
import { Text } from '~/components/common/Text'
import { TypeBadge } from '~/components/common/TypeBadge'
import { useGraphData } from '~/components/DataRetriever'
import { useDataStore } from '~/stores/useDataStore'
import { NodeExtended } from '~/types'
import { getSelectedNodeTimestamps } from '~/utils'
Expand Down
69 changes: 38 additions & 31 deletions src/components/Auth/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import { Text } from '~/components/common/Text'
import { isDevelopment, isE2E } from '~/constants'
import { getIsAdmin } from '~/network/auth'
import { useUserStore } from '~/stores/useUserStore'
import { executeIfProd, getSignedMessageFromRelay, updateBudget } from '~/utils'
import { sphinxBridge } from '~/testSphinxBridge'
import { getSignedMessageFromRelay, updateBudget } from '~/utils'

interface setAuthenticated {
setAuthenticated: (state: boolean) => void
Expand All @@ -18,49 +19,55 @@ export const Auth = ({ setAuthenticated }: setAuthenticated) => {
const [setBudget, setIsAdmin, setPubKey] = useUserStore((s) => [s.setBudget, s.setIsAdmin, s.setPubKey])

const handleAuth = useCallback(async () => {
await executeIfProd(async () => {
localStorage.removeItem('admin')
// await executeIfProd(async () => {
localStorage.removeItem('admin')

try {
let sphinxEnable

try {
if (!isE2E) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const sphinxEnable = await sphinx.enable()

sessionStorage.setItem('isSphinx', sphinxEnable ? 'true' : 'false')

setPubKey(sphinxEnable?.pubkey)
} catch (error) {
setPubKey('')
sphinxEnable = await sphinx.enable()
} else {
sphinxEnable = await sphinxBridge.enable()
}

await updateBudget(setBudget)
sessionStorage.setItem('isSphinx', sphinxEnable ? 'true' : 'false')

try {
const sigAndMessage = await getSignedMessageFromRelay()
setPubKey(sphinxEnable?.pubkey)
} catch (error) {
setPubKey('')
}

const res = await getIsAdmin({
message: sigAndMessage.message,
signature: sigAndMessage.signature,
})
await updateBudget(setBudget)

if (!res.data.isPublic && !res.data.isAdmin && !res.data.isMember) {
setUnauthorized(true)
try {
const sigAndMessage = await getSignedMessageFromRelay()

return
}
const res = await getIsAdmin({
message: sigAndMessage.message,
signature: sigAndMessage.signature,
})

if (res.data.isAdmin) {
localStorage.setItem('admin', JSON.stringify({ isAdmin: true }))
setIsAdmin(true)
}
if (!res.data.isPublic && !res.data.isAdmin && !res.data.isMember) {
setUnauthorized(true)

setAuthenticated(true)
return
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (error: any) {
/* not an admin */
if (res.data.isAdmin) {
localStorage.setItem('admin', JSON.stringify({ isAdmin: true }))
setIsAdmin(true)
}
})

setAuthenticated(true)

// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (error: any) {
/* not an admin */
}
// })

if (isE2E || isDevelopment) {
setAuthenticated(true)
Expand Down
2 changes: 1 addition & 1 deletion src/components/Modal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export const BaseModal = ({
py={noWrap ? 0 : 20}
>
{onClose && (
<CloseButton onClick={onClose}>
<CloseButton data-testid="close-modal" onClick={onClose}>
<ClearIcon />
</CloseButton>
)}
Expand Down
2 changes: 1 addition & 1 deletion src/components/SettingsModal/SettingsView/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export const SettingsView: React.FC<Props> = ({ onClose }) => {
const SettingsHeader = ({ children }: { children: React.ReactNode }) => (
<StyledHeader>
<Flex direction="row" pt={3}>
<StyledText>{getSettingsLabel()}</StyledText>
<StyledText data-testid="setting-label">{getSettingsLabel()}</StyledText>
{resolveAdminActions()}
</Flex>
{children}
Expand Down
4 changes: 2 additions & 2 deletions src/components/Universe/Graph/Particles/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ export const Particles: React.FC = () => {
const ref = useRef<THREE.Points>(null)

useFrame(() => {
const positions = (ref.current?.geometry.getAttribute('position') as THREE.BufferAttribute).array as Float32Array
const velocities = (ref.current?.geometry.getAttribute('velocity') as THREE.BufferAttribute).array as Float32Array
const positions = (ref.current?.geometry.getAttribute('position') as THREE.BufferAttribute)?.array as Float32Array
const velocities = (ref.current?.geometry.getAttribute('velocity') as THREE.BufferAttribute)?.array as Float32Array

if (positions && velocities) {
for (let i = 0; i < positions.length; i += 3) {
Expand Down
11 changes: 11 additions & 0 deletions src/testSphinxBridge/enable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { getCurrentUser } from './helper'

export function enableSphinx() {
const user = getCurrentUser()

if (!user) {
return null
}

return { budget: 0, pubkey: user.pubkey }
}
47 changes: 47 additions & 0 deletions src/testSphinxBridge/getLsat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { getCurrentUser } from './helper'

export async function getLsatSphinx(host: string) {
try {
const user = getCurrentUser()

if (user) {
const result = await fetch(`${user.external_ip}/active_lsat?issuer=${host}`, {
headers: { 'x-user-token': user.authToken },
})

const res = await result.json()

if (res.success) {
const { lsat } = res.response

return {
paymentRequest: lsat.paymentRequest,
macaroon: lsat.macaroon,
issuer: lsat.issuer,
identifier: lsat.identifier,
preimage: lsat.preimage,
paths: lsat.paths,
status: lsat.status,
success: false,
}
}

return {
paymentRequest: '',
macaroon: '',
issuer: '',
identifier: '',
preimage: '',
paths: '',
status: '',
success: false,
}
}

return null
} catch (error) {
console.log(JSON.stringify(error))

Check warning on line 43 in src/testSphinxBridge/getLsat.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected console statement

return null
}
}
29 changes: 29 additions & 0 deletions src/testSphinxBridge/helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { RelayUser } from '~/types'
import nodes from '../../relay/nodes.json'

export function getCurrentUser(): RelayUser | null {
/* eslint-disable @typescript-eslint/no-explicit-any */
if ((window as any).CYPRESS_USER) {
const user = (window as any).CYPRESS_USER

let userNode

for (let i = 0; i < nodes.length; i += 1) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
if (nodes[i].alias === user) {
userNode = nodes[i]

break
}
}

if (!userNode) {
return null
}

return userNode
}

return null
}
Loading

0 comments on commit 3e47acc

Please sign in to comment.