From 2450eaaad906b4153ee4b7058db650f7249245ac Mon Sep 17 00:00:00 2001 From: noah Date: Wed, 18 Apr 2018 00:27:42 -0700 Subject: [PATCH] Tests non-localhost hosts --- .gitignore | 3 ++- index.test.js | 45 +++++++++++++++++++++++++++++++++++++++++++-- models/model.js | 3 ++- readme.md | 6 ++++-- 4 files changed, 51 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 76add87..9c97bbd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules -dist \ No newline at end of file +dist +.env diff --git a/index.test.js b/index.test.js index 1c199b1..66c725a 100644 --- a/index.test.js +++ b/index.test.js @@ -6,11 +6,11 @@ import { isEqual, sortBy, isEmpty } from 'lodash' dotenv.config() -const { NAME } = process.env +const { NAME, TEST_EXTERNAL_HOST } = process.env const username = `test${randomBytes(8).toString('hex')}` -const db = ormjs.connect({ +let db = ormjs.connect({ database: NAME || 'test' }) @@ -208,3 +208,44 @@ test('complex query', async () => { expect(posts.data[0].creator.name).toBe(username) }) + +test('connect without localhost', async () => { + /* + The purpose of this test is to be sure other hosts work. I use ngrok like so + + bash> ngrok http 127.0.0.1:8529 + + ngrok will output a WAN address like `123abc.ngrok.io` which you should make TEST_EXTERNAL_HOST in your env. + + https://github.com/OKNoah/final-orm/issues/16 + */ + db = ormjs.connect({ + host: TEST_EXTERNAL_HOST, + protocol: 'https', + port: 443, + user: 'root', + password: '', + database: NAME || 'test' + }) + + class User2 extends db.Model { + static schema = { + name: { $type: String, index: true, unique: true }, + profile: { + vegan: { $type: Boolean, optional: true } + } + } + } + + const newUserName = username + '123' + + await User2.add({ name: newUserName }) + + const user = await User2.findOne({ + where: { name: newUserName } + }) + + expect(TEST_EXTERNAL_HOST).not.toBe(undefined) + expect(user).toHaveProperty('createdAt') + expect(moment(user.createdAt).isValid()).toBe(true) +}) diff --git a/models/model.js b/models/model.js index 7c3c67f..dd242fb 100644 --- a/models/model.js +++ b/models/model.js @@ -29,7 +29,8 @@ export default class Model { const port = this.options.port || 8529 const username = this.options.username || 'root' const password = this.options.password || '' - const url = this.options.url || `http://${username}:${password}@${host}:${port}` + const protocol = this.options.protocol || `${host === 'localhost' ? 'http' : 'https'}` + const url = this.options.url || `${protocol}://${username}:${password}@${host}:${port}` const db = arangojs({ url diff --git a/readme.md b/readme.md index 8e757c9..d0ef474 100644 --- a/readme.md +++ b/readme.md @@ -48,13 +48,15 @@ create init class model.js var orm = require('final-orm') var options = { database: 'test', // db name - // You can initialize the database using a url. + // You can initialize the database using just a url. url: 'http://root:@localhost:8529', // Or supply each of these values. You do not need both. host: 'localhost', port: '8529', username: 'root', - password: '' + password: '', + // You can also supply a protocol. If localhost, it's `http` by default, otherwise `https` + protocol: 'tcp' } var { Model, Edge } = orm.connect(options)