package main

import (
	"encoding/json"
	"os"
	"testing"
	"time"
)

func TestCRMAttachFile(t *testing.T) {
	path1 := "media_for_test/200x200.png"
	path2 := "/tmp/wechat_hitxy_测试_" + time.Now().Format("2006-Jan-02-05-04-05")
	r, e := crmCreateAttachment("media_for_test/200x200.png")

	AssertEqual(t, e, nil, "should be no error when create attachment")
	AssertEqual(t, r.ID != "", true, "ID should not be empty ")
	//
	crmDownloadAttachmentAs(r.ID, path2)

	AssertEqual(t, md5CompFile(path1, path2), true, "attachment should be downloade as same file ")

	delete, e := crmDeleteEntity("Attachment", r.ID)
	AssertEqual(t, delete, true, "temp attachment should have been deleted")

	os.Remove(path2)
}

func TestDecodeJsonResponse(t *testing.T) {
	msg := `
{"id":"591e55398345683ee","name":"static_qr_code_without_logo.png","deleted":false,"type":"image\/png"
,"size":509,"sourceId":null,"createdAt":"2017-05-19 02:15:21","role":"Attachment","storage":null,"storageFilePath"
:null,"global":false,"parentId":null,"parentType":null,"relatedId":null,"relatedType":null,"createdById"
:"1","createdByName":"Admin"}	`
	info := crmdAttachment{}
	err := json.Unmarshal([]byte(msg), &info)
	AssertEqual(t, err, nil, "json decode shold be correct")
}
