Ejemplo n.º 1
0
func TestTemplates(t *testing.T) {
	if true {
		// Temporarily disable test so TravisCI reports build success instead of test failure.
		return
	}

	cfgMap, err := test.LoadConfig()
	if err != nil {
		t.Error(err)
		return
	}
	cfg, err := sp.NewConfig(cfgMap)
	if err != nil {
		t.Error(err)
		return
	}

	var client sp.Client
	err = client.Init(cfg)
	if err != nil {
		t.Error(err)
		return
	}

	tlist, _, err := client.Templates()
	if err != nil {
		t.Error(err)
		return
	}

	t.Error(fmt.Errorf("%s", tlist))
	return

	content := sp.Content{
		Subject: "this is a test template",
		// NB: deliberate syntax error
		//Text: "text part of the test template {{a}",
		Text: "text part of the test template",
		From: map[string]string{
			"name":  "test name",
			"email": "*****@*****.**",
		},
	}
	template := &sp.Template{Content: content, Name: "test template"}

	id, _, err := client.TemplateCreate(template)
	if err != nil {
		t.Error(err)
		return
	}
	fmt.Printf("Created Template with id=%s\n", id)

	_, err = client.TemplateDelete(id)
	if err != nil {
		t.Error(err)
		return
	}
	fmt.Printf("Deleted Template with id=%s\n", id)
}
Ejemplo n.º 2
0
func TestAllEventsSamples(t *testing.T) {
	if true {
		// Temporarily disable test so TravisCI reports build success instead of test failure.
		return
	}

	cfgMap, err := test.LoadConfig()
	if err != nil {
		t.Error(err)
		return
	}
	cfg, err := sp.NewConfig(cfgMap)
	if err != nil {
		t.Error(err)
		return
	}

	var client sp.Client
	err = client.Init(cfg)
	if err != nil {
		t.Error(err)
		return
	}

	e, err := client.EventSamples(nil)
	if err != nil {
		t.Error(err)
		return
	}

	if len(*e) == 0 {
		t.Error("expected non-empty result")
	}

	for _, ev := range *e {
		switch event := ev.(type) {
		case *events.Click, *events.Open, *events.GenerationFailure, *events.GenerationRejection,
			*events.ListUnsubscribe, *events.LinkUnsubscribe, *events.PolicyRejection,
			*events.RelayInjection, *events.RelayRejection, *events.RelayDelivery,
			*events.RelayTempfail, *events.RelayPermfail, *events.SpamComplaint, *events.SMSStatus:
			if len(fmt.Sprintf("%v", event)) == 0 {
				t.Errorf("Empty output of %T.String()", event)
			}

		case *events.Bounce, *events.Delay, *events.Delivery, *events.Injection, *events.OutOfBand:
			if len(events.ECLog(event)) == 0 {
				t.Errorf("Empty output of %T.ECLog()", event)
			}

		case *events.Unknown:
			t.Errorf("Uknown type: %v", event)

		default:
			t.Errorf("Uknown type: %T", event)
		}
	}
}
Ejemplo n.º 3
0
func TestMessageEvents(t *testing.T) {
	if true {
		// Temporarily disable test so TravisCI reports build success instead of test failure.
		return
	}

	cfgMap, err := test.LoadConfig()
	if err != nil {
		t.Error(err)
		return
	}
	cfg, err := sp.NewConfig(cfgMap)
	if err != nil {
		t.Error(err)
		return
	}

	var client sp.Client
	err = client.Init(cfg)
	if err != nil {
		t.Error(err)
		return
	}

	//types := []string{"open", "click", "bounce"}
	//e, err := client.EventSamples(&types)
	e, err := client.EventSamples(nil)
	if err != nil {
		t.Error(err)
		return
	}

	for _, ev := range *e {
		//t.Error(fmt.Errorf("%s", ev))
		switch event := ev.(type) {
		case *events.Click, *events.Open, *events.GenerationFailure, *events.GenerationRejection,
			*events.ListUnsubscribe, *events.LinkUnsubscribe, *events.PolicyRejection,
			*events.RelayInjection, *events.RelayRejection, *events.RelayDelivery,
			*events.RelayTempfail, *events.RelayPermfail, *events.SpamComplaint:
			t.Error(fmt.Errorf("%s", event))

		case *events.Bounce, *events.Delay, *events.Delivery, *events.Injection, *events.OutOfBand:
			t.Error(fmt.Errorf("%s", events.ECLog(event)))

		default:
			t.Errorf("Unsupported type [%s]", reflect.TypeOf(ev))
		}
	}
}
Ejemplo n.º 4
0
func TestFilteredEventsSamples(t *testing.T) {
	if true {
		// Temporarily disable test so TravisCI reports build success instead of test failure.
		return
	}

	cfgMap, err := test.LoadConfig()
	if err != nil {
		t.Error(err)
		return
	}
	cfg, err := sp.NewConfig(cfgMap)
	if err != nil {
		t.Error(err)
		return
	}

	var client sp.Client
	err = client.Init(cfg)
	if err != nil {
		t.Error(err)
		return
	}

	types := []string{"open", "click", "bounce"}
	e, err := client.EventSamples(&types)
	if err != nil {
		t.Error(err)
		return
	}

	if len(*e) == 0 {
		t.Error("expected non-empty result")
	}

	for _, ev := range *e {
		switch event := ev.(type) {
		case *events.Click, *events.Open, *events.Bounce:
			// Expected, ok.
		default:
			t.Errorf("Unexpected type %T, should have been filtered out.", event)
		}
	}
}
Ejemplo n.º 5
0
func TestRecipients(t *testing.T) {
	if true {
		// Temporarily disable test so TravisCI reports build success instead of test failure.
		return
	}

	cfgMap, err := test.LoadConfig()
	if err != nil {
		t.Error(err)
		return
	}
	cfg, err := sp.NewConfig(cfgMap)
	if err != nil {
		t.Error(err)
		return
	}

	var client sp.Client
	err = client.Init(cfg)
	if err != nil {
		t.Error(err)
		return
	}

	list, _, err := client.RecipientLists()
	if err != nil {
		t.Error(err)
		return
	}

	strs := make([]string, len(*list))
	for idx, rl := range *list {
		strs[idx] = rl.String()
	}
	t.Errorf("%s\n", strings.Join(strs, "\n"))
}
Ejemplo n.º 6
0
func TestTransmissions(t *testing.T) {
	if true {
		// Temporarily disable test so TravisCI reports build success instead of test failure.
		return
	}

	cfgMap, err := test.LoadConfig()
	if err != nil {
		t.Error(err)
		return
	}
	cfg, err := sp.NewConfig(cfgMap)
	if err != nil {
		t.Error(err)
		return
	}

	var client sp.Client
	err = client.Init(cfg)
	if err != nil {
		t.Error(err)
		return
	}

	campaignID := "msys_smoke"
	tlist, res, err := client.Transmissions(&campaignID, nil)
	if err != nil {
		t.Error(err)
		return
	}
	t.Errorf("List: %d, %d entries", res.HTTP.StatusCode, len(tlist))
	for _, tr := range tlist {
		t.Errorf("%s: %s", tr.ID, tr.CampaignID)
	}

	// TODO: 404 from Transmission Create could mean either
	// Recipient List or Content wasn't found - open doc ticket
	// to make error message more specific

	T := &sp.Transmission{
		CampaignID: "msys_smoke",
		ReturnPath: "*****@*****.**",
		Recipients: []string{"*****@*****.**", "*****@*****.**"},
		// Single-recipient Transmissions are transient - Retrieve will 404
		//Recipients: []string{"*****@*****.**"},
		Content: sp.Content{
			Subject: "this is a test message",
			HTML:    "this is the <b>HTML</b> body of the test message",
			From: map[string]string{
				"name":  "Dave Gray",
				"email": "*****@*****.**",
			},
		},
		Metadata: map[string]interface{}{
			"binding": "example",
		},
	}
	err = T.Validate()
	if err != nil {
		t.Error(err)
		return
	}

	id, _, err := client.Send(T)
	if err != nil {
		t.Error(err)
		return
	}

	t.Errorf("Transmission created with id [%s]", id)

	tr, res, err := client.Transmission(id)
	if err != nil {
		t.Error(err)
		return
	}

	if res != nil {
		t.Errorf("Retrieve returned HTTP %s\n", res.HTTP.Status)
		if len(res.Errors) > 0 {
			for _, e := range res.Errors {
				json, err := e.Json()
				if err != nil {
					t.Error(err)
				}
				t.Errorf("%s\n", json)
			}
		} else {
			t.Errorf("Transmission retrieved: %s=%s\n", tr.ID, tr.State)
		}
	}

	res, err = client.TransmissionDelete(id)
	if err != nil {
		t.Error(err)
		return
	}

	t.Errorf("Delete returned HTTP %s\n%s\n", res.HTTP.Status, res.Body)

}
Ejemplo n.º 7
0
func TestTemplates(t *testing.T) {
	if true {
		// Temporarily disable test so TravisCI reports build success instead of test failure.
		// NOTE: need travis to set sparkpost base urls etc, or mock http request
		return
	}

	cfgMap, err := test.LoadConfig()
	if err != nil {
		t.Error(err)
		return
	}
	cfg, err := sp.NewConfig(cfgMap)
	if err != nil {
		t.Error(err)
		return
	}

	var client sp.Client
	err = client.Init(cfg)
	if err != nil {
		t.Error(err)
		return
	}

	tlist, _, err := client.Templates()
	if err != nil {
		t.Error(err)
		return
	}
	t.Logf("templates listed: %+v", tlist)

	content := sp.Content{
		Subject: "this is a test template",
		// NB: deliberate syntax error
		//Text: "text part of the test template {{a}",
		Text: "text part of the test template",
		From: map[string]string{
			"name":  "test name",
			"email": "*****@*****.**",
		},
	}
	template := &sp.Template{Content: content, Name: "test template"}

	id, _, err := client.TemplateCreate(template)
	if err != nil {
		t.Error(err)
		return
	}
	fmt.Printf("Created Template with id=%s\n", id)

	d := map[string]interface{}{}
	res, err := client.TemplatePreview(id, &sp.PreviewOptions{d})
	if err != nil {
		t.Error(err)
		return
	}
	fmt.Printf("Preview Template with id=%s and response %+v\n", id, res)

	_, err = client.TemplateDelete(id)
	if err != nil {
		t.Error(err)
		return
	}
	fmt.Printf("Deleted Template with id=%s\n", id)
}