Skip to content

Commit

Permalink
Add legacy relay service route (safe-global#1155)
Browse files Browse the repository at this point in the history
This adds the legacy relayer routes to the relay controller for redirection purposes:

- `POST` `/v1/relay/:chainId`
- `GET` `/v1/relay/:chainId/:safeAddress`
  • Loading branch information
iamacook authored Feb 19, 2024
1 parent 90374be commit a286a32
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/routes/relay/relay.controller.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import { Module } from '@nestjs/common';
import { RelayDomainModule } from '@/domain/relay/relay.domain.module';
import { RelayService } from '@/routes/relay/relay.service';
import { RelayController } from '@/routes/relay/relay.controller';
import { RelayLegacyController } from '@/routes/relay/relay.legacy.controller';

@Module({
imports: [RelayDomainModule],
providers: [RelayService],
controllers: [RelayController],
controllers: [RelayController, RelayLegacyController],
})
export class RelayControllerModule {}
87 changes: 87 additions & 0 deletions src/routes/relay/relay.legacy.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { Test, TestingModule } from '@nestjs/testing';
import * as request from 'supertest';
import { AppModule } from '@/app.module';
import { CacheModule } from '@/datasources/cache/cache.module';
import { TestCacheModule } from '@/datasources/cache/__tests__/test.cache.module';
import configuration from '@/config/entities/__tests__/configuration';
import { RequestScopedLoggingModule } from '@/logging/logging.module';
import { TestLoggingModule } from '@/logging/__tests__/test.logging.module';
import { NetworkModule } from '@/datasources/network/network.module';
import { TestNetworkModule } from '@/datasources/network/__tests__/test.network.module';
import { TestAppProvider } from '@/__tests__/test-app.provider';
import { AccountDataSourceModule } from '@/datasources/account/account.datasource.module';
import { TestAccountDataSourceModule } from '@/datasources/account/__tests__/test.account.datasource.module';
import { INestApplication } from '@nestjs/common';
import { faker } from '@faker-js/faker';

describe('Relay controller', () => {
let app: INestApplication;
const supportedChainIds = Object.keys(configuration().relay.apiKey);

beforeEach(async () => {
jest.resetAllMocks();

const defaultConfiguration = configuration();
const testConfiguration = (): typeof defaultConfiguration => ({
...defaultConfiguration,
features: {
...defaultConfiguration.features,
relay: true,
},
});

const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule.register(testConfiguration)],
})
.overrideModule(AccountDataSourceModule)
.useModule(TestAccountDataSourceModule)
.overrideModule(CacheModule)
.useModule(TestCacheModule)
.overrideModule(RequestScopedLoggingModule)
.useModule(TestLoggingModule)
.overrideModule(NetworkModule)
.useModule(TestNetworkModule)
.compile();

app = await new TestAppProvider().provide(moduleFixture);
await app.init();
});

afterAll(async () => {
await app.close();
});

describe('POST /v1/chains/:chainId/relay', () => {
it('should return 302 and redirect to the new endpoint', async () => {
const chainId = faker.helpers.arrayElement(supportedChainIds);
const safeAddress = faker.finance.ethereumAddress();
const data = faker.string.hexadecimal();

await request(app.getHttpServer())
.post(`/v1/relay/${chainId}`)
.send({
to: safeAddress,
data,
})
.expect(308)
.expect((res) => {
expect(res.get('location')).toBe('/v1/chains/:chainId/relay');
});
});
});
describe('GET /v1/relay/:chainId/:safeAddress', () => {
it('should return 302 and redirect to the new endpoint', async () => {
const chainId = faker.string.numeric();
const safeAddress = faker.finance.ethereumAddress();

await request(app.getHttpServer())
.get(`/v1/relay/${chainId}/${safeAddress}`)
.expect(301)
.expect((res) => {
expect(res.get('location')).toBe(
'/v1/chains/:chainId/relay/:safeAddress',
);
});
});
});
});
18 changes: 18 additions & 0 deletions src/routes/relay/relay.legacy.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Controller, Post, Redirect, Get, HttpStatus } from '@nestjs/common';

@Controller({
version: '1',
path: 'relay/:chainId',
})
export class RelayLegacyController {
@Post()
@Redirect('/v1/chains/:chainId/relay', HttpStatus.PERMANENT_REDIRECT)
relay(): void {}

@Get(':safeAddress')
@Redirect(
'/v1/chains/:chainId/relay/:safeAddress',
HttpStatus.MOVED_PERMANENTLY,
)
getRelaysRemaining(): void {}
}

0 comments on commit a286a32

Please sign in to comment.