Skip to content

DE-901 - Support template creation #193

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

Merged
merged 4 commits into from
Dec 5, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,166 @@ public async Task UpdateTemplate_should_execute_the_right_query_and_parameters()
dbQuery.VerifySqlQueryContains("PreviewImage = @PreviewImage");
dbQuery.VerifySqlQueryContains("Name = @Name");
}

[Fact]
public async Task CreatePrivateTemplate_should_execute_the_right_query_and_parameters()
{
// Arrange
var dbContextMock = new Mock<IDbContext>();
dbContextMock.Setup(x =>
x.ExecuteAsync(It.IsAny<ISingleItemDbQuery<CreatePrivateTemplateDbQuery.Result>>()))
.ReturnsAsync(new CreatePrivateTemplateDbQuery.Result() { NewTemplateId = 123 });

var sut = new DopplerTemplateRepository(dbContextMock.Object);

var previewImage = "NEW PREVIEW IMAGE";
var name = "NEW NAME";
var htmlComplete = "NEW HTML CONTENT";
var meta = "{\"test\":\"NEW META\"}";
var templateModel = new TemplateModel(
TemplateId: 0,
IsPublic: false,
PreviewImage: previewImage,
Name: name,
Content: new UnlayerTemplateContentData(
HtmlComplete: htmlComplete,
Meta: meta));
var accountName = "test@test";

// Act
var result = await sut.CreatePrivateTemplate(accountName, templateModel);

// Assert
var dbQuery = dbContextMock.VerifyAndGetSingleItemDbQuery<CreatePrivateTemplateDbQuery.Result>();
dbQuery.VerifySqlParametersContain("EditorType", _unlayerEditorType);
dbQuery.VerifySqlParametersContain("HtmlCode", htmlComplete);
dbQuery.VerifySqlParametersContain("Meta", meta);
dbQuery.VerifySqlParametersContain("PreviewImage", previewImage);
dbQuery.VerifySqlParametersContain("Name", name);
dbQuery.VerifySqlQueryContains("FROM [User] u");
dbQuery.VerifySqlQueryContains("WHERE u.Email = @AccountName");
dbQuery.VerifySqlQueryContains("INSERT INTO Template (IdUser, EditorType, HtmlCode, Meta, PreviewImage, Name, Active)");
dbQuery.VerifySqlQueryContains("u.IdUser AS IdUser");
dbQuery.VerifySqlQueryContains("@EditorType AS EditorType");
dbQuery.VerifySqlQueryContains("@HtmlCode AS HtmlCode");
dbQuery.VerifySqlQueryContains("@Meta AS Meta");
dbQuery.VerifySqlQueryContains("@PreviewImage AS PreviewImage");
dbQuery.VerifySqlQueryContains("@Name AS Name");
dbQuery.VerifySqlQueryContains("1 AS Active");
dbQuery.VerifySqlQueryContains("OUTPUT INSERTED.idTemplate AS NewTemplateId");
}

[Fact]
public async Task CreatePrivateTemplate_throw_when_there_are_no_rows_inserted()
{
// Arrange
var dbContextMock = new Mock<IDbContext>();
dbContextMock.Setup(x =>
x.ExecuteAsync(It.IsAny<ISingleItemDbQuery<CreatePrivateTemplateDbQuery.Result>>()))
.ReturnsAsync((CreatePrivateTemplateDbQuery.Result)null);

var sut = new DopplerTemplateRepository(dbContextMock.Object);

var templateModel = new TemplateModel(
TemplateId: 0,
IsPublic: false,
PreviewImage: "NEW PREVIEW IMAGE",
Name: "NEW NAME",
Content: new UnlayerTemplateContentData(
HtmlComplete: "NEW HTML CONTENT",
Meta: "{\"test\":\"NEW META\"}"));
var accountName = "test@test";

// Act
var action = async () => await sut.CreatePrivateTemplate(accountName, templateModel);

// Assert
var exception = await Assert.ThrowsAsync<ArgumentException>(action);
Assert.Equal("accountName", exception.ParamName);
Assert.Equal($"Account with name '{accountName}' does not exist. (Parameter 'accountName')", exception.Message);
}

[Fact]
public async Task CreatePrivateTemplate_should_throw_when_TemplateId_is_not_0()
{
// Arrange
var templateId = 123;
var dbContextMock = new Mock<IDbContext>();

var sut = new DopplerTemplateRepository(dbContextMock.Object);

var templateModel = new TemplateModel(
TemplateId: templateId,
IsPublic: false,
PreviewImage: "NEW PREVIEW IMAGE",
Name: "NEW NAME",
Content: new UnlayerTemplateContentData(
HtmlComplete: "NEW HTML CONTENT",
Meta: "{\"test\":\"NEW META\"}"));
var accountName = "test@test";

// Act
var action = async () => await sut.CreatePrivateTemplate(accountName, templateModel);

// Assert
var exception = await Assert.ThrowsAsync<ArgumentException>(action);
Assert.Equal("templateModel", exception.ParamName);
Assert.Equal("TemplateId should not be set to create a new private template (Parameter 'templateModel')", exception.Message);
dbContextMock.VerifyNoOtherCalls();
}

