|
| 1 | +import { validate } from 'class-validator'; |
| 2 | +import { plainToInstance } from 'class-transformer'; |
| 3 | +import { ListSubscriptionsQueryDto } from './list-subscriptions-query.dto'; |
| 4 | +import { ListCreatorSubscribersQueryDto } from './list-creator-subscribers-query.dto'; |
| 5 | +import { SubscriptionStateQueryDto } from './subscription-state-query.dto'; |
| 6 | +import { SubscriptionIndexerEventDto } from './subscription-indexer-event.dto'; |
| 7 | +import { SetSpendingCapDto } from './spending-cap.dto'; |
| 8 | +import { FanDashboardQueryDto } from './fan-dashboard-query.dto'; |
| 9 | + |
| 10 | +describe('ListSubscriptionsQueryDto – invalid input', () => { |
| 11 | + async function validateDto(plain: object) { |
| 12 | + const dto = plainToInstance(ListSubscriptionsQueryDto, plain); |
| 13 | + return validate(dto); |
| 14 | + } |
| 15 | + |
| 16 | + it('fails when fan is empty string', async () => { |
| 17 | + const errors = await validateDto({ fan: '' }); |
| 18 | + expect(errors.some((e) => e.property === 'fan')).toBe(true); |
| 19 | + }); |
| 20 | + |
| 21 | + it('fails when fan is missing', async () => { |
| 22 | + const errors = await validateDto({}); |
| 23 | + expect(errors.some((e) => e.property === 'fan')).toBe(true); |
| 24 | + }); |
| 25 | + |
| 26 | + it('fails when status is an invalid string', async () => { |
| 27 | + const errors = await validateDto({ fan: 'GFAN', status: 'bogus' }); |
| 28 | + const statusErrors = errors.filter((e) => e.property === 'status'); |
| 29 | + expect(statusErrors).toHaveLength(1); |
| 30 | + }); |
| 31 | + |
| 32 | + it('fails when sort is an invalid string', async () => { |
| 33 | + const errors = await validateDto({ fan: 'GFAN', sort: 'alphabetical' }); |
| 34 | + const sortErrors = errors.filter((e) => e.property === 'sort'); |
| 35 | + expect(sortErrors).toHaveLength(1); |
| 36 | + }); |
| 37 | + |
| 38 | + it('fails when limit is zero', async () => { |
| 39 | + const errors = await validateDto({ fan: 'GFAN', limit: 0 }); |
| 40 | + expect(errors.some((e) => e.property === 'limit')).toBe(true); |
| 41 | + }); |
| 42 | + |
| 43 | + it('fails when limit is negative', async () => { |
| 44 | + const errors = await validateDto({ fan: 'GFAN', limit: -5 }); |
| 45 | + expect(errors.some((e) => e.property === 'limit')).toBe(true); |
| 46 | + }); |
| 47 | + |
| 48 | + it('fails when limit exceeds maximum', async () => { |
| 49 | + const errors = await validateDto({ fan: 'GFAN', limit: 101 }); |
| 50 | + expect(errors.some((e) => e.property === 'limit')).toBe(true); |
| 51 | + }); |
| 52 | + |
| 53 | + it('fails when page is zero', async () => { |
| 54 | + const errors = await validateDto({ fan: 'GFAN', page: 0 }); |
| 55 | + expect(errors.some((e) => e.property === 'page')).toBe(true); |
| 56 | + }); |
| 57 | + |
| 58 | + it('fails when page is negative', async () => { |
| 59 | + const errors = await validateDto({ fan: 'GFAN', page: -1 }); |
| 60 | + expect(errors.some((e) => e.property === 'page')).toBe(true); |
| 61 | + }); |
| 62 | +}); |
| 63 | + |
| 64 | +describe('ListCreatorSubscribersQueryDto – invalid input', () => { |
| 65 | + async function validateDto(plain: object) { |
| 66 | + const dto = plainToInstance(ListCreatorSubscribersQueryDto, plain); |
| 67 | + return validate(dto); |
| 68 | + } |
| 69 | + |
| 70 | + it('fails when creator is missing', async () => { |
| 71 | + const errors = await validateDto({}); |
| 72 | + expect(errors.some((e) => e.property === 'creator')).toBe(true); |
| 73 | + }); |
| 74 | + |
| 75 | + it('fails when creator is empty string', async () => { |
| 76 | + const errors = await validateDto({ creator: '' }); |
| 77 | + expect(errors.some((e) => e.property === 'creator')).toBe(true); |
| 78 | + }); |
| 79 | + |
| 80 | + it('fails when status is not active or expired', async () => { |
| 81 | + const errors = await validateDto({ creator: 'GCREATOR', status: 'pending' }); |
| 82 | + expect(errors.some((e) => e.property === 'status')).toBe(true); |
| 83 | + }); |
| 84 | + |
| 85 | + it('fails when sort is invalid', async () => { |
| 86 | + const errors = await validateDto({ creator: 'GCREATOR', sort: 'price' }); |
| 87 | + expect(errors.some((e) => e.property === 'sort')).toBe(true); |
| 88 | + }); |
| 89 | + |
| 90 | + it('fails when limit exceeds maximum', async () => { |
| 91 | + const errors = await validateDto({ creator: 'GCREATOR', limit: 200 }); |
| 92 | + expect(errors.some((e) => e.property === 'limit')).toBe(true); |
| 93 | + }); |
| 94 | + |
| 95 | + it('fails when limit is zero', async () => { |
| 96 | + const errors = await validateDto({ creator: 'GCREATOR', limit: 0 }); |
| 97 | + expect(errors.some((e) => e.property === 'limit')).toBe(true); |
| 98 | + }); |
| 99 | +}); |
| 100 | + |
| 101 | +describe('SubscriptionStateQueryDto – invalid input', () => { |
| 102 | + async function validateDto(plain: object) { |
| 103 | + const dto = plainToInstance(SubscriptionStateQueryDto, plain); |
| 104 | + return validate(dto); |
| 105 | + } |
| 106 | + |
| 107 | + it('fails when creator is missing', async () => { |
| 108 | + const errors = await validateDto({}); |
| 109 | + expect(errors.some((e) => e.property === 'creator')).toBe(true); |
| 110 | + }); |
| 111 | + |
| 112 | + it('fails when creator is empty string', async () => { |
| 113 | + const errors = await validateDto({ creator: '' }); |
| 114 | + expect(errors.some((e) => e.property === 'creator')).toBe(true); |
| 115 | + }); |
| 116 | + |
| 117 | + it('fails when creator does not match G-address format', async () => { |
| 118 | + const errors = await validateDto({ creator: 'not-a-stellar-address' }); |
| 119 | + const creatorErrors = errors.filter((e) => e.property === 'creator'); |
| 120 | + expect(creatorErrors.length).toBeGreaterThan(0); |
| 121 | + }); |
| 122 | + |
| 123 | + it('fails when creator is lowercase g-address', async () => { |
| 124 | + const errors = await validateDto({ creator: 'g' + 'A'.repeat(55) }); |
| 125 | + const creatorErrors = errors.filter((e) => e.property === 'creator'); |
| 126 | + expect(creatorErrors.length).toBeGreaterThan(0); |
| 127 | + }); |
| 128 | + |
| 129 | + it('fails when creator is too short', async () => { |
| 130 | + const errors = await validateDto({ creator: 'GABCDEF' }); |
| 131 | + const creatorErrors = errors.filter((e) => e.property === 'creator'); |
| 132 | + expect(creatorErrors.length).toBeGreaterThan(0); |
| 133 | + }); |
| 134 | +}); |
| 135 | + |
| 136 | +describe('SubscriptionIndexerEventDto – invalid input', () => { |
| 137 | + async function validateDto(plain: object) { |
| 138 | + const dto = plainToInstance(SubscriptionIndexerEventDto, plain); |
| 139 | + return validate(dto); |
| 140 | + } |
| 141 | + |
| 142 | + it('fails when event is missing', async () => { |
| 143 | + const errors = await validateDto({ userId: 'GFAN', creatorId: 'GCREATOR', planId: 1 }); |
| 144 | + expect(errors.some((e) => e.property === 'event')).toBe(true); |
| 145 | + }); |
| 146 | + |
| 147 | + it('fails when event is invalid value', async () => { |
| 148 | + const errors = await validateDto({ event: 'created', userId: 'GFAN', creatorId: 'GCREATOR', planId: 1 }); |
| 149 | + expect(errors.some((e) => e.property === 'event')).toBe(true); |
| 150 | + }); |
| 151 | + |
| 152 | + it('fails when userId is missing', async () => { |
| 153 | + const errors = await validateDto({ event: 'renewed', creatorId: 'GCREATOR', planId: 1 }); |
| 154 | + expect(errors.some((e) => e.property === 'userId')).toBe(true); |
| 155 | + }); |
| 156 | + |
| 157 | + it('fails when creatorId is missing', async () => { |
| 158 | + const errors = await validateDto({ event: 'renewed', userId: 'GFAN', planId: 1 }); |
| 159 | + expect(errors.some((e) => e.property === 'creatorId')).toBe(true); |
| 160 | + }); |
| 161 | + |
| 162 | + it('fails when planId is missing', async () => { |
| 163 | + const errors = await validateDto({ event: 'renewed', userId: 'GFAN', creatorId: 'GCREATOR' }); |
| 164 | + expect(errors.some((e) => e.property === 'planId')).toBe(true); |
| 165 | + }); |
| 166 | + |
| 167 | + it('fails when planId is zero', async () => { |
| 168 | + const errors = await validateDto({ event: 'renewed', userId: 'GFAN', creatorId: 'GCREATOR', planId: 0 }); |
| 169 | + expect(errors.some((e) => e.property === 'planId')).toBe(true); |
| 170 | + }); |
| 171 | + |
| 172 | + it('fails when planId is negative', async () => { |
| 173 | + const errors = await validateDto({ event: 'renewed', userId: 'GFAN', creatorId: 'GCREATOR', planId: -1 }); |
| 174 | + expect(errors.some((e) => e.property === 'planId')).toBe(true); |
| 175 | + }); |
| 176 | + |
| 177 | + it('fails when expiry is negative', async () => { |
| 178 | + const errors = await validateDto({ event: 'renewed', userId: 'GFAN', creatorId: 'GCREATOR', planId: 1, expiry: -100 }); |
| 179 | + expect(errors.some((e) => e.property === 'expiry')).toBe(true); |
| 180 | + }); |
| 181 | + |
| 182 | + it('fails when cancelledAt is negative', async () => { |
| 183 | + const errors = await validateDto({ event: 'cancelled', userId: 'GFAN', creatorId: 'GCREATOR', planId: 1, cancelledAt: -1 }); |
| 184 | + expect(errors.some((e) => e.property === 'cancelledAt')).toBe(true); |
| 185 | + }); |
| 186 | + |
| 187 | + it('passes for valid renewed event', async () => { |
| 188 | + const errors = await validateDto({ event: 'renewed', userId: 'GFAN', creatorId: 'GCREATOR', planId: 1, expiry: 1700000000 }); |
| 189 | + expect(errors).toHaveLength(0); |
| 190 | + }); |
| 191 | + |
| 192 | + it('passes for valid cancelled event', async () => { |
| 193 | + const errors = await validateDto({ event: 'cancelled', userId: 'GFAN', creatorId: 'GCREATOR', planId: 1, cancelledAt: 1700000000 }); |
| 194 | + expect(errors).toHaveLength(0); |
| 195 | + }); |
| 196 | +}); |
| 197 | + |
| 198 | +describe('SetSpendingCapDto – invalid input', () => { |
| 199 | + async function validateDto(plain: object) { |
| 200 | + const dto = plainToInstance(SetSpendingCapDto, plain); |
| 201 | + return validate(dto); |
| 202 | + } |
| 203 | + |
| 204 | + it('fails when period is invalid', async () => { |
| 205 | + const errors = await validateDto({ period: 'weekly' }); |
| 206 | + expect(errors.some((e) => e.property === 'period')).toBe(true); |
| 207 | + }); |
| 208 | + |
| 209 | + it('fails when capAmount is negative', async () => { |
| 210 | + const errors = await validateDto({ capAmount: -100, period: 'monthly' }); |
| 211 | + expect(errors.some((e) => e.property === 'capAmount')).toBe(true); |
| 212 | + }); |
| 213 | + |
| 214 | + it('fails when capAmount is a float', async () => { |
| 215 | + const errors = await validateDto({ capAmount: 10.5, period: 'monthly' }); |
| 216 | + expect(errors.some((e) => e.property === 'capAmount')).toBe(true); |
| 217 | + }); |
| 218 | + |
| 219 | + it('passes when capAmount is zero', async () => { |
| 220 | + const errors = await validateDto({ capAmount: 0, period: 'monthly' }); |
| 221 | + expect(errors.filter((e) => e.property === 'capAmount')).toHaveLength(0); |
| 222 | + }); |
| 223 | + |
| 224 | + it('passes when capAmount is omitted', async () => { |
| 225 | + const errors = await validateDto({ period: 'monthly' }); |
| 226 | + expect(errors.filter((e) => e.property === 'capAmount')).toHaveLength(0); |
| 227 | + }); |
| 228 | +}); |
| 229 | + |
| 230 | +describe('FanDashboardQueryDto – invalid input', () => { |
| 231 | + async function validateDto(plain: object) { |
| 232 | + const dto = plainToInstance(FanDashboardQueryDto, plain); |
| 233 | + return validate(dto); |
| 234 | + } |
| 235 | + |
| 236 | + it('fails when page is zero', async () => { |
| 237 | + const errors = await validateDto({ page: 0 }); |
| 238 | + expect(errors.some((e) => e.property === 'page')).toBe(true); |
| 239 | + }); |
| 240 | + |
| 241 | + it('fails when page is negative', async () => { |
| 242 | + const errors = await validateDto({ page: -1 }); |
| 243 | + expect(errors.some((e) => e.property === 'page')).toBe(true); |
| 244 | + }); |
| 245 | + |
| 246 | + it('fails when limit is zero', async () => { |
| 247 | + const errors = await validateDto({ limit: 0 }); |
| 248 | + expect(errors.some((e) => e.property === 'limit')).toBe(true); |
| 249 | + }); |
| 250 | + |
| 251 | + it('fails when limit exceeds maximum', async () => { |
| 252 | + const errors = await validateDto({ limit: 101 }); |
| 253 | + expect(errors.some((e) => e.property === 'limit')).toBe(true); |
| 254 | + }); |
| 255 | + |
| 256 | + it('passes with valid page and limit', async () => { |
| 257 | + const errors = await validateDto({ page: 1, limit: 50 }); |
| 258 | + expect(errors).toHaveLength(0); |
| 259 | + }); |
| 260 | + |
| 261 | + it('passes when both are omitted', async () => { |
| 262 | + const errors = await validateDto({}); |
| 263 | + expect(errors).toHaveLength(0); |
| 264 | + }); |
| 265 | +}); |
0 commit comments