|
| 1 | +import { |
| 2 | + createUsers, |
| 3 | + createUserToken, |
| 4 | + expectHTTPErrorCode, |
| 5 | + getTestClient, |
| 6 | +} from './utils'; |
| 7 | +import chai from 'chai'; |
| 8 | +import chaiAsPromised from 'chai-as-promised'; |
| 9 | +import { v4 as uuidv4 } from 'uuid'; |
| 10 | + |
| 11 | +chai.use(chaiAsPromised); |
| 12 | + |
| 13 | +function randomIP() { |
| 14 | + return ( |
| 15 | + Math.floor(Math.random() * 255) + |
| 16 | + 1 + |
| 17 | + '.' + |
| 18 | + Math.floor(Math.random() * 255) + |
| 19 | + '.' + |
| 20 | + Math.floor(Math.random() * 255) + |
| 21 | + '.' + |
| 22 | + Math.floor(Math.random() * 255) |
| 23 | + ); |
| 24 | +} |
| 25 | + |
| 26 | +// mockIP appends X-Forwarded-For to the user agent string |
| 27 | +function mockIP(client, ip) { |
| 28 | + client.setUserAgent( |
| 29 | + client.getUserAgent() + `&X-Forwarded-For=${ip}&X-Forwarded-Port=80`, |
| 30 | + ); |
| 31 | +} |
| 32 | + |
| 33 | +describe('ban user by ip', () => { |
| 34 | + const serverClient = getTestClient(true); |
| 35 | + const adminUser = { |
| 36 | + id: uuidv4(), |
| 37 | + }; |
| 38 | + |
| 39 | + const ip1 = randomIP(); |
| 40 | + const ip2 = randomIP(); |
| 41 | + const ip3 = randomIP(); |
| 42 | + |
| 43 | + const tommasoID = `tommaso-${uuidv4()}`; |
| 44 | + const thierryID = `thierry-${uuidv4()}`; |
| 45 | + const tommasoChannelId = `test-channels-${uuidv4()}`; |
| 46 | + const thierryChannelId = `test-channels-${uuidv4()}`; |
| 47 | + const tommasoToken = createUserToken(tommasoID); |
| 48 | + const thierryToken = createUserToken(thierryID); |
| 49 | + const tommasoClient1 = getTestClient(); |
| 50 | + mockIP(tommasoClient1, ip1); |
| 51 | + const tommasoClient2 = getTestClient(); |
| 52 | + mockIP(tommasoClient2, ip2); |
| 53 | + const thierryClient1 = getTestClient(); |
| 54 | + mockIP(thierryClient1, ip1); |
| 55 | + const thierryClient2 = getTestClient(); |
| 56 | + mockIP(thierryClient2, ip3); |
| 57 | + |
| 58 | + before(async () => { |
| 59 | + await createUsers([adminUser.id]); |
| 60 | + await tommasoClient1.setUser({ id: tommasoID }, tommasoToken); |
| 61 | + await thierryClient1.setUser({ id: thierryID }, thierryToken); |
| 62 | + }); |
| 63 | + |
| 64 | + it('tommaso and thierry create channels', async () => { |
| 65 | + await tommasoClient1.channel('messaging', tommasoChannelId).watch(); |
| 66 | + await thierryClient1.channel('messaging', thierryChannelId).watch(); |
| 67 | + }); |
| 68 | + |
| 69 | + it('tommasso1 is not banned yet', async () => { |
| 70 | + await tommasoClient1 |
| 71 | + .channel('messaging', tommasoChannelId) |
| 72 | + .sendMessage({ text: 'I am not banned yet' }); |
| 73 | + }); |
| 74 | + |
| 75 | + it('ban tommaso by IP', async () => { |
| 76 | + await serverClient.banUser(tommasoID, { |
| 77 | + user_id: adminUser.id, |
| 78 | + ip_ban: true, |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + it('tommasso1 is banned', async () => { |
| 83 | + await expectHTTPErrorCode( |
| 84 | + 403, |
| 85 | + tommasoClient1 |
| 86 | + .channel('messaging', tommasoChannelId) |
| 87 | + .sendMessage({ text: 'I am banned' }), |
| 88 | + ); |
| 89 | + }); |
| 90 | + |
| 91 | + it('thierry1 is banned because he has same ip', async () => { |
| 92 | + await expectHTTPErrorCode( |
| 93 | + 403, |
| 94 | + thierryClient1 |
| 95 | + .channel('messaging', thierryChannelId) |
| 96 | + .sendMessage({ text: 'I am banned' }), |
| 97 | + ); |
| 98 | + }); |
| 99 | + |
| 100 | + it('tommaso and thierry switch IP addresses', async () => { |
| 101 | + await tommasoClient2.setUser({ id: tommasoID }, tommasoToken); |
| 102 | + await thierryClient2.setUser({ id: thierryID }, thierryToken); |
| 103 | + await tommasoClient2.channel('messaging', tommasoChannelId).watch(); |
| 104 | + await thierryClient2.channel('messaging', thierryChannelId).watch(); |
| 105 | + }); |
| 106 | + |
| 107 | + it('tommasso2 is banned', async () => { |
| 108 | + await expectHTTPErrorCode( |
| 109 | + 403, |
| 110 | + tommasoClient2 |
| 111 | + .channel('messaging', tommasoChannelId) |
| 112 | + .sendMessage({ text: 'I am banned' }), |
| 113 | + ); |
| 114 | + }); |
| 115 | + |
| 116 | + it('thierry2 is not banned', async () => { |
| 117 | + await thierryClient2 |
| 118 | + .channel('messaging', thierryChannelId) |
| 119 | + .sendMessage({ text: 'I am not banned' }); |
| 120 | + }); |
| 121 | + |
| 122 | + it('unban tommaso', async () => { |
| 123 | + await serverClient.unbanUser(tommasoID); |
| 124 | + }); |
| 125 | + |
| 126 | + it('no one is banned', async () => { |
| 127 | + await tommasoClient1 |
| 128 | + .channel('messaging', tommasoChannelId) |
| 129 | + .sendMessage({ text: 'I am not banned' }); |
| 130 | + await tommasoClient2 |
| 131 | + .channel('messaging', tommasoChannelId) |
| 132 | + .sendMessage({ text: 'I am not banned' }); |
| 133 | + await thierryClient1 |
| 134 | + .channel('messaging', thierryChannelId) |
| 135 | + .sendMessage({ text: 'I am not banned' }); |
| 136 | + await thierryClient2 |
| 137 | + .channel('messaging', thierryChannelId) |
| 138 | + .sendMessage({ text: 'I am not banned' }); |
| 139 | + }); |
| 140 | +}); |
0 commit comments