summaryrefslogtreecommitdiffstats
path: root/SendGrid/Tests/Transport/TestWebApi.cs
blob: ecb1ad8eeb6c7c80a21218ae3fd51e31ac863339 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using Moq;
using NUnit.Framework;
using SendGrid;

namespace Transport
{
    [TestFixture]
    internal class TestWebApi
    {
        private const string TestUsername = "username";
        private const string TestPassword = "password";
        private const string TestApiKey = "apikey";

        [Test]
        public void TestFetchFileBodies()
        {
            TestFetchFileBodiesHelper(new Web(new NetworkCredential(TestUsername, TestPassword)));
            TestFetchFileBodiesHelper(new Web(TestApiKey));
        }

        public void TestFetchFileBodiesHelper(Web webApi)
        {
            // Test using credentials
            var message = new Mock<ISendGrid>();
            var attachments = new[] {"foo", "bar", "foobar"};
            message.SetupProperty(foo => foo.Attachments, null);
            var result = webApi.FetchFileBodies(message.Object);
            Assert.AreEqual(0, result.Count);

            message.SetupProperty(foo => foo.Attachments, attachments);
            result = webApi.FetchFileBodies(message.Object);
            Assert.AreEqual(attachments.Count(), result.Count);
            for (var index = 0; index < attachments.Length; index++)
                Assert.AreEqual(result[index].Value.Name, attachments[index]);
        }

        [Test]
        public void TestFetchFormParams()
        {
            TestFetchFormParamsHelper(new Web(new NetworkCredential(TestUsername, TestPassword)), true);
            TestFetchFormParamsHelper(new Web(TestApiKey), false);
        }

        public void TestFetchFormParamsHelper(Web webApi, bool credentials)
        {
            // Test Variables
            const string toAddress = "foobar@outlook.com";
            const string ccAddress = "cc@outlook.com";
            const string bcc1Address = "bcc1@outlook.com";
            const string bcc2Address = "bcc2@outlook.com";
            MailAddress[] bccAddresses = {new MailAddress(bcc1Address), new MailAddress(bcc2Address)};
            const string fromAddress = "test@outlook.com";
            const string subject = "Test Subject";
            const string textBody = "Test Text Body";
            const string htmlBody = "<p>Test HTML Body</p>";
            const string headerKey = "headerkey";
            var testHeader = new Dictionary<string, string> { { headerKey, "headervalue" } };
            const string categoryName = "Example Category";

            var message = new SendGridMessage();
            message.AddTo(toAddress);
            message.AddCc(ccAddress);
            message.Bcc = bccAddresses;
            message.From = new MailAddress(fromAddress);
            message.Subject = subject;
            message.Text = textBody;
            message.Html = htmlBody;
            message.AddHeaders(testHeader);
            message.Header.SetCategory(categoryName);

            var result = webApi.FetchFormParams(message);
            if (credentials)
            {
                Assert.True(result.Any(r => r.Key == "api_user" && r.Value == TestUsername));
                Assert.True(result.Any(r => r.Key == "api_key" && r.Value == TestPassword));
            }
            Assert.True(result.Any(r => r.Key == "to[]" && r.Value == toAddress));
            Assert.True(result.Any(r => r.Key == "cc[]" && r.Value == ccAddress));
            Assert.True(result.Any(r => r.Key == "bcc[]" && r.Value == bcc1Address));
            Assert.True(result.Any(r => r.Key == "bcc[]" && r.Value == bcc2Address));
            Assert.True(result.Any(r => r.Key == "from" && r.Value == fromAddress));
            Assert.True(result.Any(r => r.Key == "subject" && r.Value == subject));
            Assert.True(result.Any(r => r.Key == "text" && r.Value == textBody));
            Assert.True(result.Any(r => r.Key == "html" && r.Value == htmlBody));
            Assert.True(
                result.Any(
                    r => r.Key == "headers" && r.Value == String.Format("{{\"{0}\":\"{1}\"}}", headerKey, testHeader[headerKey])));
            Assert.True(
                result.Any(r => r.Key == "x-smtpapi" && r.Value == String.Format("{{\"category\" : \"{0}\"}}", categoryName)));
        }
    }
}