|
| 1 | +/* global describe before it */ |
| 2 | +import chai from 'chai' |
| 3 | +import crypto from 'crypto' |
| 4 | +import chaiHTTP from 'chai-http' |
| 5 | +import { tools, wallet } from 'nanocurrency-web' |
| 6 | + |
| 7 | +import server from '#api/server.mjs' |
| 8 | +import knex from '#db' |
| 9 | +import { mochaGlobalSetup } from './global.mjs' |
| 10 | + |
| 11 | +process.env.NODE_ENV = 'test' |
| 12 | +// chai.should() |
| 13 | +chai.use(chaiHTTP) |
| 14 | +const expect = chai.expect |
| 15 | + |
| 16 | +describe('API /auth', () => { |
| 17 | + before(mochaGlobalSetup) |
| 18 | + |
| 19 | + describe('errors', () => { |
| 20 | + it('should return 400 if pub field is missing', async () => { |
| 21 | + const response = await chai |
| 22 | + .request(server) |
| 23 | + .post('/api/auth/register') |
| 24 | + .send({ |
| 25 | + address: 'someaddress', |
| 26 | + signature: 'somesignature', |
| 27 | + username: 'test_username' |
| 28 | + }) // missing public_key |
| 29 | + expect(response).to.have.status(400) |
| 30 | + expect(response.body.error).to.include('missing public_key param') |
| 31 | + }) |
| 32 | + |
| 33 | + it('should return 400 if address field is missing', async () => { |
| 34 | + const response = await chai |
| 35 | + .request(server) |
| 36 | + .post('/api/auth/register') |
| 37 | + .send({ |
| 38 | + public_key: 'somepub', |
| 39 | + signature: 'somesignature', |
| 40 | + username: 'test_username' |
| 41 | + }) // missing address |
| 42 | + expect(response).to.have.status(400) |
| 43 | + expect(response.body.error).to.include('missing address param') |
| 44 | + }) |
| 45 | + |
| 46 | + it('should return 400 if signature field is missing', async () => { |
| 47 | + const response = await chai |
| 48 | + .request(server) |
| 49 | + .post('/api/auth/register') |
| 50 | + .send({ |
| 51 | + public_key: 'somepub', |
| 52 | + address: 'someaddress', |
| 53 | + username: 'test_username' |
| 54 | + }) // missing signature |
| 55 | + expect(response).to.have.status(400) |
| 56 | + expect(response.body.error).to.include('missing signature param') |
| 57 | + }) |
| 58 | + |
| 59 | + it('should return 400 if username field is missing', async () => { |
| 60 | + const response = await chai |
| 61 | + .request(server) |
| 62 | + .post('/api/auth/register') |
| 63 | + .send({ |
| 64 | + public_key: 'somepub', |
| 65 | + address: 'someaddress', |
| 66 | + signature: 'somesignature' |
| 67 | + }) // missing username |
| 68 | + expect(response).to.have.status(400) |
| 69 | + expect(response.body.error).to.include('missing username param') |
| 70 | + }) |
| 71 | + |
| 72 | + it('should return 401 if pub param is invalid', async () => { |
| 73 | + const w = wallet.generate() |
| 74 | + const account = w.accounts[0] |
| 75 | + |
| 76 | + const response = await chai |
| 77 | + .request(server) |
| 78 | + .post('/api/auth/register') |
| 79 | + .send({ |
| 80 | + public_key: 'invalidpub', |
| 81 | + address: account.address, |
| 82 | + signature: 'somesignature', |
| 83 | + username: 'test_username' |
| 84 | + }) |
| 85 | + expect(response).to.have.status(401) |
| 86 | + expect(response.body.error).to.equal('invalid public_key param') |
| 87 | + }) |
| 88 | + |
| 89 | + it('should return 401 if address param is invalid', async () => { |
| 90 | + const w = wallet.generate() |
| 91 | + const account = w.accounts[0] |
| 92 | + |
| 93 | + const response = await chai |
| 94 | + .request(server) |
| 95 | + .post('/api/auth/register') |
| 96 | + .send({ |
| 97 | + public_key: account.publicKey, |
| 98 | + address: 'invalidaddress', |
| 99 | + signature: 'somesignature', |
| 100 | + username: 'test_username' |
| 101 | + }) |
| 102 | + expect(response).to.have.status(401) |
| 103 | + expect(response.body.error).to.equal('invalid address param') |
| 104 | + }) |
| 105 | + |
| 106 | + const invalid_usernames = [ |
| 107 | + 'contains space', |
| 108 | + 'constains@character', |
| 109 | + '1starts_with_number', |
| 110 | + 'contains!character', |
| 111 | + 'contains.period', |
| 112 | + 'contains-hyphen', |
| 113 | + 'contains$dollar', |
| 114 | + 'contains#hash' |
| 115 | + ] |
| 116 | + |
| 117 | + invalid_usernames.forEach((username) => { |
| 118 | + it(`should return 401 if username param is invalid: ${username}`, async () => { |
| 119 | + const w = wallet.generate() |
| 120 | + const account = w.accounts[0] |
| 121 | + |
| 122 | + const response = await chai |
| 123 | + .request(server) |
| 124 | + .post('/api/auth/register') |
| 125 | + .send({ |
| 126 | + public_key: account.publicKey, |
| 127 | + address: account.address, |
| 128 | + signature: 'somesignature', |
| 129 | + username |
| 130 | + }) |
| 131 | + expect(response).to.have.status(401) |
| 132 | + expect(response.body.error).to.equal('invalid username param') |
| 133 | + }) |
| 134 | + }) |
| 135 | + |
| 136 | + it('should return 401 if signature is invalid', async () => { |
| 137 | + const w = wallet.generate() |
| 138 | + const account = w.accounts[0] |
| 139 | + |
| 140 | + const signature = tools.sign(account.privateKey, 'some message') |
| 141 | + |
| 142 | + const response = await chai |
| 143 | + .request(server) |
| 144 | + .post('/api/auth/register') |
| 145 | + .send({ |
| 146 | + public_key: account.publicKey, |
| 147 | + address: account.address, |
| 148 | + signature, |
| 149 | + username: 'test_username' |
| 150 | + }) |
| 151 | + expect(response).to.have.status(401) |
| 152 | + expect(response.body.error).to.equal('invalid signature') |
| 153 | + }) |
| 154 | + |
| 155 | + it('should return 401 if username already exists', async () => { |
| 156 | + const seed = crypto.randomBytes(64).toString('hex') |
| 157 | + const accounts = wallet.accounts(seed, 0, 1) |
| 158 | + const account_0 = accounts[0] |
| 159 | + const account_1 = accounts[1] |
| 160 | + |
| 161 | + await knex('users').insert({ |
| 162 | + id: 1, |
| 163 | + username: 'existing_username', |
| 164 | + public_key: account_0.publicKey, |
| 165 | + last_visit: Math.floor(Date.now() / 1000) |
| 166 | + }) |
| 167 | + |
| 168 | + const signature = tools.sign(account_1.privateKey, account_1.publicKey) |
| 169 | + |
| 170 | + const response = await chai |
| 171 | + .request(server) |
| 172 | + .post('/api/auth/register') |
| 173 | + .send({ |
| 174 | + public_key: account_1.publicKey, |
| 175 | + address: account_1.address, |
| 176 | + signature, |
| 177 | + username: 'existing_username' |
| 178 | + }) |
| 179 | + expect(response).to.have.status(401) |
| 180 | + expect(response.body.error).to.equal('username exists') |
| 181 | + }) |
| 182 | + }) |
| 183 | +}) |
0 commit comments