Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Microsoft.Bot.Connector.Tests project #7

Merged
merged 1 commit into from
Jan 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
346 changes: 165 additions & 181 deletions tests/Microsoft.Bot.Connector.Tests/AttachmentsTests.cs
Original file line number Diff line number Diff line change
@@ -1,181 +1,165 @@
//namespace Connector.Tests
//{
// using System;
// using System.IO;
// using Microsoft.Bot.Connector;

// using Microsoft.Rest;
// using Xunit;

// public class AttachmentsTests : BaseTest
// {
// protected const string conversationId = "B21UTEF8S:T03CWQ0QB:D2369CT7C";

// [Fact]
// public void UploadAttachmentAndGetAttachment()
// {
// UseClientFor(async client =>
// {
// var attachment = new AttachmentData("image/png", ReadFile("bot.png"), "Bot.png", ReadFile("bot_icon.png"));
// var response = await client.Conversations.UploadAttachmentAsync(conversationId, attachment);
// var attachmentId = response.Id;
// var attachmentInfo = await client.Attachments.GetAttachmentInfoAsync(attachmentId);

// Assert.NotNull(attachmentInfo);
// Assert.Equal("Bot.png", attachmentInfo.Name);
// Assert.Equal("image/png", attachmentInfo.Type);
// Assert.Equal(2, attachmentInfo.Views.Count);
// });
// }

// [Fact]
// public void UploadAttachmentWithoutOriginalFails()
// {
// UseClientFor(async client =>
// {
// var attachment = new AttachmentData()
// {
// Name = "Bot.png",
// Type = "image/png"
// };

// var ex = await Assert.ThrowsAsync<ValidationException>(() => client.Conversations.UploadAttachmentAsync(conversationId, attachment));
// Assert.Contains("OriginalBase64", ex.Message);
// });
// }

// [Fact]
// public void UploadAttachmentWithoutTypeFails()
// {
// UseClientFor(async client =>
// {
// var attachment = new AttachmentData()
// {
// Name = "Bot.png",
// OriginalBase64 = ReadFile("Bot.png")
// };

// var ex = await Assert.ThrowsAsync<ValidationException>(() => client.Conversations.UploadAttachmentAsync(conversationId, attachment));
// Assert.Contains("Type", ex.Message);
// });
// }

// [Fact]
// public void UploadAttachmentWithNullConversationId()
// {
// UseClientFor(async client =>
// {
// var attachment = new AttachmentData("image/png", ReadFile("bot.png"), "Bot.png", ReadFile("bot_icon.png"));

// var ex = await Assert.ThrowsAsync<ValidationException>(() => client.Conversations.UploadAttachmentAsync(null, attachment));
// Assert.Contains("cannot be null", ex.Message);
// });
// }

// [Fact]
// public void UploadAttachmentWithNullAttachment()
// {
// UseClientFor(async client =>
// {
// var ex = await Assert.ThrowsAsync<ValidationException>(() => client.Conversations.UploadAttachmentAsync(conversationId, null));
// Assert.Contains("cannot be null", ex.Message);
// });
// }

// [Fact]
// public void GetAttachmentInfoWithInvalidIdFails()
// {
// UseClientFor(async client =>
// {
// var ex = await Assert.ThrowsAsync<ErrorResponseException>(() => client.Attachments.GetAttachmentInfoAsync("bt13796-GJS4yaxDLI"));
// Assert.Contains("NotFound", ex.Message);
// });
// }

// [Fact]
// public void GetAttachmentInfoWithNullIdFails()
// {
// UseClientFor(async client =>
// {
// var ex = await Assert.ThrowsAsync<ValidationException>(() => client.Attachments.GetAttachmentInfoAsync(null));
// Assert.Contains("cannot be null", ex.Message);
// });
// }

// [Fact]
// public void GetAttachmentView()
// {
// UseClientFor(async client =>
// {
// var attachment = new AttachmentData("image/png", ReadFile("bot.png"), "Bot.png", ReadFile("bot_icon.png"));
// var response = await client.Conversations.UploadAttachmentAsync(conversationId, attachment);
// var attachmentId = response.Id;
// var stream = await client.Attachments.GetAttachmentAsync(attachmentId, "original");

// // Workaround for TestFramework not saving/replaying binary content
// // Instead, convert the expected output the same way that the TestRecorder converts binary content to string
// var expectedAsString = new StreamReader(new MemoryStream(attachment.OriginalBase64)).ReadToEnd();
// var actualAsString = new StreamReader(stream).ReadToEnd();

