|
| 1 | +import test from 'tape'; |
| 2 | +import {Observable} from 'rx'; |
| 3 | +import database from '../../db'; |
| 4 | +import { |
| 5 | + getBooks, |
| 6 | + getBooksLength, |
| 7 | + permissionBook |
| 8 | +} from './books' |
| 9 | + |
| 10 | +const dbconf = { |
| 11 | + mongodb: { |
| 12 | + uri: process.env.MONGO_URI || 'mongodb://localhost:27017/dev', |
| 13 | + }, |
| 14 | + |
| 15 | + neo4j: { |
| 16 | + uri: process.env.NEO_URI || 'bolt://localhost', |
| 17 | + }, |
| 18 | +}; |
| 19 | + |
| 20 | +const populate = ` |
| 21 | + CREATE (reader:User { _id:"testUser1" }) |
| 22 | + CREATE (user:User { _id:"testUser" }) |
| 23 | + CREATE (world:World {_id: "testWorld"})<-[:OWNER {archived: false}]-(user) |
| 24 | + CREATE (world)<-[:READER {archived: false}]-(reader) |
| 25 | + CREATE (:Book {_id : "testBook"})-[:IN {archived: false}]->(world) |
| 26 | + CREATE (:Book {_id: "testBook1" })-[:IN {archived: false}]->(world) |
| 27 | + CREATE (:Book {_id: "testBookAlone"}) |
| 28 | + return user |
| 29 | + `; |
| 30 | +const remove = ` |
| 31 | + MATCH (n) |
| 32 | + OPTIONAL MATCH (n)-[r]-() |
| 33 | + WITH n,r LIMIT 50000 |
| 34 | + WHERE n._id =~ ".*test.*" |
| 35 | + DELETE n,r |
| 36 | + RETURN count(n) as deletedNodesCount |
| 37 | + `; |
| 38 | + |
| 39 | +const books = [ |
| 40 | + {_id: 'testBook', title: 'Book for test'}, |
| 41 | + {_id: 'testBook1', title: 'Book for test2'}, |
| 42 | +]; |
| 43 | + |
| 44 | +const bookIds = books.map(book => book._id); |
| 45 | + |
| 46 | +test('permissionBook', t => { |
| 47 | + const db = database(dbconf); |
| 48 | + let neo, mongo, actual, expected; |
| 49 | + let book = 'testBook'; |
| 50 | + let owner = 'testUser'; |
| 51 | + let reader = 'testUser1' |
| 52 | + |
| 53 | + db |
| 54 | + .map(db => { |
| 55 | + neo = db.neo; |
| 56 | + mongo = db.mongo; |
| 57 | + return neo; |
| 58 | + }) |
| 59 | + .flatMap(neo => |
| 60 | + neo.run(populate) |
| 61 | + ) |
| 62 | + .flatMap(neo => |
| 63 | + db::permissionBook(book, owner) |
| 64 | + ) |
| 65 | + .flatMap(permission => { |
| 66 | + |
| 67 | + actual = permission; |
| 68 | + expected = true; |
| 69 | + t.equals(actual, expected, "should have permission to read the book"); |
| 70 | + |
| 71 | + |
| 72 | + return db::permissionBook(book, owner, true) |
| 73 | + }) |
| 74 | + .flatMap(permission => { |
| 75 | + |
| 76 | + actual = permission; |
| 77 | + expected = true; |
| 78 | + t.equals(actual, expected, "should have permission to modify the book"); |
| 79 | + |
| 80 | + return db::permissionBook(book, reader, true) |
| 81 | + }) |
| 82 | + .flatMap(permission => { |
| 83 | + |
| 84 | + actual = permission; |
| 85 | + expected = false; |
| 86 | + t.equals(actual, expected, "should not have permission to modify the book"); |
| 87 | + |
| 88 | + return neo.run(remove); |
| 89 | + }) |
| 90 | + .subscribe(() => { |
| 91 | + mongo.close().then(() => neo.close(() => neo.disconnect())); |
| 92 | + t.end(); |
| 93 | + }) |
| 94 | +}); |
| 95 | + |
| 96 | +test('getBooks', t => { |
| 97 | + const db = database(dbconf); |
| 98 | + let neo, mongo, actual, expected; |
| 99 | + let booksToFind = ["testBook", "testBook1"]; |
| 100 | + let userId = 'testUser'; |
| 101 | + db |
| 102 | + .map(db => { |
| 103 | + neo = db.neo; |
| 104 | + mongo = db.mongo; |
| 105 | + return neo; |
| 106 | + }) |
| 107 | + .flatMap(neo => |
| 108 | + neo.run(populate) |
| 109 | + ) |
| 110 | + .flatMap(() => |
| 111 | + mongo.collection('books').insertMany(books) |
| 112 | + ) |
| 113 | + .flatMap(neo => |
| 114 | + db::getBooks(booksToFind, userId) |
| 115 | + ) |
| 116 | + .flatMap((book) => { |
| 117 | + |
| 118 | + actual = bookIds.find(id => book._id) != null; |
| 119 | + expected = true; |
| 120 | + t.equals(actual, expected, 'should match one of the ids that has been passed to getBooks'); |
| 121 | + |
| 122 | + return neo.run(remove) |
| 123 | + }) |
| 124 | + |
| 125 | + .flatMap(() => { |
| 126 | + //t.throws(() => db::getBooks(['invalidID'], userId), 'should throw an exception'); |
| 127 | + return mongo.collection('books').removeMany({_id: {$in: bookIds}}) |
| 128 | + }) |
| 129 | + .subscribe( |
| 130 | + () => { |
| 131 | + mongo.close().then(() => neo.close(() => neo.disconnect())); |
| 132 | + t.end(); |
| 133 | + }, |
| 134 | + error => { |
| 135 | + mongo.close().then(() => neo.close(() => neo.disconnect())); |
| 136 | + } |
| 137 | + ) |
| 138 | +}); |
| 139 | + |
| 140 | + |
| 141 | +test('getBooksLength', t => { |
| 142 | + const db = database(dbconf); |
| 143 | + let neo, mongo, actual, expected; |
| 144 | + |
| 145 | + db |
| 146 | + .map(db => { |
| 147 | + neo = db.neo; |
| 148 | + mongo = db.mongo; |
| 149 | + return neo; |
| 150 | + }) |
| 151 | + .flatMap(neo => |
| 152 | + neo.run(populate) |
| 153 | + ) |
| 154 | + .flatMap(() => |
| 155 | + db::getBooksLength('testWorld') |
| 156 | + ) |
| 157 | + .flatMap(length => { |
| 158 | + |
| 159 | + actual = length; |
| 160 | + expected = 2; |
| 161 | + t.equals(actual, expected, "should match the number of books the world has"); |
| 162 | + |
| 163 | + return neo.run(remove); |
| 164 | + }) |
| 165 | + .flatMap(() => |
| 166 | + db::getBooksLength('invalidWorld') |
| 167 | + ) |
| 168 | + .flatMap(length => { |
| 169 | + |
| 170 | + actual = length; |
| 171 | + expected = 0; |
| 172 | + t.equals(actual, expected, "should not have any book when the world is not valid"); |
| 173 | + |
| 174 | + return neo.run(remove); |
| 175 | + }) |
| 176 | + .subscribe(() => { |
| 177 | + mongo.close().then(() => neo.close(() => neo.disconnect())); |
| 178 | + t.end(); |
| 179 | + }) |
| 180 | +}); |
| 181 | + |
0 commit comments