/*globals describe, before, it, after*/
'use strict'

require('should')

var utils = require('./utils'),
	mongoose = require('../mongoose'),
	Log = mongoose.model('Log1')

describe('write API', function () {
	var peer
	before(function (done) {
		utils.connect(function (err, peer_) {
			peer = peer_
			done(err)
		})
	})

	var log = {
		date: new Date,
		name: 'name',
		level: 2,
		relevance: 1,
		time: 17,
		message: 'message',
		commit: new Buffer([3, 14, 15]),
		extra: {
			'my-extra': [92, 65]
		}
	}
	
	it('should accept the write message', function () {
		peer.send('log', log)
	})

	it('should return success for the write call', function (done) {
		peer.call('log', log, done)
	})

	it('should have inserted the logs', function (done) {
		Log.find().sort('-_id').limit(2).lean().select('-_id').exec(function (err, logs) {
			if (err) {
				return done(err)
			}
			logs.should.have.length(2)
			logs[0].should.be.eql(logs[1])
			logs[0].commit = logs[0].commit.buffer
			logs[0].should.be.eql({
				origin: 'test',
				date: log.date,
				name: log.name,
				level: log.level,
				time: log.time,
				message: log.message,
				commit: log.commit,
				extra: log.extra
			})
			done()
		})
	})

	after(function (done) {
		peer.once('close', done)
		peer.close()
	})
})
@@ -47,16 +47,29 @@
 trict'%0A%0A
+var should = 
 require(
@@ -77,22 +77,19 @@
 should')
-%0A%0Avar 
+,%0A%09
 utils = 
@@ -173,16 +173,79 @@
 ('Log1')
+,%0A%09request = require('request'),%0A%09config = require('../config')
 %0A%0Adescri
@@ -576,16 +576,83 @@
 %09%09%7D%0A%09%7D%0A%09
+log.commit.toJSON = function () %7B%0A%09%09return this.toString('hex')%0A%09%7D%0A
 %0A%09it('sh
@@ -823,24 +823,400 @@
  done)%0A%09%7D)%0A%0A
+%09it('should accept logs by http', function (done) %7B%0A%09%09request(%7B%0A%09%09%09url: 'https://localhost:' + config.httpPort + '/log',%0A%09%09%09json: log,%0A%09%09%09method: 'POST',%0A%09%09%09ca: config.socket.cert,%0A%09%09%09auth: %7B%0A%09%09%09%09user: 'test',%0A%09%09%09%09pass: '',%0A%09%09%09%09sendImmediately: true%0A%09%09%09%7D%0A%09%09%7D, function (err, _, result) %7B%0A%09%09%09should(err).be.null%0A%09%09%09result.should.be.eql(%7B%0A%09%09%09%09ok: true%0A%09%09%09%7D)%0A%09%09%09done()%0A%09%09%7D)%0A%09%7D)%0A%0A
 %09it('should 
@@ -1269,16 +1269,39 @@
 og.find(
+%7B%0A%09%09%09origin: 'test'%0A%09%09%7D
 ).sort('
@@ -1313,17 +1313,17 @@
 ).limit(
-2
+3
 ).lean()
@@ -1373,44 +1373,27 @@
 %0A%09%09%09
-if (err) %7B%0A%09%09%09%09return done(err)%0A%09%09%09%7D
+should(err).be.null
 %0A%09%09%09
@@ -1416,17 +1416,17 @@
 .length(
-2
+3
 )%0A%09%09%09log
@@ -1453,16 +1453,50 @@
 ogs%5B1%5D)%0A
+%09%09%09logs%5B0%5D.should.be.eql(logs%5B2%5D)%0A
 %09%09%09logs%5B

Test for write api over http