Skip to content

Commit

Permalink
Roles test (#366)
Browse files Browse the repository at this point in the history
* code cleanup, add user roles test

* code cleanup

* add user role docs

* fix restore

* update wordpress cli user

* cleanup

* code cleanup

* remove unnecessary auth test
  • Loading branch information
ThomasPe authored Sep 22, 2024
1 parent 54954c7 commit cc1cea5
Show file tree
Hide file tree
Showing 57 changed files with 599 additions and 578 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/integration-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ jobs:
uses: actions/setup-dotnet@v4
with:
dotnet-version: 8.x
- name: Dotnet Restore
run: dotnet restore ${{ env.solution }} --disable-parallel
- name: Build Solution
run: dotnet build ${{ env.solution }} -c Release
run: dotnet build ${{ env.solution }} -c Release --no-restore

- name: Create dockerzied WordPress
run: |
Expand Down
8 changes: 4 additions & 4 deletions WordPressPCL.Tests.Hosted/Basic_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public async Task Hosted_BasicSetupTest()
Assert.IsNotNull(_client);
// Posts
var posts = await _client.Posts.GetAllAsync();
Assert.AreNotEqual(posts.Count(), 0);
Assert.AreNotEqual(posts.Count, 0);
Assert.IsNotNull(posts);
}

Expand Down Expand Up @@ -73,7 +73,7 @@ public async Task Hosted_GetPostsByTag()

