Exemple #1
0
func TestPut(t *testing.T) {
	mb1, err := mailbox.Create("put1")
	if err != nil {
		t.Fatal(err)
	}
	mb2, err := mailbox.Create("put2")
	if err != nil {
		t.Fatal(err)
	}
	accessKey := mailbox.AccessKey{FullAccess: true}
	accessKey.Create()
	req := api.PutMessageRequest{
		Mailboxes: []string{
			mb1.Id,
			mb2.Id,
		},
		Body: "TEST",
	}
	req.Sign(accessKey.Name, accessKey.Secret)
	var resp api.PutMessageResponse
	code := doRequest(t, req, &resp, "put")
	count, err := mb1.MessageCount()
	if err != nil {
		t.Fatal(err)
	}
	if count == 0 {
		t.Fatal("Message not added to mailbox")
	}
	count, err = mb2.MessageCount()
	if err != nil {
		t.Fatal(err)
	}
	if count == 0 {
		t.Fatal("Message not added to mailbox")
	}
	if code != 200 {
		t.Fatal("Server responded with", code)
	}
	message1, err := mb1.GetMessage()
	if err != nil {
		t.Fatal(err)
	}
	message2, err := mb2.GetMessage()
	if err != nil {
		t.Fatal(err)
	}
	if message1 == nil || message2 == nil {
		t.Fatal("Message is nil")
	}
	if message1.Body != "TEST" {
		t.Fatal("Incorrect message1 body", message1.Body)
	}
	if message2.Body != "TEST" {
		t.Fatal("Incorrect message2 body", message2.Body)
	}
}
Exemple #2
0
// TestClientPut checks to make sure the client is capable of sending messages
// to a given mailbox.
func TestClientPut(t *testing.T) {
	mb1, _ := mailbox.Create("put1")
	mb2, _ := mailbox.Create("put2")
	_, err := pmClient.Put([]string{mb1.Id, mb2.Id}, "", "PUT TEST", "", "")
	if err != nil {
		t.Fatal(err)
	}
	count1, _ := mb1.MessageCount()
	count2, _ := mb2.MessageCount()
	if count1 != 1 || count2 != 1 {
		t.Fatal("Message counts are ", count1, ",", count2)
	}
}
Exemple #3
0
func TestRun(t *testing.T) {
	mb, err := mailbox.Create("test.testrun")
	if err != nil {
		t.Fatal(err)
	}
	key := mailbox.AccessKey{MailboxId: mb.Id}
	err = key.Create()
	if err != nil {
		t.Fatal(err)
	}
	msg, err := mb.PutMessage(`$("test");`)
	if err != nil {
		t.Fatal(err)
	}
	viper.Set("host", ":5112")
	viper.Set("mailbox", mb.Id)
	viper.Set("access_key", key.Secret)
	viper.Set("access_key_name", key.Name)
	viper.Set("show_requests", true)
	go serverCmd.Run(serverCmd, []string{})
	runCmd.ParseFlags([]string{"-1", "-d"})
	runCmd.Run(runCmd, []string{})
	dep, _ := mailbox.FindDeployment(msg.Deployment)
	resp, _ := dep.GetResponses()
	if len(resp) != 1 {
		t.Fatal("No response")
	}
}
Exemple #4
0
func TestResponse(t *testing.T) {
	mb, err := mailbox.Create("deployment.response")
	if err != nil {
		t.Fatal(err)
	}
	dep := &mailbox.Deployment{
		Name:        "dep",
		DeployedBy:  accessKey.Name,
		MessageBody: "testMessage",
	}
	err = dep.Create()
	if err != nil {
		t.Fatal(err)
	}
	msg, err := mb.DeployMessage(dep.Id)
	if err != nil {
		t.Fatal(err)
	}
	err = pmClient.Respond(msg.Id, "testing repsonse", false)
	if err != nil {
		t.Fatal(err)
	}

	responses, err := dep.GetResponses()
	if err != nil {
		t.Fatal(err)
	}

	if len(responses) == 0 {
		t.Fatal("Response was not added")
	}
}
Exemple #5
0
func generateDeployment() (*TestDeployment, error) {
	mb, err := mailbox.Create(mailbox.GenerateIdentifier())
	if err != nil {
		return nil, err
	}
	accessKey := &mailbox.AccessKey{MailboxId: mb.Id}
	err = accessKey.Create()
	if err != nil {
		return nil, err
	}
	deployment := &mailbox.Deployment{
		MessageBody: mailbox.GenerateIdentifier(),
		Name:        mailbox.GenerateIdentifier(),
		DeployedBy:  accessKey.Name,
	}
	err = deployment.Create()
	if err != nil {
		return nil, err
	}
	msg, err := deployment.Deploy(mb)
	if err != nil {
		return nil, err
	}
	return &TestDeployment{
		AccessKey:  accessKey,
		Deployment: deployment,
		Mailbox:    mb,
		Message:    msg,
	}, nil
}
Exemple #6
0
func TestDeploymentDetail(t *testing.T) {
	mb, err := mailbox.Create("deployment.detail")
	if err != nil {
		t.Fatal(err)
	}
	dep := mailbox.Deployment{MessageBody: "test message"}
	err = dep.Create()
	if err != nil {
		t.Fatal(err)
	}
	_, err = mb.DeployMessage(dep.Id)
	if err != nil {
		t.Fatal(err)
	}
	err = dep.AddResponse(mb.Id, "test repsonse", false)
	if err != nil {
		t.Fatal(err)
	}
	pmClient.Mailbox = mb.Id
	resp, err := pmClient.DeploymentDetail(dep.Id)
	if err != nil {
		t.Fatal(err)
	}
	if len(resp.Deployments) == 0 {
		t.Fatal("No deployments returned")
	}
	if len(resp.Deployments[0].Responses) == 0 {
		t.Fatal("No deployment responses returned")
	}
}
Exemple #7
0
func TestListDeploys(t *testing.T) {
	mb, err := mailbox.Create("deployment.list")
	if err != nil {
		t.Fatal(err)
	}
	dep := &mailbox.Deployment{
		Name:        "dep",
		DeployedBy:  accessKey.Name,
		MessageBody: "test message",
	}
	err = dep.Create()
	if err != nil {
		t.Fatal(err)
	}
	_, err = mb.DeployMessage(dep.Id)
	if err != nil {
		t.Fatal(err)
	}
	pmClient.Mailbox = mb.Id
	resp, err := pmClient.ListDeploys(".*", false, 10)
	if err != nil {
		t.Fatal(err)
	}
	if len(resp.Deployments) == 0 {
		t.Fatal("No deployments returned")
	}
}
Exemple #8
0
func TestMain(m *testing.M) {
	// open database in memory for testing
	mailbox.OpenMemDB()
	err := mailbox.CreateDB()
	if err != nil {
		panic(err)
	}

	// create a default mailbox to use
	mb, err = mailbox.Create("mb")
	if err != nil {
		panic(err)
	}

	// create an access token for the default mailbox
	accessKey = &mailbox.AccessKey{FullAccess: true}
	accessKey.Create()

	// create a postmasterClient
	pmClient = client.Client{
		Host:          "localhost:4111",
		Mailbox:       mb.Id,
		AccessKeyName: accessKey.Name,
		AccessKey:     accessKey.Secret,
	}

	// Start up a test server to use
	server.EnableLongPolling = false
	go server.Start(":4111")
	retCode := m.Run()

	// cleanup
	mailbox.CloseDB()
	os.Exit(retCode)
}
Exemple #9
0
func TestAddResponse(t *testing.T) {
	mb, err := mailbox.Create("tests.AddResponses")
	if err != nil {
		t.Fatal(err)
	}
	key := mailbox.AccessKey{FullAccess: true}
	key.Create()
	if err != nil {
		t.Fatal(err)
	}
	dep := mailbox.Deployment{MessageBody: "test", DeployedBy: key.Name}
	err = dep.Create()
	if err != nil {
		t.Fatal(err)
	}
	msg, err := mb.DeployMessage(dep.Id)
	if err != nil {
		t.Fatal(err)
	}
	err = dep.AddResponse(msg.Id, "response", false)
	if err != nil {
		t.Fatal(err)
	}
	responses, err := dep.GetResponses()
	if len(responses) == 0 {
		t.Fatal("Deployment has no responses")
	}
}
Exemple #10
0
func TestDelete(t *testing.T) {
	mb, err := mailbox.Create("delete")
	if err != nil {
		t.Fatal(err)
	}

	msg, err := mb.PutMessage("TEST")
	if err != nil {
		t.Fatal(err)
	}

	key := mailbox.AccessKey{MailboxId: mb.Id}
	key.Create()
	req := api.DeleteMessageRequest{Message: msg.Id}
	req.Sign(key.Name, key.Secret)
	resp := api.DeleteMessageResponse{}

	statusCode := doRequest(t, req, &resp, "delete")
	if statusCode != 200 {
		t.Fatal("Server responded with", statusCode)
	}

	count, err := mb.MessageCount()
	if err != nil {
		t.Fatal(err)
	}

	if count != 0 {
		t.Fatal("Message count should be 0 but is", count)
	}
}
Exemple #11
0
func TestMailboxKeyName(t *testing.T) {
	mb, _ := mailbox.Create("tokentest.admin")
	key := mailbox.AccessKey{MailboxId: mb.Id}
	key.Create()
	if key.Name != mb.Id {
		t.Fatal("Key name should be set to mailbox name")
	}
}
Exemple #12
0
func TestCreateMailboxWithKeyName(t *testing.T) {
	key := &mailbox.AccessKey{Name: "dupmbtest", FullAccess: true}
	key.Create()
	_, err := mailbox.Create("dupmbtest")
	if err != nil {
		t.Fatal("Should not create mailbox with key name")
	}
}
Exemple #13
0
// TestClientDelete checks to make sure the client is capable of deleting
// messages.
func TestClientDelete(t *testing.T) {
	mb, _ := mailbox.Create("delete")
	msg, _ := mb.PutMessage("TEST DELETE")
	pmClient.Delete(msg.Id)
	count, _ := mb.MessageCount()
	if count != 0 {
		t.Fatal("Message count is", count)
	}
}
Exemple #14
0
func TestCreateDupKey(t *testing.T) {
	mailbox.Create("dupkeytest")
	mbKey := &mailbox.AccessKey{MailboxId: "dupkeytest"}
	mbKey.Create()
	key := &mailbox.AccessKey{Name: "dupkeytest", FullAccess: true}
	err := key.Create()
	if err == nil {
		t.Fatal("Should not create key with the same name as a mailbox")
	}
}
Exemple #15
0
// TestNoMessage checks that a mailbox should respond with an empty response if
// there are no messages in the queue.
func TestNoMessages(t *testing.T) {
	mb, _ = mailbox.Create("empty")
	pmClient.Mailbox = mb.Id
	resp, err := pmClient.Get()
	if err != nil {
		t.Fatal(err)
	}
	if !resp.IsEmpty() {
		t.Fatal("Response body is not empty")
	}
}
Exemple #16
0
func TestSystemStats(t *testing.T) {
	mb, _ = mailbox.Create("stats.system")
	mb.PutMessage("test")
	resp, err := pmClient.Stats()
	if err != nil {
		t.Fatal(err)
	}
	if resp.PendingMessages == 0 {
		t.Fatal("Pending message count should not be 0")
	}
}
Exemple #17
0
// register is used by administrative clients to reigster new mailboxes.
func register(w http.ResponseWriter, r *http.Request) {
	var request api.RegisterRequest
	err := readRequest(r, &request)
	if err != nil {
		sendError(w, err.Error())
		return
	}

	accessKey, err := mailbox.FindKeyByName(request.AccessKeyName)
	if accessKey == nil {
		sendError(w, "Access key is invalid")
		return
	}

	if !request.Validate(accessKey.Secret) {
		sendError(w, "Could not validate signature")
		return
	}

	if !accessKey.CanAdmin() {
		sendError(w, "Not allowed to register mailboxes.")
		return
	}

	if mailbox.KeyExists(request.Mailbox) {
		sendError(w, "An access key already exists with that mailbox name")
		return
	}

	mb, err := mailbox.Create(request.Mailbox)
	if err != nil {
		sendError(w, err.Error())
		return
	}

	mbKey := &mailbox.AccessKey{
		MailboxId: mb.Id,
	}

	err = mbKey.Create()
	if err != nil {
		sendError(w, err.Error())
		return
	}

	resp := api.RegisterResponse{
		Mailbox:         mb.Id,
		AccessKeyName:   mbKey.Name,
		AccessKeySecret: mbKey.Secret,
	}
	resp.Sign(accessKey.Name, accessKey.Secret)
	writeResponse(&w, resp)
	log.Infof("Mailbox %s registered.", mb.Id)
}
Exemple #18
0
func TestCreateMailboxKey(t *testing.T) {
	mb, _ := mailbox.Create("tokentest.mailboxToken")
	key := &mailbox.AccessKey{MailboxId: mb.Id}
	err := key.Create()
	if err != nil {
		t.Fatal(err)
	}
	if !key.CanGet(mb) {
		t.Fatal("Mailbox token not able to retrieve messages.")
	}
}
Exemple #19
0
func TestCreateAPIToken(t *testing.T) {
	mb, _ := mailbox.Create("tokentest.admin")
	key := mailbox.AccessKey{FullAccess: true}
	err := key.Create()
	if err != nil {
		t.Fatal(err)
	}
	if !key.CanPut(mb) {
		t.Fatal("Admin token not able to put messages.")
	}
}
Exemple #20
0
func TestDeregisterMailbox(t *testing.T) {
	mb, _ := mailbox.Create("test.deregister")
	_, err := pmClient.DeregisterMailbox(mb.Id)
	if err != nil {
		t.Fatal(err)
	}
	_mb, _ := mailbox.Find(mb.Id)
	if _mb != nil {
		t.Fatal("Mailbox not removed")
	}

}
Exemple #21
0
func TestSystemStats(t *testing.T) {
	mailbox.Create("stats.systemtest")
	req := api.SimpleRequest{}
	accessKey := mailbox.AccessKey{FullAccess: true}
	accessKey.Create()
	req.Sign(accessKey.Name, accessKey.Secret)
	var resp api.SystemStatsResponse
	code := doRequest(t, req, &resp, "stats")
	if code != 200 {
		t.Fatal("Server responded with", code)
	}
}
Exemple #22
0
func TestSearch(t *testing.T) {
	_, err := mailbox.Create("org.Test")
	if err != nil {
		t.Fatal(err)
	}
	results, err := mailbox.Search("Org.*")
	if err != nil {
		t.Fatal(err)
	}
	if len(results) == 0 {
		t.Fatal("Mailbox not found.")
	}
}
Exemple #23
0
func TestServerPurge(t *testing.T) {
	mb, _ := mailbox.Create("purge.test")
	msg, _ := mb.PutMessage("tesT")
	if msg == nil {
		t.Fatal("Message not deployed")
	}
	mailbox.CloseDB()
	purgeCmd.Run(purgeCmd, []string{"purge.test"})
	msg, _ = mb.GetMessage()
	if msg != nil {
		t.Fatal("Message was not purged")
	}
}
Exemple #24
0
func TestGetBadSignature(t *testing.T) {
	mb, err := mailbox.Create("get.badtoken")
	if err != nil {
		t.Fatal(err)
	}
	mb.PutMessage("TEST")
	req := api.GetMessageRequest{Mailbox: mb.Id}
	var resp api.GetMessageResponse
	code := doRequest(t, req, &resp, "get")
	if code == 200 {
		t.Fatal("Bad token should respond with an error")
	}
}
Exemple #25
0
func TestPutBadToken(t *testing.T) {
	mb, _ := mailbox.Create("puttest.badtoken")
	accessKey := mailbox.AccessKey{MailboxId: mb.Id}
	accessKey.Create()
	req := api.PutMessageRequest{
		Mailboxes: []string{mb.Id},
		Body:      "TEST MESSAGE",
	}
	req.Sign(accessKey.Name, accessKey.Secret)
	var resp api.PutMessageResponse
	code := doRequest(t, req, &resp, "put")
	if code == 200 {
		t.Fatal("Bad token should return error")
	}
}
Exemple #26
0
func TestAutoCreateDeploy(t *testing.T) {
	mb, _ := mailbox.Create("put.autocreate.deploy")
	msg, err := pmClient.Put([]string{mb.Id}, "", "TEST MESSAGE", "blah", "")
	if err != nil {
		t.Fatal(err)
	}
	if msg.Deployment == "" {
		t.Fatal("Deployment is empty")
	}
	dep, err := mailbox.FindDeployment(msg.Deployment)
	if err != nil {
		t.Fatal(err)
	}
	if dep.Name != "blah" {
		t.Fatal("Deployment name not set")
	}
}
Exemple #27
0
func TestDeployInfoList(t *testing.T) {
	mb, _ := mailbox.Create("stats.deployinfo")
	mb.PutMessage("test")
	mb.PutMessage("test2")
	key := mailbox.AccessKey{FullAccess: true}
	key.Create()
	req := api.DeploymentStatsRequest{Count: 2}
	req.Sign(key.Name, key.Secret)
	var resp api.DeploymentStatsResponse
	code := doRequest(t, req, &resp, "deploy/list")
	if code != 200 {
		t.Fatalf("Server repsponded with %d", code)
	}
	if len(resp.Deployments) != 2 {
		t.Fatalf("Deployment count %d!=2", len(resp.Deployments))
	}
}
Exemple #28
0
func TestPutByPattern(t *testing.T) {
	mb, _ := mailbox.Create("PATTERN")
	accessKey := mailbox.AccessKey{FullAccess: true}
	accessKey.Create()
	req := api.PutMessageRequest{Pattern: "P*"}
	req.Sign(accessKey.Name, accessKey.Secret)
	var resp api.PutMessageResponse
	code := doRequest(t, req, &resp, "put")
	count, err := mb.MessageCount()
	if err != nil {
		t.Fatal(err)
	}
	if count == 0 {
		t.Fatal("Message not added to mailbox")
	}
	if code != 200 {
		t.Fatal("Server responded with", code)
	}
}
Exemple #29
0
func TestDeploy(t *testing.T) {
	mailbox.OpenMemDB()
	mailbox.CreateDB()
	os.Create("test.js")
	file, err := os.OpenFile("test.js", os.O_APPEND|os.O_WRONLY, 0644)
	file.WriteString("console.log('test');")
	file.Close()

	mb, err := mailbox.Create("test.test")
	if err != nil {
		t.Fatal(err)
	}
	if err != nil {
		t.Fatal(err)
	}

	key := mailbox.AccessKey{FullAccess: true}
	err = key.Create()
	if err != nil {
		t.Fatal(err)
	}

	viper.Set("host", ":5112")
	viper.Set("mailbox", mb.Id)
	viper.Set("access_key", key.Secret)
	viper.Set("access_key_name", key.Name)
	viper.Set("show_requests", true)

	go serverCmd.Run(serverCmd, []string{})
	deployCmd.ParseFlags([]string{"-x"})
	deployCmd.Run(deployCmd, []string{"test.js", "test.test"})

	os.Remove("test.js")

	msg, err := mb.GetMessage()
	if err != nil {
		t.Fatal(err)
	}
	if msg == nil {
		t.Fatal("No message waiting")
	}
}
Exemple #30
0
	"conduit/log"
	"github.com/spf13/cobra"
	"postmaster/mailbox"
)

// registerCmd represents the register command
var serverRegisterCmd = &cobra.Command{
	Use:   "register [name]",
	Short: "Register a new mailbox",
	Long:  `This registers a new mailbox for the local server.`,
	Run: func(cmd *cobra.Command, args []string) {
		if len(args) == 0 {
			log.Fatal("No mailbox name specified")
		}
		mailbox.OpenDB()
		mb, err := mailbox.Create(args[0])
		if err != nil {
			log.Debug(err.Error())
			log.Fatal("Could not create mailbox")
		}
		key := &mailbox.AccessKey{MailboxId: mb.Id}
		err = key.Create()
		if err != nil {
			log.Debug(err.Error())
			log.Fatal("Could not create mailbox access token")
		}
		log.Infof("Mailbox created: %s", mb.Id)
		log.Infof("Access key created: %s", key.Secret)
	},
}