Skip to content

Commit

Permalink
add: セッション消去機能
Browse files Browse the repository at this point in the history
  • Loading branch information
takonasu committed Mar 19, 2023
1 parent 1c344fa commit abca9af
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 38 deletions.
98 changes: 65 additions & 33 deletions __tests__/grpc/Session.service.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { startGrpcServer, stopGrpcServer } from '../../src/grpc'
import { prismaClient } from '../../src/database/prisma-client'
import * as protoLoader from '@grpc/proto-loader'
import path from 'path'
import * as grpc from '@grpc/grpc-js'
Expand All @@ -22,26 +23,32 @@ beforeAll(async () => {
) as unknown) as GrpcClient<SessionService>
})

const userId = uuid()
let sessionId = ''

test('セッションを作成する', (done) => {
client.startSession({ userId }, (err, res) => {
const userId = [uuid(), uuid()]
let sessionId = ['', '']
test('セッションAを作成する', (done) => {
client.startSession({ userId: userId[0] }, (err, res) => {
expect(err).toBeNull()
expect(res?.session?.userId).toEqual(userId[0])
expect(res?.session?.sessionId).not.toBeNull()
sessionId[0] = res?.session?.sessionId ?? ''
done()
})
})
test('セッションBを作成する', (done) => {
client.startSession({ userId: userId[1] }, (err, res) => {
expect(err).toBeNull()
expect(res?.session?.userId).toEqual(userId)
expect(res?.session?.userId).toEqual(userId[1])
expect(res?.session?.sessionId).not.toBeNull()
sessionId = res?.session?.sessionId ?? ''
console.log(res)
sessionId[1] = res?.session?.sessionId ?? ''
done()
})
})

test('存在するセッションを返す', (done) => {
client.getSession({ sessionId }, (err, res) => {
console.log(err)
test('存在するセッションAを返す', (done) => {
client.getSession({ sessionId: sessionId[0] }, (err, res) => {
expect(err).toBeNull()
expect(res?.userId).toEqual(userId)
expect(res?.sessionId).toEqual(sessionId)
expect(res?.userId).toEqual(userId[0])
expect(res?.sessionId).toEqual(sessionId[0])
done()
})
})
Expand All @@ -53,26 +60,51 @@ test('存在しないセッションでエラーを返す', (done) => {
})
})

//未実装
// test('セッションを消去する', (done) => {
// client.deleteSession({ sessionId }, (err, res) => {
// expect(err).toBeNull()
// done()
// })
// })
test('存在しないセッション(空文字列)でエラーを返す', (done) => {
client.getSession({ sessionId: '' }, (err, res) => {
expect(err?.code).toBe(3)
done()
})
})

test('セッションAを消去する', (done) => {
client.deleteSession({ sessionId: sessionId[0] }, (err, res) => {
expect(err).toBeNull()
done()
})
})

test('存在しないセッションを消去するとエラーを返す', (done) => {
client.deleteSession({ sessionId: uuid() }, (err, res) => {
expect(err?.code).toBe(5)
done()
})
})

test('存在しないセッション(空文字列)を消去するとエラーを返す', (done) => {
client.deleteSession({ sessionId: '' }, (err, res) => {
expect(err?.code).toBe(3)
done()
})
})

// test('存在しないセッションを消去するとエラーを返す', (done) => {
// client.deleteSession({ sessionId: uuid() }, (err, res) => {
// expect(err?.code).toBe(5)
// done()
// })
// })
test('消去したセッションAを得ようとしてもエラーを返す', (done) => {
client.getSession({ sessionId: sessionId[0] }, (err, res) => {
expect(err?.code).toBe(5)
done()
})
})

// test('消去したセッションを得ようとしてもエラーを返す', (done) => {
// client.getSession({ sessionId }, (err, res) => {
// expect(err?.code).toBe(5)
// done()
// })
// })
test('存在するセッションBを返す', (done) => {
client.getSession({ sessionId: sessionId[1] }, (err, res) => {
expect(err).toBeNull()
expect(res?.userId).toEqual(userId[1])
expect(res?.sessionId).toEqual(sessionId[1])
done()
})
})

afterAll(stopGrpcServer)
afterAll(() => {
stopGrpcServer()
prismaClient.$disconnect()
})
2 changes: 1 addition & 1 deletion jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export default {
// runner: "jest-runner",

// The paths to modules that run some code to configure or set up the testing environment before each test
// setupFiles: [],
setupFiles: ['dotenv/config'],

// A list of paths to modules that run some code to configure or set up the testing framework before each test
// setupFilesAfterEnv: [],
Expand Down
9 changes: 8 additions & 1 deletion protos/SessionService.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import "google/protobuf/timestamp.proto";
service SessionService {
rpc StartSession(StartSessionRequest) returns (StartSessionResponse);
rpc GetSession(GetSessionRequest) returns (Session);
rpc DeleteSession(DeleteSessionRequest) returns (DeleteSessionResponse);
}

message StartSessionRequest {
Expand All @@ -29,4 +30,10 @@ message Session {
string session_id = 1;
string user_id = 2;
google.protobuf.Timestamp expired_at = 3;
}
}

message DeleteSessionRequest {
string session_id = 1;
}

message DeleteSessionResponse {}
2 changes: 1 addition & 1 deletion src/database/prisma-client.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { PrismaClient } from '@prisma/client'

export const prismaClient = new PrismaClient({ log: ['query'] })
export const prismaClient = new PrismaClient({ log: ['error'] })
12 changes: 12 additions & 0 deletions src/database/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,15 @@ export async function findActiveSessionById({
},
})
}

export async function deleteSessionById({
id,
}: {
id: string
}): Promise<{ id: string } | null> {
return await prismaClient.session.delete({
where: {
id,
},
})
}
33 changes: 31 additions & 2 deletions src/grpc/session.service.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
import { Status } from '@grpc/grpc-js/build/src/constants'
import dayjs from 'dayjs'
import { google, SessionService, StartSessionResponse } from '../../generated'
import {
SessionService,
StartSessionResponse,
DeleteSessionResponse,
} from '../../generated'
import { sessionLifeTimeHours } from '../constant'
import { createSession, findActiveSessionById } from '../database/session'
import {
createSession,
findActiveSessionById,
deleteSessionById,
} from '../database/session'
import { GrpcServer } from './type'
import { transformSession } from './transformer'

export const sessionService: GrpcServer<SessionService> = {
async startSession({ request }, callback) {
if (!request.userId) {
return callback({ code: Status.INVALID_ARGUMENT })
}
const session = await createSession({
userId: request.userId,
expiredAt: dayjs().add(sessionLifeTimeHours, 'hour'),
Expand All @@ -25,6 +36,9 @@ export const sessionService: GrpcServer<SessionService> = {
)
},
async getSession({ request }, callback) {
if (!request.sessionId) {
return callback({ code: Status.INVALID_ARGUMENT })
}
const session = await findActiveSessionById({
id: request.sessionId,
})
Expand All @@ -35,4 +49,19 @@ export const sessionService: GrpcServer<SessionService> = {

callback(null, transformSession(session))
},
async deleteSession({ request }, callback) {
if (!request.sessionId) {
return callback({ code: Status.INVALID_ARGUMENT })
}

try {
await deleteSessionById({
id: request.sessionId,
})
} catch {
return callback({ code: Status.NOT_FOUND })
}

callback(null, DeleteSessionResponse.create())
},
}

0 comments on commit abca9af

Please sign in to comment.