// Initialize
var posts = await _client.Posts.GetPostsByTagAsync(tagId);
Assert.AreNotEqual(0, posts.Count());
Assert.AreNotEqual(0, posts.Count);
foreach (Post post in posts)
{
Assert.IsTrue(post.Tags.ToList().Contains(tagId));
Expand All @@ -87,7 +87,7 @@ public async Task Hosted_GetPostsByAuthor()
int author = 3722200;
// Initialize
var posts = await _client.Posts.GetPostsByAuthorAsync(author);
Assert.AreNotEqual(0, posts.Count());
Assert.AreNotEqual(0, posts.Count);
foreach (Post post in posts)
{
Assert.IsTrue(post.Author == author);
Expand All @@ -101,7 +101,7 @@ public async Task Hosted_GetPostsBySearch()
string search = "hello";
// Initialize
var posts = await _client.Posts.GetPostsBySearchAsync(search);
Assert.AreNotEqual(0, posts.Count());
Assert.AreNotEqual(0, posts.Count);
foreach (Post post in posts)
{
bool containsOnContentOrTitle = false;
Expand Down
5 changes: 1 addition & 4 deletions WordPressPCL.Tests.Hosted/Utility/ClientHelper.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System.Threading.Tasks;
using WordPressPCL.Models;

namespace WordPressPCL.Tests.Hosted.Utility;
namespace WordPressPCL.Tests.Hosted.Utility;

public static class ClientHelper
{
Expand Down
6 changes: 3 additions & 3 deletions WordPressPCL.Tests.Hosted/Utility/HttpHelper_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public async Task Hosted_HttpHelper_InvalidPreProcessing()
// We call Get tag list without pre processing
var tags = await client.Tags.GetAllAsync();
Assert.IsNotNull(tags);
Assert.AreNotEqual(tags.Count(), 0);
Assert.AreNotEqual(tags.Count, 0);
CollectionAssert.AllItemsAreUnique(tags.Select(e => e.Id).ToList());

// Now we add a PreProcessing task
Expand Down Expand Up @@ -73,7 +73,7 @@ public async Task Hosted_HttpHelper_ValidPreProcessing() {
// We call Get tag list without pre processing
var tags = await client.Tags.GetAllAsync();
Assert.IsNotNull(tags);
Assert.AreNotEqual(tags.Count(), 0);
Assert.AreNotEqual(tags.Count, 0);
CollectionAssert.AllItemsAreUnique(tags.Select(e => e.Id).ToList());

// Now we add a PreProcessing task
Expand All @@ -84,7 +84,7 @@ public async Task Hosted_HttpHelper_ValidPreProcessing() {

tags = await client.Tags.GetAllAsync();
Assert.IsNotNull(tags);
Assert.AreNotEqual(tags.Count(), 0);
Assert.AreNotEqual(tags.Count, 0);
CollectionAssert.AllItemsAreUnique(tags.Select(e => e.Id).ToList());
}
}
6 changes: 3 additions & 3 deletions WordPressPCL.Tests.Hosted/WordPressPCL.Tests.Hosted.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.2.2" />
<PackageReference Include="MSTest.TestFramework" Version="3.2.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="MSTest.TestAdapter" Version="3.6.0" />
<PackageReference Include="MSTest.TestFramework" Version="3.6.0" />
</ItemGroup>

<ItemGroup>
Expand Down
17 changes: 8 additions & 9 deletions WordPressPCL.Tests.Selfhosted/ApplicationPasswords_Tests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Diagnostics;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
using System.Threading.Tasks;
using WordPressPCL.Models;
using WordPressPCL.Tests.Selfhosted.Utility;
Expand All @@ -21,15 +20,15 @@ public static async Task Init(TestContext testContext)
[TestMethod]
public async Task Application_Passwords_Create()
{
var password = await _clientAuth.Users.CreateApplicationPasswordAsync(System.Guid.NewGuid().ToString());
ApplicationPassword password = await _clientAuth.Users.CreateApplicationPasswordAsync(System.Guid.NewGuid().ToString());
Assert.IsNotNull(password.Password);
}

[TestMethod]
public async Task Read()
{
await _clientAuth.Users.CreateApplicationPasswordAsync(System.Guid.NewGuid().ToString());
var passwords = await _clientAuth.Users.GetApplicationPasswords();
List<ApplicationPassword> passwords = await _clientAuth.Users.GetApplicationPasswords();

Assert.IsNotNull(passwords);
Assert.AreNotEqual(0, passwords.Count);
Expand All @@ -38,16 +37,16 @@ public async Task Read()
[TestMethod]
public async Task Application_Password_Auth()
{
var appPassword = await _clientAuth.Users.CreateApplicationPasswordAsync(System.Guid.NewGuid().ToString());
var appPasswordClient = new WordPressClient(ApiCredentials.WordPressUri);
ApplicationPassword appPassword = await _clientAuth.Users.CreateApplicationPasswordAsync(System.Guid.NewGuid().ToString());
WordPressClient appPasswordClient = new(ApiCredentials.WordPressUri);
appPasswordClient.Auth.UseBasicAuth(ApiCredentials.Username, appPassword.Password);

var post = new Post()
Post post = new()
{
Title = new Title("Title 1"),
Content = new Content("Content PostCreate")
};
var postCreated = await appPasswordClient.Posts.CreateAsync(post);
Post postCreated = await appPasswordClient.Posts.CreateAsync(post);
Assert.IsNotNull(postCreated);
Assert.AreEqual("Title 1", postCreated.Title.Raw);
}
Expand Down
22 changes: 11 additions & 11 deletions WordPressPCL.Tests.Selfhosted/Basic_Tests.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using WordPressPCL.Tests.Selfhosted.Utility;
using System.Threading.Tasks;
using WordPressPCL.Models;
using System.Linq;
using System.Collections.Generic;

namespace WordPressPCL.Tests.Selfhosted;

Expand All @@ -28,20 +28,20 @@ public async Task BasicSetupTest()
// Initialize
Assert.IsNotNull(_client);
// Posts
var posts = await _client.Posts.GetAllAsync();
List<Post> posts = await _client.Posts.GetAllAsync();
Assert.IsNotNull(posts);

// Test Auth Client
var postsAuth = await _clientAuth.Posts.GetAllAsync();
List<Post> postsAuth = await _clientAuth.Posts.GetAllAsync();
Assert.IsNotNull(postsAuth);
}

[TestMethod]
public async Task GetFirstPostTest()
{
// Initialize
var posts = await _client.Posts.GetAllAsync();
var post = await _client.Posts.GetByIDAsync(posts.First().Id);
List<Post> posts = await _client.Posts.GetAllAsync();
Post post = await _client.Posts.GetByIDAsync(posts.First().Id);
Assert.IsTrue(posts.First().Id == post.Id);
Assert.IsTrue(!string.IsNullOrEmpty(posts.First().Content.Rendered));
}
Expand All @@ -50,7 +50,7 @@ public async Task GetFirstPostTest()
public async Task GetStickyPosts()
{
// Initialize
var posts = await _client.Posts.GetStickyPostsAsync();
List<Post> posts = await _client.Posts.GetStickyPostsAsync();

foreach (Post post in posts)
{
Expand All @@ -64,7 +64,7 @@ public async Task GetPostsByCategory()
// This CategoryID MUST exists at ApiCredentials.WordPressUri
int category = 1;
// Initialize
var posts = await _client.Posts.GetPostsByCategoryAsync(category);
List<Post> posts = await _client.Posts.GetPostsByCategoryAsync(category);

foreach (Post post in posts)
{
Expand All @@ -78,7 +78,7 @@ public async Task GetPostsByTag()
// This TagID MUST exists at ApiCredentials.WordPressUri
int tag = 12;
// Initialize
var posts = await _client.Posts.GetPostsByTagAsync(tag);
List<Post> posts = await _client.Posts.GetPostsByTagAsync(tag);

foreach (Post post in posts)
{
Expand All @@ -92,7 +92,7 @@ public async Task GetPostsByAuthor()
// This AuthorID MUST exists at ApiCredentials.WordPressUri
int author = 2;
// Initialize
var posts = await _client.Posts.GetPostsByAuthorAsync(author);
List<Post> posts = await _client.Posts.GetPostsByAuthorAsync(author);

foreach (Post post in posts)
{
Expand All @@ -106,7 +106,7 @@ public async Task GetPostsBySearch()
// This search term MUST be used at least once
string search = "hello";
// Initialize
var posts = await _client.Posts.GetPostsBySearchAsync(search);
List<Post> posts = await _client.Posts.GetPostsBySearchAsync(search);

foreach (Post post in posts)
{
Expand All @@ -124,7 +124,7 @@ public async Task GetPostsBySearch()
[TestMethod]
public async Task Authorize()
{
var validToken = await _clientAuth.Auth.IsValidJWTokenAsync();
bool validToken = await _clientAuth.Auth.IsValidJWTokenAsync();
Assert.IsTrue(validToken);
}
}
39 changes: 19 additions & 20 deletions WordPressPCL.Tests.Selfhosted/Categories_Tests.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WordPressPCL;
using WordPressPCL.Models;
using WordPressPCL.Tests.Selfhosted.Utility;
using WordPressPCL.Utility;
Expand All @@ -27,8 +26,8 @@ public static async Task Init(TestContext testContext)
public async Task Categories_Create()
{
Random random = new();
var name = $"TestCategory {random.Next(0, 10000)}";
var category = await _clientAuth.Categories.CreateAsync(new Category()
string name = $"TestCategory {random.Next(0, 10000)}";
Category category = await _clientAuth.Categories.CreateAsync(new Category()
{
Name = name,
Description = "Test"
Expand All @@ -41,30 +40,30 @@ public async Task Categories_Create()
[TestMethod]
public async Task Categories_Read()
{
var categories = await _client.Categories.GetAllAsync();
List<Category> categories = await _client.Categories.GetAllAsync();
Assert.IsNotNull(categories);
Assert.AreNotEqual(categories.Count(), 0);
Assert.AreNotEqual(categories.Count, 0);
CollectionAssert.AllItemsAreUnique(categories.Select(tag => tag.Id).ToList());
}

[TestMethod]
public async Task Categories_Get()
{
var categories = await _client.Categories.GetAsync();
List<Category> categories = await _client.Categories.GetAsync();
Assert.IsNotNull(categories);
Assert.AreNotEqual(categories.Count(), 0);
Assert.AreNotEqual(categories.Count, 0);
CollectionAssert.AllItemsAreUnique(categories.Select(tag => tag.Id).ToList());
}

[TestMethod]
public async Task Categories_Update()
{
var categories = await _clientAuth.Categories.GetAllAsync();
var category = categories.First();
List<Category> categories = await _clientAuth.Categories.GetAllAsync();
Category category = categories.First();
Random random = new();
var name = $"UpdatedCategory {random.Next(0, 10000)}";
string name = $"UpdatedCategory {random.Next(0, 10000)}";
category.Name = name;
var updatedCategory = await _clientAuth.Categories.UpdateAsync(category);
Category updatedCategory = await _clientAuth.Categories.UpdateAsync(category);
Assert.AreEqual(updatedCategory.Name, name);
Assert.AreEqual(updatedCategory.Id, category.Id);
}
Expand All @@ -73,8 +72,8 @@ public async Task Categories_Update()
public async Task Categories_Delete()
{
Random random = new();
var name = $"TestCategory {random.Next(0, 10000)}";
var category = await _clientAuth.Categories.CreateAsync(new Category()
string name = $"TestCategory {random.Next(0, 10000)}";
Category category = await _clientAuth.Categories.CreateAsync(new Category()
{
Name = name,
Description = "Test"
Expand All @@ -84,26 +83,26 @@ public async Task Categories_Delete()
{
Assert.Inconclusive();
}
var response = await _clientAuth.Categories.DeleteAsync(category.Id);
bool response = await _clientAuth.Categories.DeleteAsync(category.Id);
Assert.IsTrue(response);
var categories = await _clientAuth.Categories.GetAllAsync();
var c = categories.Where(x => x.Id == category.Id).ToList();
List<Category> categories = await _clientAuth.Categories.GetAllAsync();
List<Category> c = categories.Where(x => x.Id == category.Id).ToList();
Assert.AreEqual(c.Count, 0);
}

[TestMethod]
public async Task Categories_Query()
{
var queryBuilder = new CategoriesQueryBuilder()
CategoriesQueryBuilder queryBuilder = new()
{
Page = 1,
PerPage = 15,
OrderBy = TermsOrderBy.Id,
Order = Order.DESC,
};
var queryresult = await _clientAuth.Categories.QueryAsync(queryBuilder);
List<Category> queryresult = await _clientAuth.Categories.QueryAsync(queryBuilder);
Assert.AreEqual("?page=1&per_page=15&orderby=id&order=desc&context=view", queryBuilder.BuildQuery());
Assert.IsNotNull(queryresult);
Assert.AreNotSame(queryresult.Count(), 0);
Assert.AreNotSame(queryresult.Count, 0);
}
}
Loading

0 comments on commit cc1cea5

Please sign in to comment.