1// Copyright 2011 Google Inc. All rights reserved.
2// Use of this source code is governed by the Apache 2.0
3// license that can be found in the LICENSE file.
4
5package mail
6
7import (
8	"testing"
9
10	"github.com/golang/protobuf/proto"
11
12	"google.golang.org/appengine/internal/aetesting"
13	basepb "google.golang.org/appengine/internal/base"
14	pb "google.golang.org/appengine/internal/mail"
15)
16
17func TestMessageConstruction(t *testing.T) {
18	var got *pb.MailMessage
19	c := aetesting.FakeSingleContext(t, "mail", "Send", func(in *pb.MailMessage, out *basepb.VoidProto) error {
20		got = in
21		return nil
22	})
23
24	msg := &Message{
25		Sender: "dsymonds@example.com",
26		To:     []string{"nigeltao@example.com"},
27		Body:   "Hey, lunch time?",
28		Attachments: []Attachment{
29			// Regression test for a prod bug. The address of a range variable was used when
30			// constructing the outgoing proto, so multiple attachments used the same name.
31			{
32				Name:      "att1.txt",
33				Data:      []byte("data1"),
34				ContentID: "<att1>",
35			},
36			{
37				Name: "att2.txt",
38				Data: []byte("data2"),
39			},
40		},
41	}
42	if err := Send(c, msg); err != nil {
43		t.Fatalf("Send: %v", err)
44	}
45	want := &pb.MailMessage{
46		Sender:   proto.String("dsymonds@example.com"),
47		To:       []string{"nigeltao@example.com"},
48		Subject:  proto.String(""),
49		TextBody: proto.String("Hey, lunch time?"),
50		Attachment: []*pb.MailAttachment{
51			{
52				FileName:  proto.String("att1.txt"),
53				Data:      []byte("data1"),
54				ContentID: proto.String("<att1>"),
55			},
56			{
57				FileName: proto.String("att2.txt"),
58				Data:     []byte("data2"),
59			},
60		},
61	}
62	if !proto.Equal(got, want) {
63		t.Errorf("Bad proto for %+v\n got %v\nwant %v", msg, got, want)
64	}
65}
66