// Assert.Equal(expectedAsString, actualAsString);
// });
// }

// [Fact]
// public void GetAttachmentViewWithInvalidAttachmentIdFails()
// {
// UseClientFor(async client =>
// {
// var ex = await Assert.ThrowsAsync<HttpOperationException>(() => client.Attachments.GetAttachmentAsync("bt13796-GJS4yaxDLI", "original"));
// Assert.Contains("NotFound", ex.Message);
// });
// }

// [Fact]
// public void GetAttachmentViewWithNullAttachmentIdFails()
// {

// UseClientFor(async client =>
// {
// var ex = await Assert.ThrowsAsync<ValidationException>(() => client.Attachments.GetAttachmentAsync(null, "original"));
// Assert.Contains("cannot be null", ex.Message);
// });
// }

// [Fact]
// public void GetAttachmentViewWithInvalidViewIdFails()
// {

// UseClientFor(async client =>
// {
// var attachment = new AttachmentData("image/png", ReadFile("bot.png"), "Bot.png", ReadFile("bot_icon.png"));
// var response = await client.Conversations.UploadAttachmentAsync(conversationId, attachment);

// var ex = await Assert.ThrowsAsync<HttpOperationException>(() => client.Attachments.GetAttachmentAsync(response.Id, "invalid"));

// Assert.Contains("NotFound", ex.Message);
// });
// }

// [Fact]
// public void GetAttachmentViewWithNullViewIdFails()
// {

// UseClientFor(async client =>
// {
// var attachment = new AttachmentData("image/png", ReadFile("bot.png"), "Bot.png", ReadFile("bot_icon.png"));
// var response = await client.Conversations.UploadAttachmentAsync(conversationId, attachment);

// var ex = await Assert.ThrowsAsync<ValidationException>(() => client.Attachments.GetAttachmentAsync(response.Id, null));

// Assert.Contains("cannot be null", ex.Message);
// });
// }

