Skip to content

Commit

Permalink
Track WordPressMocks repository locally (#19001)
Browse files Browse the repository at this point in the history
  • Loading branch information
mokagio authored Jul 7, 2022
2 parents e4ddc33 + f1bfbf8 commit adfda7d
Show file tree
Hide file tree
Showing 169 changed files with 44,835 additions and 29 deletions.
41 changes: 41 additions & 0 deletions API-Mocks/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
Network mocking for testing the WordPress mobile apps based on [WireMock](https://wiremock.org/).

## Usage

To start the WireMock server as a standalone process, you can run it with this command:

```
./scripts/start.sh 8282
```

Here `8282` is the port to run the server on. It can now be accessed from `http://localhost:8282`.

## Creating a mock file

The JSON files used by WireMock to handle requests and are located in `src/main/assets`. To generate one of these files
you're first going to want to set up [Charles Proxy](https://www.charlesproxy.com/) (or similar) to work with your iOS Simulator.

Here's an example of what a mock might look like:

```json
{
"request": {
"urlPattern": "/rest/v1.1/me/",
"method": "GET"
},
"response": {
"status": 200,
"jsonBody": {
// Your response here...
},
"headers": {
"Content-Type": "application/json",
"Connection": "keep-alive",
"Cache-Control": "no-cache, must-revalidate, max-age=0"
}
}
}
```

These files are used to match network requests while the tests are being run. For more on request matching with
WireMock check out [their documentation](http://wiremock.org/docs/request-matching/).
81 changes: 81 additions & 0 deletions API-Mocks/Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# frozen_string_literal: true

require 'json'
require 'jsonlint'
require 'git'

desc 'Re-format all JSON files to be pretty-printed'
task :format, [:silent] do |_, args|
args.with_defaults(silent: false)

for_each_mock_file do |file|
puts "Formatting #{file}..." unless args[:silent]
json = JSON.parse(File.read(file))
File.write(file, JSON.pretty_generate(json))
rescue StandardError => e
linter = JsonLint::Linter.new
linter.check(file)
linter.display_errors

abort("Invalid JSON. See errors above. (#{e})")
end
end

desc 'Check that all files are properly formatted in CI'
task :checkformat do
repo = Git.open('../.')

abort('Repo is dirty – unable to verify JSON files are correctly formatted') unless repo.diff.lines.zero?
Rake::Task['format'].invoke(true)

if repo.diff.lines.positive?
repo.reset_hard
abort('Repo contains unformatted JSON files – run `rake format` then commit your changes.')
end
end

desc "Ensure all JSON files are valid and don't contain common mistakes"
task :lint do
file_errors = {}

for_each_mock_file do |file|
# Ensure the file is valid JSON
linter = JsonLint::Linter.new
linter.check(file)
if linter.errors_count.positive?
linter.errors.map { |_key, value| value }.each do |error|
append_error(file, file_errors, "Invalid JSON: #{error}}")
end
end

## Ensure there are no references to the actual API location – we should use the mocks
# base URL – this ensures that any requests made based on the contents of other
# requests won't fail.
if File.open(file).each_line.any? { |line| line.include?('public-api.wordpress.com') }
append_error(file, file_errors, 'Contains references to `https://public-api.wordpress.com`. Replace them with `{{request.requestLine.baseUrl}}`.')
end
end

# Output file errors in a pretty way
puts "There are errors in #{file_errors.count} files:\n" unless file_errors.empty?
file_errors.each do |file, errors|
puts "=== #{file}"
errors.each do |e|
puts " #{e}"
end
end

abort unless file_errors.empty?
puts 'Lint Complete. Everything looks good.'
end

def for_each_mock_file
Dir.glob('WordPressMocks/**/*.json').each do |file|
yield(File.expand_path(file))
end
end

def append_error(file, errors, message)
errors[file] = [] if errors[file].nil?
errors[file].append(message)
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"request": {
"method": "GET",
"urlPath": "/rest/v1.1/connect/site-info",
"queryParameters": {
"url": {
"matches": "^http(s)?://((?!wordpress.com).)*$"
}
}
},
"response": {
"status": 200,
"jsonBody": {
"urlAfterRedirects": "{{request.query.url}}",
"exists": true,
"isWordPress": true,
"hasJetpack": true,
"jetpackVersion": "7.3.1",
"isJetpackActive": true,
"isJetpackConnected": false,
"isWordPressDotCom": false
},
"headers": {
"Content-Type": "application/json",
"Connection": "keep-alive",
"Cache-Control": "no-cache, must-revalidate, max-age=0"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"request": {
"url": "/xmlrpc.php",
"method": "POST",
"bodyPatterns": [
{
"matches": ".*<methodName>system.listMethods</methodName>.*"
}
]
},
"response": {
"status": 200,
"body": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<methodResponse>\n <params>\n <param>\n <value>\n <array><data>\n <value><string>system.multicall</string></value>\n <value><string>system.listMethods</string></value>\n <value><string>system.getCapabilities</string></value>\n <value><string>demo.addTwoNumbers</string></value>\n <value><string>demo.sayHello</string></value>\n <value><string>pingback.extensions.getPingbacks</string></value>\n <value><string>pingback.ping</string></value>\n <value><string>mt.publishPost</string></value>\n <value><string>mt.getTrackbackPings</string></value>\n <value><string>mt.supportedTextFilters</string></value>\n <value><string>mt.supportedMethods</string></value>\n <value><string>mt.setPostCategories</string></value>\n <value><string>mt.getPostCategories</string></value>\n <value><string>mt.getRecentPostTitles</string></value>\n <value><string>mt.getCategoryList</string></value>\n <value><string>metaWeblog.getUsersBlogs</string></value>\n <value><string>metaWeblog.deletePost</string></value>\n <value><string>metaWeblog.newMediaObject</string></value>\n <value><string>metaWeblog.getCategories</string></value>\n <value><string>metaWeblog.getRecentPosts</string></value>\n <value><string>metaWeblog.getPost</string></value>\n <value><string>metaWeblog.editPost</string></value>\n <value><string>metaWeblog.newPost</string></value>\n <value><string>blogger.deletePost</string></value>\n <value><string>blogger.editPost</string></value>\n <value><string>blogger.newPost</string></value>\n <value><string>blogger.getRecentPosts</string></value>\n <value><string>blogger.getPost</string></value>\n <value><string>blogger.getUserInfo</string></value>\n <value><string>blogger.getUsersBlogs</string></value>\n <value><string>wp.restoreRevision</string></value>\n <value><string>wp.getRevisions</string></value>\n <value><string>wp.getPostTypes</string></value>\n <value><string>wp.getPostType</string></value>\n <value><string>wp.getPostFormats</string></value>\n <value><string>wp.getMediaLibrary</string></value>\n <value><string>wp.getMediaItem</string></value>\n <value><string>wp.getCommentStatusList</string></value>\n <value><string>wp.newComment</string></value>\n <value><string>wp.editComment</string></value>\n <value><string>wp.deleteComment</string></value>\n <value><string>wp.getComments</string></value>\n <value><string>wp.getComment</string></value>\n <value><string>wp.setOptions</string></value>\n <value><string>wp.getOptions</string></value>\n <value><string>wp.getPageTemplates</string></value>\n <value><string>wp.getPageStatusList</string></value>\n <value><string>wp.getPostStatusList</string></value>\n <value><string>wp.getCommentCount</string></value>\n <value><string>wp.deleteFile</string></value>\n <value><string>wp.uploadFile</string></value>\n <value><string>wp.suggestCategories</string></value>\n <value><string>wp.deleteCategory</string></value>\n <value><string>wp.newCategory</string></value>\n <value><string>wp.getTags</string></value>\n <value><string>wp.getCategories</string></value>\n <value><string>wp.getAuthors</string></value>\n <value><string>wp.getPageList</string></value>\n <value><string>wp.editPage</string></value>\n <value><string>wp.deletePage</string></value>\n <value><string>wp.newPage</string></value>\n <value><string>wp.getPages</string></value>\n <value><string>wp.getPage</string></value>\n <value><string>wp.editProfile</string></value>\n <value><string>wp.getProfile</string></value>\n <value><string>wp.getUsers</string></value>\n <value><string>wp.getUser</string></value>\n <value><string>wp.getTaxonomies</string></value>\n <value><string>wp.getTaxonomy</string></value>\n <value><string>wp.getTerms</string></value>\n <value><string>wp.getTerm</string></value>\n <value><string>wp.deleteTerm</string></value>\n <value><string>wp.editTerm</string></value>\n <value><string>wp.newTerm</string></value>\n <value><string>wp.getPosts</string></value>\n <value><string>wp.getPost</string></value>\n <value><string>wp.deletePost</string></value>\n <value><string>wp.editPost</string></value>\n <value><string>wp.newPost</string></value>\n <value><string>wp.getUsersBlogs</string></value>\n</data></array>\n </value>\n </param>\n </params>\n</methodResponse>\n",
"headers": {
"Content-Type": "text/xml; charset=UTF-8",
"Connection": "keep-alive"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"request": {
"url": "/xmlrpc.php",
"method": "POST",
"bodyPatterns": [
{
"matches": ".*<methodName>wp.getComments</methodName>.*"
},
{
"matches": ".*<param><value><string>e2eflowtestingmobile</string></value></param>.*"
}
]
},
"response": {
"status": 200,
"body": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<methodResponse>\n <params>\n <param>\n <value>\n <array><data>\n <value><struct>\n <member><name>date_created_gmt</name><value><dateTime.iso8601>20190215T11:23:34</dateTime.iso8601></value></member>\n <member><name>user_id</name><value><string>0</string></value></member>\n <member><name>comment_id</name><value><string>1</string></value></member>\n <member><name>parent</name><value><string>0</string></value></member>\n <member><name>status</name><value><string>approve</string></value></member>\n <member><name>content</name><value><string>Hi, this is a comment.\nTo get started with moderating, editing, and deleting comments, please visit the Comments screen in the dashboard.\nCommenter avatars come from &lt;a href=&quot;https://gravatar.com&quot;&gt;Gravatar&lt;/a&gt;.</string></value></member>\n <member><name>link</name><value><string>{{request.requestLine.baseUrl}}/2019/02/hello-world/#comment-1</string></value></member>\n <member><name>post_id</name><value><string>1</string></value></member>\n <member><name>post_title</name><value><string>Hello world!</string></value></member>\n <member><name>author</name><value><string>A WordPress Commenter</string></value></member>\n <member><name>author_url</name><value><string>https://wordpress.org/</string></value></member>\n <member><name>author_email</name><value><string>[email protected]</string></value></member>\n <member><name>author_ip</name><value><string></string></value></member>\n <member><name>type</name><value><string></string></value></member>\n</struct></value>\n</data></array>\n </value>\n </param>\n </params>\n</methodResponse>\n",
"headers": {
"Content-Type": "text/xml; charset=UTF-8",
"Connection": "keep-alive"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"request": {
"url": "/xmlrpc.php",
"method": "POST",
"bodyPatterns": [
{
"matches": ".*<methodName>wp.getOptions</methodName>.*"
},
{
"matches": ".*<param><value><string>e2eflowtestingmobile</string></value></param>.*"
}
]
},
"response": {
"status": 200,
"body": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<methodResponse>\n <params>\n <param>\n <value>\n <struct>\n <member><name>software_version</name><value><struct>\n <member><name>desc</name><value><string>Software Version</string></value></member>\n <member><name>readonly</name><value><boolean>1</boolean></value></member>\n <member><name>value</name><value><string>5.1.1</string></value></member>\n</struct></value></member>\n <member><name>post_thumbnail</name><value><struct>\n <member><name>desc</name><value><string>Post Thumbnail</string></value></member>\n <member><name>readonly</name><value><boolean>1</boolean></value></member>\n <member><name>value</name><value><boolean>1</boolean></value></member>\n</struct></value></member>\n <member><name>default_comment_status</name><value><struct>\n <member><name>desc</name><value><string>Allow people to post comments on new articles</string></value></member>\n <member><name>readonly</name><value><boolean>0</boolean></value></member>\n <member><name>value</name><value><string>open</string></value></member>\n</struct></value></member>\n <member><name>jetpack_client_id</name><value><struct>\n <member><name>desc</name><value><string>The Client ID/WP.com Blog ID of this site</string></value></member>\n <member><name>readonly</name><value><boolean>1</boolean></value></member>\n <member><name>value</name><value><boolean>0</boolean></value></member>\n</struct></value></member>\n <member><name>home_url</name><value><struct>\n <member><name>desc</name><value><string>Site Address (URL)</string></value></member>\n <member><name>readonly</name><value><boolean>1</boolean></value></member>\n <member><name>value</name><value><string>{{request.requestLine.baseUrl}}</string></value></member>\n</struct></value></member>\n <member><name>admin_url</name><value><struct>\n <member><name>desc</name><value><string>The URL to the admin area</string></value></member>\n <member><name>readonly</name><value><boolean>1</boolean></value></member>\n <member><name>value</name><value><string>{{request.requestLine.baseUrl}}/wp-admin/</string></value></member>\n</struct></value></member>\n <member><name>login_url</name><value><struct>\n <member><name>desc</name><value><string>Login Address (URL)</string></value></member>\n <member><name>readonly</name><value><boolean>1</boolean></value></member>\n <member><name>value</name><value><string>{{request.requestLine.baseUrl}}/wp-login.php</string></value></member>\n</struct></value></member>\n <member><name>blog_title</name><value><struct>\n <member><name>desc</name><value><string>Site Title</string></value></member>\n <member><name>readonly</name><value><boolean>0</boolean></value></member>\n <member><name>value</name><value><string>e2eflowtestingmobile.mystagingwebsite.com at Pressable</string></value></member>\n</struct></value></member>\n <member><name>time_zone</name><value><struct>\n <member><name>desc</name><value><string>Time Zone</string></value></member>\n <member><name>readonly</name><value><boolean>0</boolean></value></member>\n <member><name>value</name><value><string>0</string></value></member>\n</struct></value></member>\n</struct>\n </value>\n </param>\n </params>\n</methodResponse>\n",
"headers": {
"Content-Type": "text/xml; charset=UTF-8",
"Connection": "keep-alive"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"request": {
"url": "/xmlrpc.php",
"method": "POST",
"bodyPatterns": [
{
"matches": ".*<methodName>wp.getPostFormats</methodName>.*"
},
{
"matches": ".*<param><value><string>e2eflowtestingmobile</string></value></param>.*"
}
]
},
"response": {
"status": 200,
"body": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<methodResponse>\n <params>\n <param>\n <value>\n <struct>\n <member><name>standard</name><value><string>Standard</string></value></member>\n <member><name>aside</name><value><string>Aside</string></value></member>\n <member><name>chat</name><value><string>Chat</string></value></member>\n <member><name>gallery</name><value><string>Gallery</string></value></member>\n <member><name>link</name><value><string>Link</string></value></member>\n <member><name>image</name><value><string>Image</string></value></member>\n <member><name>quote</name><value><string>Quote</string></value></member>\n <member><name>status</name><value><string>Status</string></value></member>\n <member><name>video</name><value><string>Video</string></value></member>\n <member><name>audio</name><value><string>Audio</string></value></member>\n</struct>\n </value>\n </param>\n </params>\n</methodResponse>\n",
"headers": {
"Content-Type": "text/xml; charset=UTF-8",
"Connection": "keep-alive"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"request": {
"url": "/xmlrpc.php",
"method": "POST",
"bodyPatterns": [
{
"matches": ".*<methodName>wp.getProfile</methodName>.*"
},
{
"matches": ".*<param><value><string>e2eflowtestingmobile</string></value></param>.*"
}
]
},
"response": {
"status": 200,
"body": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<methodResponse>\n <params>\n <param>\n <value>\n <struct>\n <member><name>user_id</name><value><string>1</string></value></member>\n <member><name>username</name><value><string>e2eflowtestingmobile</string></value></member>\n <member><name>first_name</name><value><string></string></value></member>\n <member><name>last_name</name><value><string></string></value></member>\n <member><name>registered</name><value><dateTime.iso8601>20190215T11:23:34</dateTime.iso8601></value></member>\n <member><name>bio</name><value><string></string></value></member>\n <member><name>email</name><value><string>[email protected]</string></value></member>\n <member><name>nickname</name><value><string>e2eflowtestingmobile</string></value></member>\n <member><name>nicename</name><value><string>e2eflowtestingmobile</string></value></member>\n <member><name>url</name><value><string></string></value></member>\n <member><name>display_name</name><value><string>e2eflowtestingmobile</string></value></member>\n <member><name>roles</name><value><array><data>\n <value><string>administrator</string></value>\n</data></array></value></member>\n</struct>\n </value>\n </param>\n </params>\n</methodResponse>\n",
"headers": {
"Content-Type": "text/xml; charset=UTF-8",
"Connection": "keep-alive"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"request": {
"url": "/xmlrpc.php",
"method": "POST",
"bodyPatterns": [
{
"matches": ".*<methodName>wp.getTerms</methodName>.*"
},
{
"matches": ".*<param><value><string>e2eflowtestingmobile</string></value></param>.*"
}
]
},
"response": {
"status": 200,
"body": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<methodResponse>\n <params>\n <param>\n <value>\n <array><data>\n <value><struct>\n <member><name>term_id</name><value><string>1</string></value></member>\n <member><name>name</name><value><string>Uncategorized</string></value></member>\n <member><name>slug</name><value><string>uncategorized</string></value></member>\n <member><name>term_group</name><value><string>0</string></value></member>\n <member><name>term_taxonomy_id</name><value><string>1</string></value></member>\n <member><name>taxonomy</name><value><string>category</string></value></member>\n <member><name>description</name><value><string></string></value></member>\n <member><name>parent</name><value><string>0</string></value></member>\n <member><name>count</name><value><int>1</int></value></member>\n <member><name>filter</name><value><string>raw</string></value></member>\n <member><name>custom_fields</name><value><array><data>\n</data></array></value></member>\n</struct></value>\n</data></array>\n </value>\n </param>\n </params>\n</methodResponse>\n",
"headers": {
"Content-Type": "text/xml; charset=UTF-8",
"Connection": "keep-alive"
}
}
}
Loading

0 comments on commit adfda7d

Please sign in to comment.