|
| 1 | +const hooks = require('hooks'); |
| 2 | +const {MongoClient} = require('mongodb'); |
| 3 | +const ObjectID = require('mongodb').ObjectID; |
| 4 | + |
| 5 | +let db; |
| 6 | + |
| 7 | +const address = [ |
| 8 | + {"_id":ObjectID("579f21ae98684924944651bd"),"_class":"works.weave.socks.accounts.entities.Address","number":"69","street":"Wilson Street","city":"Hartlepool","postcode":"TS26 8JU","country":"United Kingdom"}, |
| 9 | + {"_id":ObjectID("579f21ae98684924944651c0"),"_class":"works.weave.socks.accounts.entities.Address","number":"122","street":"Radstone WayNet","city":"Northampton","postcode":"NN2 8NT","country":"United Kingdom"}, |
| 10 | + {"_id":ObjectID("579f21ae98684924944651c3"),"_class":"works.weave.socks.accounts.entities.Address","number":"3","street":"Radstone Way","city":"Northampton","postcode":"NN2 8NT","country":"United Kingdom"} |
| 11 | +]; |
| 12 | + |
| 13 | + |
| 14 | +const card = [ |
| 15 | + {"_id":ObjectID("579f21ae98684924944651be"),"_class":"works.weave.socks.accounts.entities.Card","longNum":"8575776807334952","expires":"08/19","ccv":"014"}, |
| 16 | + {"_id":ObjectID("579f21ae98684924944651c1"),"_class":"works.weave.socks.accounts.entities.Card","longNum":"8918468841895184","expires":"08/19","ccv":"597"}, |
| 17 | + {"_id":ObjectID("579f21ae98684924944651c4"),"_class":"works.weave.socks.accounts.entities.Card","longNum":"6426429851404909","expires":"08/19","ccv":"381"} |
| 18 | +]; |
| 19 | + |
| 20 | +const cart = [ |
| 21 | + {"_id":ObjectID("579f21de98689ebf2bf1cd2f"),"_class":"works.weave.socks.cart.entities.Cart","customerId":"579f21ae98684924944651bf","items":[{"$ref":"item","$id":ObjectID("579f227698689ebf2bf1cd31")},{"$ref":"item","$id":ObjectID("579f22ac98689ebf2bf1cd32")}]}, |
| 22 | + {"_id":ObjectID("579f21e298689ebf2bf1cd30"),"_class":"works.weave.socks.cart.entities.Cart","customerId":"579f21ae98684924944651bfaa","items":[]} |
| 23 | +]; |
| 24 | + |
| 25 | + |
| 26 | +const item = [ |
| 27 | + {"_id":ObjectID("579f227698689ebf2bf1cd31"),"_class":"works.weave.socks.cart.entities.Item","itemId":"819e1fbf-8b7e-4f6d-811f-693534916a8b","quantity":20,"unitPrice":99.0} |
| 28 | +]; |
| 29 | + |
| 30 | + |
| 31 | +const customer = [ |
| 32 | + {"_id":"579f21ae98684924944651bf","_class":"works.weave.socks.accounts.entities.Customer","firstName":"Eve","lastName":"Berger","username":"Eve_Berger","addresses":[{"$ref":"address","$id":ObjectID("579f21ae98684924944651bd")}],"cards":[{"$ref":"card","$id":ObjectID("579f21ae98684924944651be")}] |
| 33 | + }, |
| 34 | + {"_id":"579f21ae98684924944651c2","_class":"works.weave.socks.accounts.entities.Customer","firstName":"User","lastName":"Name","username":"user","addresses":[{"$ref":"address","$id":ObjectID("579f21ae98684924944651c0")}],"cards":[{"$ref":"card","$id":ObjectID("579f21ae98684924944651c1")}]}, |
| 35 | + {"_id":"579f21ae98684924944651c5","_class":"works.weave.socks.accounts.entities.Customer","firstName":"User1","lastName":"Name1","username":"user1","addresses":[{"$ref":"address","$id":ObjectID("579f21ae98684924944651c3")}],"cards":[{"$ref":"card","$id":ObjectID("579f21ae98684924944651c4")}]} |
| 36 | +]; |
| 37 | + |
| 38 | + |
| 39 | +// Setup database connection before Dredd starts testing |
| 40 | +hooks.beforeAll((transactions, done) => { |
| 41 | + var MongoEndpoint = process.env.MONGO_ENDPOINT || 'mongodb://localhost:32769/data'; |
| 42 | + MongoClient.connect(MongoEndpoint, function(err, conn) { |
| 43 | + if (err) { |
| 44 | + console.error(err); |
| 45 | + } |
| 46 | + db = conn; |
| 47 | + done(err); |
| 48 | + }); |
| 49 | +}); |
| 50 | + |
| 51 | +// Close database connection after Dredd finishes testing |
| 52 | +hooks.afterAll((transactions, done) => { |
| 53 | + db.dropDatabase(); |
| 54 | + done(); |
| 55 | + |
| 56 | +}); |
| 57 | + |
| 58 | +hooks.beforeEach((transaction, done) => { |
| 59 | + db.dropDatabase(function(s, r) { |
| 60 | + var promisesToKeep = [ |
| 61 | + db.collection('customer').insertMany(customer), |
| 62 | + db.collection('card').insertMany(card), |
| 63 | + db.collection('cart').insertMany(cart), |
| 64 | + db.collection('address').insertMany(address), |
| 65 | + db.collection('item').insertMany(item) |
| 66 | + ]; |
| 67 | + Promise.all(promisesToKeep).then(function(vls) { |
| 68 | + done(); |
| 69 | + }, function(vls) { |
| 70 | + console.error(vls); |
| 71 | + done(); |
| 72 | + }); |
| 73 | + }) |
| 74 | + |
| 75 | +}); |
| 76 | + |
| 77 | + |
| 78 | +hooks.before("/orders > POST", function(transaction, done) { |
| 79 | + transaction.request.headers['Content-Type'] = 'application/json'; |
| 80 | + transaction.request.body = JSON.stringify( |
| 81 | + { |
| 82 | + "customer":"http://accounts-orders-mock:80/customers/57a98d98e4b00679b4a830af", |
| 83 | + "address": "http://accounts-orders-mock:80/addresses/57a98d98e4b00679b4a830ad", |
| 84 | + "card" : "http://accounts-orders-mock:80/cards/57a98d98e4b00679b4a830ae", |
| 85 | + "items": "http://accounts-orders-mock:80/carts/579f21ae98684924944651bf/items" |
| 86 | + } |
| 87 | + ); |
| 88 | + |
| 89 | + done() |
| 90 | + |
| 91 | +}); |
| 92 | + |
| 93 | +hooks.before("/orders > GET", function(transaction, done) { |
| 94 | + transaction.request.headers["User-Agent"] = "curl/7.43.0"; |
| 95 | + transaction.request.headers["Accept"] = "*/*"; |
| 96 | + done(); |
| 97 | +}) |
0 commit comments