// private byte[] ReadFile(string fileName)
// {
// var path = Path.Combine(Directory.GetCurrentDirectory(), "resources", fileName);
// return File.ReadAllBytes(path);
// }
// }
//}
namespace Connector.Tests
{
using System.IO;
using Microsoft.Bot.Connector;

using Microsoft.Rest;
using Xunit;

public class AttachmentsTests : BaseTest
{
protected const string conversationId = "B21UTEF8S:T03CWQ0QB:D2369CT7C";

[Fact]
public void UploadAttachmentAndGetAttachment()
{
UseClientFor(async client =>
{
var attachment = new AttachmentData("image/png", "Bot.png", ReadFile("bot.png"), ReadFile("bot_icon.png"));
var response = await client.Conversations.UploadAttachmentAsync(conversationId, attachment);
var attachmentId = response.Id;
var attachmentInfo = await client.Attachments.GetAttachmentInfoAsync(attachmentId);

Assert.NotNull(attachmentInfo);
Assert.Equal("Bot.png", attachmentInfo.Name);
Assert.Equal("image/png", attachmentInfo.Type);
Assert.Equal(2, attachmentInfo.Views.Count);
});
}

[Fact]
public void UploadAttachmentWithoutOriginalFails()
{
UseClientFor(async client =>
{
var attachment = new AttachmentData()
{
Name = "Bot.png",
Type = "image/png"
};

var ex = await Assert.ThrowsAsync<ErrorResponseException>(() => client.Conversations.UploadAttachmentAsync(conversationId, attachment));
Assert.Equal("MissingProperty", ex.Body.Error.Code);
Assert.Contains("original", ex.Body.Error.Message);
});
}

[Fact]
public void UploadAttachmentWithNullConversationId()
{
UseClientFor(async client =>
{
var attachment = new AttachmentData("image/png", "Bot.png", ReadFile("bot.png"), ReadFile("bot_icon.png"));

var ex = await Assert.ThrowsAsync<ValidationException>(() => client.Conversations.UploadAttachmentAsync(null, attachment));
Assert.Contains("cannot be null", ex.Message);
});
}

[Fact]
public void UploadAttachmentWithNullAttachment()
{
UseClientFor(async client =>
{
var ex = await Assert.ThrowsAsync<ValidationException>(() => client.Conversations.UploadAttachmentAsync(conversationId, null));
Assert.Contains("cannot be null", ex.Message);
});
}

[Fact]
public void GetAttachmentInfoWithInvalidIdFails()
{
UseClientFor(async client =>
{
var ex = await Assert.ThrowsAsync<ErrorResponseException>(() => client.Attachments.GetAttachmentInfoAsync("bt13796-GJS4yaxDLI"));
Assert.Contains("NotFound", ex.Message);
});
}

[Fact]
public void GetAttachmentInfoWithNullIdFails()
{
UseClientFor(async client =>
{
var ex = await Assert.ThrowsAsync<ValidationException>(() => client.Attachments.GetAttachmentInfoAsync(null));
Assert.Contains("cannot be null", ex.Message);
});
}

[Fact]
public void GetAttachmentView()
{
UseClientFor(async client =>
{
var attachment = new AttachmentData("image/png", "Bot.png", ReadFile("bot.png"), ReadFile("bot_icon.png"));
var response = await client.Conversations.UploadAttachmentAsync(conversationId, attachment);
var attachmentId = response.Id;
var stream = await client.Attachments.GetAttachmentAsync(attachmentId, "original");

// Workaround for TestFramework not saving/replaying binary content
// Instead, convert the expected output the same way that the TestRecorder converts binary content to string
var expectedAsString = new StreamReader(new MemoryStream(attachment.OriginalBase64)).ReadToEnd();
var actualAsString = new StreamReader(stream).ReadToEnd();

Assert.Equal(expectedAsString, actualAsString);
});
}

[Fact]
public void GetAttachmentViewWithInvalidAttachmentIdFails()
{
UseClientFor(async client =>
{
var ex = await Assert.ThrowsAsync<ErrorResponseException>(() => client.Attachments.GetAttachmentAsync("bt13796-GJS4yaxDLI", "original"));
Assert.Contains("NotFound", ex.Message);
});
}

[Fact]
public void GetAttachmentViewWithNullAttachmentIdFails()
{

UseClientFor(async client =>
{
var ex = await Assert.ThrowsAsync<ValidationException>(() => client.Attachments.GetAttachmentAsync(null, "original"));
Assert.Contains("cannot be null", ex.Message);
});
}

[Fact]
public void GetAttachmentViewWithInvalidViewIdFails()
{

UseClientFor(async client =>
{
var attachment = new AttachmentData("image/png", "Bot.png", ReadFile("bot.png"), ReadFile("bot_icon.png"));
var response = await client.Conversations.UploadAttachmentAsync(conversationId, attachment);

var ex = await Assert.ThrowsAsync<ErrorResponseException>(() => client.Attachments.GetAttachmentAsync(response.Id, "invalid"));

Assert.Contains("NotFound", ex.Message);
});
}

[Fact]
public void GetAttachmentViewWithNullViewIdFails()
{

UseClientFor(async client =>
{
var attachment = new AttachmentData("image/png", "Bot.png", ReadFile("bot.png"), ReadFile("bot_icon.png"));
var response = await client.Conversations.UploadAttachmentAsync(conversationId, attachment);

var ex = await Assert.ThrowsAsync<ValidationException>(() => client.Attachments.GetAttachmentAsync(response.Id, null));

Assert.Contains("cannot be null", ex.Message);
});
}

private static byte[] ReadFile(string fileName)
{
var path = Path.Combine(Directory.GetCurrentDirectory(), "resources", fileName);
return File.ReadAllBytes(path);
}
}
}
6 changes: 3 additions & 3 deletions tests/Microsoft.Bot.Connector.Tests/BaseTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ namespace Connector.Tests
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Bot.Connector;
using Microsoft.Azure.Test.HttpRecorder;
using Microsoft.Bot.Connector;
using Microsoft.Rest;
using Microsoft.Rest.ClientRuntime.Azure.TestFramework;

Expand All @@ -20,8 +20,8 @@ public class BaseTest

protected const string clientId = "[MSAPP_ID]";
protected const string clientSecret = "[MSAPP_PASSWORD]";
protected const string userId = "U8H8E2HSB:T03CWQ0QB";
protected const string botId = "B21S8SG7J:T03CWQ0QB";
protected const string userId = "U19KH8EHJ:T03CWQ0QB";
protected const string botId = "B21UTEF8S:T03CWQ0QB";
protected static Uri hostUri = new Uri("https://slack.botframework.com", UriKind.Absolute);

private readonly string token;
Expand Down
7 changes: 7 additions & 0 deletions tests/Microsoft.Bot.Connector.Tests/DisableTestRunParallel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System.Reflection;
using Xunit;

[assembly: CollectionBehavior(DisableTestParallelization = true, MaxParallelThreads = 1)]
Loading