|
| 1 | +import prisma from '../../util/prisma'; |
| 2 | +import { makeError } from '../../util/error'; |
| 3 | +import { createEntity, deleteEntity, getEntities, getEntity, updateEntity } from '../entity'; |
| 4 | +import { PrismaClientKnownRequestError } from '@prisma/client/runtime'; |
| 5 | + |
| 6 | + |
| 7 | +describe('entity controller', () => { |
| 8 | + beforeEach(() => { |
| 9 | + jest.mock('../../util/prisma'); |
| 10 | + }) |
| 11 | + |
| 12 | + afterEach(() => { |
| 13 | + jest.restoreAllMocks(); |
| 14 | + }); |
| 15 | + |
| 16 | + describe('create entity', () => { |
| 17 | + test('valid create - no socials', () => { |
| 18 | + const spy = jest.spyOn(prisma.entity, 'create'); |
| 19 | + spy.mockResolvedValueOnce({ id: 1, type: 'ARTIST', name: 'testArtist' }); |
| 20 | + |
| 21 | + expect.assertions(1) |
| 22 | + return expect(createEntity({ name: 'testArtist', type: 'ARTIST' })).resolves.toEqual({id: 1, type: 'ARTIST', name: 'testArtist'}); |
| 23 | + }) |
| 24 | + |
| 25 | + test('valid create - with socials', () => { |
| 26 | + const spy = jest.spyOn(prisma.entity, 'create'); |
| 27 | + spy.mockResolvedValueOnce({ id: 1, type: 'ARTIST', name: 'testArtist', }); |
| 28 | + |
| 29 | + expect.assertions(1) |
| 30 | + return expect( |
| 31 | + createEntity({ |
| 32 | + name: 'testArtist', |
| 33 | + type: 'ARTIST', |
| 34 | + socials: [ |
| 35 | + { type: 'TELEGRAM', value: 'telegramArtist' }, |
| 36 | + { type: 'TWITTER', value: 'twitterArtist' }, |
| 37 | + { type: 'WEBSITE', value: 'https://websiteArtist.com' }, |
| 38 | + { type: 'FURAFFINITY', value: 'furaffinityArtist' }, |
| 39 | + { type: 'CUSTOM', value: 'customArtistValue', name: 'customField' }, |
| 40 | + ] |
| 41 | + }) |
| 42 | + ).resolves.toEqual({id: 1, type: 'ARTIST', name: 'testArtist'}); |
| 43 | + }) |
| 44 | + |
| 45 | + test('invalid entity type', () => { |
| 46 | + expect.assertions(1) |
| 47 | + return expect(createEntity({ name: 'testArtist', type: 'wrong type' })).rejects.toEqual(makeError('user', 'Invalid entity type')); |
| 48 | + }) |
| 49 | + |
| 50 | + test('invalid social type', () => { |
| 51 | + expect.assertions(1) |
| 52 | + return expect(createEntity({ name: 'testCharacter', type: 'CHARACTER', socials: [{type: "wrong type", value: "something"}] })) |
| 53 | + .rejects.toEqual(makeError('user', 'Invalid social. Non-valid type provided')); |
| 54 | + }) |
| 55 | + |
| 56 | + test('invalid social form', () => { |
| 57 | + expect.assertions(1) |
| 58 | + return expect(createEntity({ name: 'testCharacter', type: 'CHARACTER', socials: [{type: "wrong type"}] })) |
| 59 | + .rejects.toEqual(makeError('user', 'Invalid social. Must provide type, value')); |
| 60 | + }) |
| 61 | + |
| 62 | + test('non-custom social provided name', () => { |
| 63 | + expect.assertions(1) |
| 64 | + return expect(createEntity({ name: 'testCharacter', type: 'CHARACTER', socials: [{type: 'TELEGRAM', name: "bad", value: "something"}] })) |
| 65 | + .rejects.toEqual(makeError('user', 'Invalid social. Non-custom social fields should not specify name')); |
| 66 | + }) |
| 67 | + |
| 68 | + test('custom social missing name', () => { |
| 69 | + expect.assertions(1) |
| 70 | + return expect(createEntity({ name: 'testCharacter', type: 'CHARACTER', socials: [{type: "CUSTOM", value: "something"}] })) |
| 71 | + .rejects.toEqual(makeError('user', 'Invalid social. Custom socials must specify name')); |
| 72 | + }) |
| 73 | + |
| 74 | + test('prisma error', () => { |
| 75 | + const spy = jest.spyOn(prisma.entity, 'create'); |
| 76 | + spy.mockRejectedValueOnce(new Error()); |
| 77 | + expect.assertions(1) |
| 78 | + return expect(createEntity({ name: 'testCharacter', type: 'CHARACTER', socials: [{type: "CUSTOM", name: 'test', value: "something"}] })) |
| 79 | + .rejects.toEqual(makeError('server')); |
| 80 | + }) |
| 81 | + }) |
| 82 | + |
| 83 | + describe('get entity', () => { |
| 84 | + test('valid fetch', () => { |
| 85 | + const spy = jest.spyOn(prisma.entity, 'findUnique'); |
| 86 | + |
| 87 | + const mock_entity = { |
| 88 | + id: 1, |
| 89 | + name: "testArtist", |
| 90 | + type: 'ARTIST', |
| 91 | + socials: [ { id: 1, type: "TELEGRAM", value: 'telegramArtist', name: null, entityId: 1 } ] |
| 92 | + } |
| 93 | + // @ts-ignore |
| 94 | + spy.mockResolvedValueOnce(mock_entity); |
| 95 | + |
| 96 | + expect.assertions(1) |
| 97 | + return expect(getEntity(1)).resolves.toEqual(mock_entity); |
| 98 | + }) |
| 99 | + |
| 100 | + test('non-existant entity', () => { |
| 101 | + const spy = jest.spyOn(prisma.entity, 'findUnique'); |
| 102 | + spy.mockRejectedValueOnce(new PrismaClientKnownRequestError('', 'P2001', '')); |
| 103 | + |
| 104 | + expect.assertions(1); |
| 105 | + return expect(getEntity(1)).rejects.toEqual(makeError('user', 'Could not find entity')); |
| 106 | + }) |
| 107 | + |
| 108 | + test('prisma error', () => { |
| 109 | + const spy = jest.spyOn(prisma.entity, 'findUnique'); |
| 110 | + spy.mockRejectedValueOnce(new Error()); |
| 111 | + |
| 112 | + expect.assertions(1); |
| 113 | + return expect(getEntity(1)).rejects.toEqual(makeError('server')); |
| 114 | + }) |
| 115 | + |
| 116 | + test('invalid ID', () => { |
| 117 | + expect.assertions(1); |
| 118 | + return expect(getEntity(NaN)).rejects.toEqual(makeError('user', 'Invalid ID provided')) |
| 119 | + }) |
| 120 | + }) |
| 121 | + |
| 122 | + describe('get entities', () => { |
| 123 | + test('valid fetch', () => { |
| 124 | + const spy = jest.spyOn(prisma.entity, 'findMany'); |
| 125 | + spy.mockResolvedValueOnce([1,2].map(i => ({id: i, name: 'testEnt', type: 'ARTIST'}))); |
| 126 | + |
| 127 | + expect.assertions(1); |
| 128 | + return expect(getEntities()).resolves.toEqual([1,2].map(i => ({id: i, name: 'testEnt', type: 'ARTIST'}))); |
| 129 | + }) |
| 130 | + |
| 131 | + test('prisma error', () => { |
| 132 | + const spy = jest.spyOn(prisma.entity, 'findMany'); |
| 133 | + spy.mockRejectedValueOnce(new Error()); |
| 134 | + |
| 135 | + expect.assertions(1) |
| 136 | + return expect(getEntities()).rejects.toEqual(makeError('server')); |
| 137 | + }) |
| 138 | + }) |
| 139 | + |
| 140 | + describe('delete entity', () => { |
| 141 | + test('valid deletion', () => { |
| 142 | + const spy = jest.spyOn(prisma.entity, 'delete'); |
| 143 | + spy.mockResolvedValueOnce({id: 1, name: 'testEnt', type: 'ARTIST'}); |
| 144 | + expect.assertions(1) |
| 145 | + return expect(deleteEntity(1)).resolves.toEqual({id: 1, name: 'testEnt', type: 'ARTIST'}); |
| 146 | + }) |
| 147 | + |
| 148 | + test('invalid ID', () => { |
| 149 | + expect.assertions(1) |
| 150 | + return expect(deleteEntity(NaN)).rejects.toEqual(makeError('user', 'Invalid ID provided')); |
| 151 | + }) |
| 152 | + |
| 153 | + test('non-existant entity', () => { |
| 154 | + const spy = jest.spyOn(prisma.entity, 'delete'); |
| 155 | + spy.mockRejectedValueOnce(new PrismaClientKnownRequestError('', 'P2001', '')); |
| 156 | + expect.assertions(1); |
| 157 | + return expect(deleteEntity(1)).rejects.toEqual(makeError('user', 'Could not find entity')); |
| 158 | + }) |
| 159 | + |
| 160 | + test('entity has associated commissions', () => { |
| 161 | + const spy = jest.spyOn(prisma.entity, 'delete'); |
| 162 | + spy.mockRejectedValueOnce(new PrismaClientKnownRequestError('', 'P2003', '')); |
| 163 | + expect.assertions(1); |
| 164 | + return expect(deleteEntity(1)).rejects.toEqual(makeError('user', 'Entity has commissions associated with it')); |
| 165 | + }) |
| 166 | + |
| 167 | + test('prisma error', () => { |
| 168 | + const spy = jest.spyOn(prisma.entity, 'delete'); |
| 169 | + spy.mockRejectedValueOnce(new Error()); |
| 170 | + expect.assertions(1); |
| 171 | + return expect(deleteEntity(1)).rejects.toEqual(makeError('server')); |
| 172 | + }) |
| 173 | + }) |
| 174 | + |
| 175 | + describe('update entity', () => { |
| 176 | + test('valid update', () => { |
| 177 | + const spy = jest.spyOn(prisma.entity, 'update'); |
| 178 | + spy.mockResolvedValueOnce({id: 1, name: 'newName', type: 'CHARACTER'}); |
| 179 | + expect.assertions(1); |
| 180 | + const updateArgs = { |
| 181 | + name: 'newName', |
| 182 | + type: 'CHARACTER', |
| 183 | + socials: { |
| 184 | + add: [{type: 'TELEGRAM', value: 'entTelegram'}], |
| 185 | + remove: [1,2,3] |
| 186 | + } |
| 187 | + } |
| 188 | + return expect(updateEntity(1, updateArgs)).resolves.toEqual({id: 1, name: 'newName', type: 'CHARACTER'}); |
| 189 | + }) |
| 190 | + |
| 191 | + test('invalid name', () => { |
| 192 | + expect.assertions(1) |
| 193 | + return expect(updateEntity(1, {name: ''})).rejects.toEqual(makeError('user', 'Entity name cannot be empty')); |
| 194 | + }) |
| 195 | + |
| 196 | + test('invalid type', () => { |
| 197 | + expect.assertions(1) |
| 198 | + return expect(updateEntity(1, {type: ''})).rejects.toEqual(makeError('user', 'Invalid entity type')); |
| 199 | + }) |
| 200 | + |
| 201 | + test('invalid social type', () => { |
| 202 | + expect.assertions(1) |
| 203 | + return expect(updateEntity(1, {socials: { add: [{type: "wrong type", value: "wrong"}] }})).rejects.toEqual(makeError('user', 'Invalid social. Non-valid type provided')); |
| 204 | + }) |
| 205 | + |
| 206 | + test('invalid social form', () => { |
| 207 | + expect.assertions(1) |
| 208 | + return expect(updateEntity(1, {socials: { add: [{type: "wrong type"}] }})).rejects.toEqual(makeError('user', 'Invalid social. Must provide type, value')); |
| 209 | + }) |
| 210 | + |
| 211 | + test('non-custom social with name', () => { |
| 212 | + expect.assertions(1) |
| 213 | + return expect(updateEntity(1, {socials: { add: [{type: "TELEGRAM", value: 'entTelegram', name: 'bad'}] }})).rejects.toEqual(makeError('user', 'Invalid social. Non-custom social fields should not specify name')); |
| 214 | + }) |
| 215 | + |
| 216 | + test('custom social without name', () => { |
| 217 | + expect.assertions(1) |
| 218 | + return expect(updateEntity(1, {socials: { add: [{type: "CUSTOM", value: 'entVal'}] }})).rejects.toEqual(makeError('user', 'Invalid social. Custom socials must specify name')); |
| 219 | + }) |
| 220 | + |
| 221 | + test('invalid social ID', () => { |
| 222 | + expect.assertions(1) |
| 223 | + return expect(updateEntity(1, {socials: { remove: [NaN] }})).rejects.toEqual(makeError('user', 'Invalid ID provided')); |
| 224 | + }) |
| 225 | + |
| 226 | + test('invalid entity ID', () => { |
| 227 | + expect.assertions(1) |
| 228 | + return expect(updateEntity(NaN, {socials: { remove: [NaN] }})).rejects.toEqual(makeError('user', 'Invalid ID provided')); |
| 229 | + }) |
| 230 | + |
| 231 | + test('prisma error', () => { |
| 232 | + const spy = jest.spyOn(prisma.entity, 'update'); |
| 233 | + spy.mockRejectedValueOnce(new Error()); |
| 234 | + expect.assertions(1); |
| 235 | + return expect(updateEntity(1, {name: "something"})).rejects.toEqual(makeError('server')); |
| 236 | + }) |
| 237 | + }) |
| 238 | +}) |
0 commit comments