[Fact]
public async Task CreatePrivateTemplate_should_throw_when_IsPublic_is_true()
{
// Arrange
var isPublic = true;
var dbContextMock = new Mock<IDbContext>();

var sut = new DopplerTemplateRepository(dbContextMock.Object);

var templateModel = new TemplateModel(
TemplateId: 0,
IsPublic: isPublic,
PreviewImage: "NEW PREVIEW IMAGE",
Name: "NEW NAME",
Content: new UnlayerTemplateContentData(
HtmlComplete: "NEW HTML CONTENT",
Meta: "{\"test\":\"NEW META\"}"));
var accountName = "test@test";

// Act
var action = async () => await sut.CreatePrivateTemplate(accountName, templateModel);

// Assert
var exception = await Assert.ThrowsAsync<ArgumentException>(action);
Assert.Equal("templateModel", exception.ParamName);
Assert.Equal("IsPublic should be false to create a new private template (Parameter 'templateModel')", exception.Message);
dbContextMock.VerifyNoOtherCalls();
}

[Fact]
public async Task CreatePrivateTemplate_should_throw_when_the_template_is_not_unlayer_one()
{
// Arrange
var content = new UnknownTemplateContentData(_msEditorType);
var dbContextMock = new Mock<IDbContext>();

var sut = new DopplerTemplateRepository(dbContextMock.Object);

var templateModel = new TemplateModel(
TemplateId: 0,
IsPublic: false,
PreviewImage: "NEW PREVIEW IMAGE",
Name: "NEW NAME",
Content: content);
var accountName = "test@test";

// Act
var action = async () => await sut.CreatePrivateTemplate(accountName, templateModel);

// Assert
var exception = await Assert.ThrowsAsync<NotImplementedException>(action);
Assert.Equal("Unsupported template content type Doppler.HtmlEditorApi.Domain.UnknownTemplateContentData", exception.Message);
dbContextMock.VerifyNoOtherCalls();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,10 @@ public async Task GET_template_should_error_when_template_content_is_mseditor(st
var responseContentJson = responseContentDoc.RootElement;

// Assert
Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
Assert.Equal("https://httpstatuses.io/500", responseContentJson.GetProperty("type").GetString());
Assert.Equal("Internal Server Error", responseContentJson.GetProperty("title").GetString());
Assert.Equal(HttpStatusCode.NotImplemented, response.StatusCode);
Assert.Equal("Not Implemented", responseContentJson.GetProperty("title").GetString());
Assert.Equal("Unsupported template content type Doppler.HtmlEditorApi.Domain.UnknownTemplateContentData", responseContentJson.GetProperty("detail").GetString());
Assert.Equal(500, responseContentJson.GetProperty("status").GetInt32());
Assert.Equal(501, responseContentJson.GetProperty("status").GetInt32());
}

[Theory]
Expand Down Expand Up @@ -284,6 +283,8 @@ public async Task GET_template_should_return_not_found_when_template_is_public(s

// Assert
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
Assert.Equal($"It is a public template, use /shared/templates/{idTemplate}", responseContent);
Assert.Contains($"\"detail\":\"It is a public template, use /shared/templates/{idTemplate}\"", responseContent);
Assert.Contains("\"title\":\"Not Found\"", responseContent);
Assert.Contains("\"status\":404", responseContent);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,9 @@ public async Task PUT_template_should_return_404_when_template_does_not_exist()

// Assert
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
Assert.Equal("Template not found, belongs to a different account, or it is a public template.", responseContent);
Assert.Contains("Template not found, belongs to a different account, or it is a public template", responseContent);
Assert.Contains("\"title\":\"Not Found\"", responseContent);
Assert.Contains("\"status\":404", responseContent);
}

[Fact]
Expand Down Expand Up @@ -273,7 +275,9 @@ public async Task PUT_template_should_return_404_when_template_is_public()

// Assert
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
Assert.Equal("Template not found, belongs to a different account, or it is a public template.", responseContent);
Assert.Contains("\"detail\":\"Template not found, belongs to a different account, or it is a public template.\"", responseContent);
Assert.Contains("\"title\":\"Not Found\"", responseContent);
Assert.Contains("\"status\":404", responseContent);
}

[Fact]
Expand Down
Loading