func TestDeployListByName(t *testing.T) { deployment1, err := generateDeployment() if err != nil { t.Fatal(err) } deployment1.Deployment.Name = "test" deployment1.Deployment.Save() generateDeployment() if err != nil { t.Fatal(err) } key := mailbox.AccessKey{FullAccess: true} key.Create() req := api.DeploymentStatsRequest{ Count: 10, NamePattern: "t*t", } 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) != 1 { t.Fatalf("Deployment length %d != 1", len(resp.Deployments)) } }
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") } }
func TestDeployInfoListByToken(t *testing.T) { _, err := generateDeployment() key := mailbox.AccessKey{FullAccess: true} key.Create() if err != nil { t.Fatal(err) } dep2, err := generateDeployment() if err != nil { t.Fatal(err) } req := api.DeploymentStatsRequest{ Count: 2, TokenPattern: dep2.AccessKey.Name, } 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) != 1 { t.Fatalf("Deployment count %d!=1", len(resp.Deployments)) } }
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") } }
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) } }
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") } }
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) } }
func TestBadMailbox(t *testing.T) { key := mailbox.AccessKey{FullAccess: true} key.Create() req := api.GetMessageRequest{Mailbox: "111"} req.Sign(key.Name, key.Secret) var resp api.GetMessageResponse code := doRequest(t, req, &resp, "get") if code != 400 { t.Fatal("Should of responded with 400 but it responded with", code) } }
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.") } }
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) } }
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") } }
func TestAgentCommand(t *testing.T) { mailbox.OpenMemDB() key := mailbox.AccessKey{FullAccess: true} key.Create() agent.Address = ":6112" agent.AccessKey = key.Secret go agent.Start() engine.Agents["test"] = ":6112" engine.AgentAccessKey = key.Secret eng := engine.New() err := eng.Execute(`$agent("test", function() { console.log("test"); });`) if err != nil { t.Fatal(err) } }
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)) } }
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) } }
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") } }
func TestRegister(t *testing.T) { key := mailbox.AccessKey{FullAccess: true} key.Create() req := api.RegisterRequest{Mailbox: "register.test"} req.Sign(key.Name, key.Secret) var resp api.RegisterResponse code := doRequest(t, req, &resp, "register") if code != 200 { t.Fatalf("Server repsponded with %d", code) } mb, err := mailbox.Find("register.test") if err != nil { t.Fatal(err) } if mb == nil { t.Fatal("Mailbox not registered") } if !key.CanGet(mb) { t.Fatal("Key not bound to mailbox") } }
func TestAsset(t *testing.T) { mailbox.OpenMemDB() mailbox.CreateDB() mb, err := mailbox.Create("test.asset") 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) client, err := ClientFromConfig() if err != nil { t.Fatal(err) } resp, err := client.CheckRemoteFile("324e63777cae0113d708633836c9cb18") if err != nil { t.Fatal(err) } if resp != false { t.Fatal("Check file came back true before upload.") } os.Create("test.js") file, err := os.OpenFile("test.js", os.O_APPEND|os.O_WRONLY, 0644) file.WriteString("console.log('test');") file.Close() go serverCmd.Run(serverCmd, []string{}) deployCmd.ParseFlags([]string{"-x", "-a", "test.js"}) deployCmd.Run(deployCmd, []string{"test.js", "test.asset"}) defer os.Remove("test.js") resp, err = client.CheckRemoteFile("324e63777cae0113d708633836c9cb18") if err != nil { t.Fatal(err) } if resp != true { t.Fatal("File check came back false after upload") } fname, err := client.DownloadAsset("324e63777cae0113d708633836c9cb18") if err != nil { t.Fatal(err) } defer os.Remove(fname) fData, err := ioutil.ReadFile(fname) if err != nil { t.Fatal(err) } if string(fData) != "console.log('test');" { t.Fatal("File data is wrong") } }
) // accessCmd represents the access command var accessCmd = &cobra.Command{ Use: "access [name]", Short: "Generate an administrative access key the local server.", Long: `This generates and returns a administrative API access key for the local Conduit server. This key gives full access to the Conduit API and should be used for administrative purposes in a Conduit client or by an external system that can manage the Conduit service. For audit purposes the access key can be given a name. If no name is specified a randomly generated identifier will be used.`, Run: func(cmd *cobra.Command, args []string) { mailbox.OpenDB() key := mailbox.AccessKey{FullAccess: true} if len(args) > 0 { key.Name = args[0] } err := key.Create() if err != nil { log.Debug(err.Error()) log.Fatal("Could not create access key.") } log.Info("Access key created: ") log.Info(" Access key name: " + key.Name) log.Info(" Access key: " + key.Secret) }, } func init() {
func TestFileUpload(t *testing.T) { key := mailbox.AccessKey{FullAccess: true} key.Create() }