-
Notifications
You must be signed in to change notification settings - Fork 128
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
Adding tests and log #95
Open
tobyash86
wants to merge
9
commits into
main
Choose a base branch
from
feature/log_test
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
779aa75
adding log and tests
tobyash86 c72456d
add dummy error handling
tobyash86 7850d24
Update readme.md
tobyash86 ceaf3e5
Update codeqa action version
tobyash86 2883211
Merge branch 'feature/log_test' of https://github.com/tobyash86/WebGo…
tobyash86 8a88488
Update dottest_sa.yml
tobyash86 e64fe67
Update dottest_tia.yml
tobyash86 0c3f21c
Update readme.md
tobyash86 9558abb
Update dottest_tia.yml
tobyash86 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
WebGoat.NET.Tests/BlogRespRepositoryTests/BlogResponsesTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using Moq; | ||
using NUnit.Framework; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using WebGoatCore.Data; | ||
using WebGoatCore.Models; | ||
|
||
namespace WebGoat.NET.Tests.BlogRespRepositoryTests | ||
{ | ||
[TestFixture] | ||
public class BlogResponsesTests | ||
{ | ||
Mock<NorthwindContext> _context; | ||
|
||
[SetUp] | ||
public void Setup() | ||
{ | ||
_context = ContextSetup.CreateContext(); | ||
} | ||
|
||
[Test] | ||
public void CreateBlogRespTest() | ||
{ | ||
var blogEntryRepo = new BlogEntryRepository(_context.Object); | ||
var entry1 = blogEntryRepo.GetBlogEntry(1); | ||
|
||
var respRepo = new BlogResponseRepository(_context.Object); | ||
var resp = new BlogResponse() { Author = "admin", Contents = "Test", Id = 4, ResponseDate = DateTime.Now, BlogEntry = entry1, BlogEntryId = entry1.Id }; | ||
|
||
respRepo.CreateBlogResponse(resp); | ||
|
||
Assert.That(_context.Object.BlogResponses.Count(), Is.EqualTo(4)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.ChangeTracking; | ||
using Microsoft.EntityFrameworkCore.ChangeTracking.Internal; | ||
using Microsoft.EntityFrameworkCore.Metadata; | ||
using Moq; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Data.Entity; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using WebGoatCore.Data; | ||
using WebGoatCore.Models; | ||
|
||
namespace WebGoat.NET.Tests.BlogRespRepositoryTests | ||
{ | ||
internal static class ContextSetup | ||
{ | ||
internal static Mock<NorthwindContext> CreateContext() | ||
{ | ||
// create test DB | ||
var context = BlogRepositoryTests.ContextSetup.CreateContext(); | ||
|
||
var blogEntryRepo = new BlogEntryRepository(context.Object); | ||
|
||
var entry1 = blogEntryRepo.GetBlogEntry(1); | ||
|
||
var r1 = new BlogResponse() { Author = "admin", Contents = "Test Content", Id = 1, ResponseDate = DateTime.Now, BlogEntry = entry1, BlogEntryId = entry1.Id }; | ||
var r2 = new BlogResponse() { Author = "kmitnick", Contents = "KM Test Content", Id = 2, ResponseDate = DateTime.Now, BlogEntry = entry1, BlogEntryId = entry1.Id }; | ||
var r3 = new BlogResponse() { Author = "me", Contents = "ME Test Content", Id = 3, ResponseDate = DateTime.Now, BlogEntry = entry1, BlogEntryId = entry1.Id }; | ||
|
||
entry1.Responses.Add(r1); | ||
entry1.Responses.Add(r2); | ||
entry1.Responses.Add(r3); | ||
|
||
var entriesList = new List<BlogResponse> { | ||
r1, r2, r3 | ||
}; | ||
var initialBlogEntries = entriesList.AsQueryable(); | ||
|
||
Func<BlogResponse, EntityEntry<BlogResponse>> mockEntityEntry = (BlogResponse data) => | ||
{ | ||
var internalEntityEntry = new InternalEntityEntry( | ||
new Mock<IStateManager>().Object, | ||
new RuntimeEntityType(nameof(BlogResponse), typeof(BlogResponse), false, null, null, null, ChangeTrackingStrategy.Snapshot, null, false), | ||
data); | ||
|
||
var entityEntry = new EntityEntry<BlogResponse>(internalEntityEntry); | ||
return entityEntry; | ||
}; | ||
|
||
var mockSet = DbSetTestUtil.CreateDbSetMock(initialBlogEntries); | ||
|
||
mockSet.Setup(m => m.Add(It.IsAny<BlogResponse>())).Returns((BlogResponse b) => | ||
{ | ||
entriesList.Add(b); | ||
return mockEntityEntry(b); | ||
}); | ||
|
||
context.SetupGet(c => c.BlogResponses).Returns(mockSet.Object); | ||
|
||
return context; | ||
} | ||
|
||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check warning
Code scanning / dotTEST
Avoid the use of "catch" on 'Exception', 'SystemException' or 'ApplicationException'