Skip to content

Commit

Permalink
Test getting app tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
samrocketman committed Jun 19, 2023
1 parent cf190ce commit 6e9b540
Show file tree
Hide file tree
Showing 9 changed files with 323 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,122 @@
package net.gleske.jervis.remotes.creds
//the GitHubAppCredentialTest() class automatically sees the GitHubAppCredential() class because they're in the same package

import static net.gleske.jervis.remotes.StaticMocking.mockStaticUrl
import net.gleske.jervis.exceptions.GitHubAppException

import org.junit.After
import org.junit.Before
import org.junit.Test

class GitHubAppCredentialTest extends GroovyTestCase {
/**
This class tests the <tt>{@link net.gleske.jervis.remotes.creds.GitHubAppCredential}</tt>
class. This uses auto-generated mock data using real API responses.
<h2>Generate Mock Data</h2>
Mock data has already been generated. This is the script which captured mock
data.
<pre><code>
import static net.gleske.jervis.remotes.StaticMocking.recordMockUrls
import net.gleske.jervis.remotes.SimpleRestServiceSupport
if(!binding.hasVariable('url')) {
String persistStr
url = persistStr
}
if(binding.hasVariable('request_meta')) {
request_meta.clear()
} else {
request_meta = [:]
}
if(binding.hasVariable('request_history')) {
request_history.clear()
} else {
request_history = []
}
// Record URL API data to files as mock data
recordMockUrls(url, URL, request_meta, true, 'SHA-256', request_history)
import net.gleske.jervis.remotes.creds.EphemeralTokenCache
import net.gleske.jervis.remotes.creds.GitHubAppCredential
import net.gleske.jervis.remotes.creds.GitHubAppRsaCredentialImpl
import net.gleske.jervis.tools.YamlOperator
import java.time.Instant
import java.time.temporal.ChronoUnit
// Configure the private key downloaded from GitHub App.
GitHubAppRsaCredentialImpl rsaCred = new GitHubAppRsaCredentialImpl('173962', new File('github-app.pem').text)
rsaCred.owner = 'sgleske-test'
// Configure in-memory token storage
EphemeralTokenCache tokenCred = new EphemeralTokenCache(true)
tokenCred.loadCache = null
tokenCred.saveCache = null
tokenCred.obtainLock = null
GitHubAppCredential app = new GitHubAppCredential(rsaCred, tokenCred)
app.token
app.token
rsaCred.owner = 'samrocketman'
app.hash = ''
app.ownerIsUser = true
app.installation_id = null
app.token
// SANITIZE sensitive information and reduce mock data
// update mock data to represent reponses 500 years in the future to simplify mocking responses
request_history.each { Map request -&gt;
Map response = YamlOperator.loadYamlFrom(request.response)
Boolean updateFile = false
if(response.token) {
response.token = 'some-token'
updateFile = true
}
if(response.expires_at) {
Long fiveHundredYearsInDays = 182500
response.expires_at = Instant.now().plus(fiveHundredYearsInDays, ChronoUnit.DAYS).toString()
updateFile = true
}
if(response?.app_id &amp;&amp; response?.account?.login) {
response = [id: response.id]
updateFile = true
}
if(updateFile) {
request.response = YamlOperator.writeObjToYaml(response)
YamlOperator.writeObjToYaml((new File(request.mock_file)), response)
}
}
null
</code></pre>
*/
class GitHubAppCredentialTest extends GroovyTestCase {
String hash = '7bb84c7e30164139b00e7f95fd0e801bfdfe190f7ffca08cc77ae0d8438be02b'
GitHubAppCredential app
GitHubAppRsaCredentialImpl rsaCred
EphemeralTokenCache tokenCred
String hash = '7bb84c7e30164139b00e7f95fd0e801bfdfe190f7ffca08cc77ae0d8438be02b'

// mock tracking variables
def myvault
def url
Map request_meta = [:]
List request_history = []
List metaResult() {
[request_history*.url.inspect(), request_history*.method.inspect(), request_history*.data.inspect(), request_history*.response_code.inspect()]
}

//set up before every test
@Before protected void setUp() {
super.setUp()
// mock network
mockStaticUrl(url, URL, request_meta, true, 'SHA-256', request_history)
// use in-memory token cache
this.tokenCred = new EphemeralTokenCache(true)
this.tokenCred.loadCache = null
Expand Down Expand Up @@ -84,4 +184,32 @@ class GitHubAppCredentialTest extends GroovyTestCase {
app.hash = 'overwrite hash'
}
}
@Test public void test_GitHubAppCredential_getToken_user() {
rsaCred.owner = 'samrocketman'
app.ownerIsUser = true
app.hash = ''

assert app.jwtToken == null
app.token == 'some-token'
assert app.jwtToken != null

List urls = ['https://api.github.com/users/samrocketman/installation', 'https://api.github.com/app/installations/38741780/access_tokens']
List methods = ['GET', 'POST']
assert request_history*.url == urls
assert request_history*.method == methods
}

@Test public void test_GitHubAppCredential_getToken_org() {
rsaCred.owner = 'sgleske-test'
app.hash = ''

assert app.jwtToken == null
app.token == 'some-token'
assert app.jwtToken != null

List urls = ['https://api.github.com/orgs/sgleske-test/installation', 'https://api.github.com/app/installations/32854008/access_tokens']
List methods = ['GET', 'POST']
assert request_history*.url == urls
assert request_history*.method == methods
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
token: some-token
expires_at: '2523-02-18T21:03:21.009943Z'
permissions:
organization_hooks: write
administration: read
contents: read
deployments: read
metadata: read
pull_requests: read
repository_hooks: write
statuses: write
repository_selection: all
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
null:
- HTTP/1.1 201 Created
Server:
- GitHub.com
Access-Control-Allow-Origin:
- '*'
X-Content-Type-Options:
- nosniff
x-github-api-version-selected:
- '2022-11-28'
Date:
- Mon, 19 Jun 2023 21:03:20 GMT
Referrer-Policy:
- origin-when-cross-origin, strict-origin-when-cross-origin
X-Frame-Options:
- deny
Strict-Transport-Security:
- max-age=31536000; includeSubdomains; preload
Access-Control-Expose-Headers:
- ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining,
X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes,
X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation,
Sunset
Cache-Control:
- public, max-age=60, s-maxage=60
X-GitHub-Media-Type:
- github.v3; format=json
ETag:
- '"5747c0ecd546a73e9f9e739f9f640da707e136ce4d901568ebe17ea6aebcaca1"'
Content-Security-Policy:
- default-src 'none'
Vary:
- Accept-Encoding, Accept, X-Requested-With
- Accept
X-XSS-Protection:
- '0'
Content-Length:
- '312'
X-GitHub-Request-Id:
- 9062:04AB:10E607E:224BEA8:6490C298
Content-Type:
- application/json; charset=utf-8
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
token: some-token
expires_at: '2523-02-18T21:03:21.011905Z'
permissions:
administration: read
contents: read
deployments: read
metadata: read
pull_requests: read
repository_hooks: write
statuses: write
repository_selection: all
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
null:
- HTTP/1.1 201 Created
Server:
- GitHub.com
Access-Control-Allow-Origin:
- '*'
X-Content-Type-Options:
- nosniff
x-github-api-version-selected:
- '2022-11-28'
Date:
- Mon, 19 Jun 2023 21:03:20 GMT
Referrer-Policy:
- origin-when-cross-origin, strict-origin-when-cross-origin
X-Frame-Options:
- deny
Strict-Transport-Security:
- max-age=31536000; includeSubdomains; preload
Access-Control-Expose-Headers:
- ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining,
X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes,
X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation,
Sunset
Cache-Control:
- public, max-age=60, s-maxage=60
X-GitHub-Media-Type:
- github.v3; format=json
ETag:
- '"dba6e4a6db1c0b57a78b0ec3a635ce26f7bbc730ce0568fb4e5f7aed90df2cba"'
Content-Security-Policy:
- default-src 'none'
Vary:
- Accept-Encoding, Accept, X-Requested-With
- Accept
X-XSS-Protection:
- '0'
Content-Length:
- '283'
X-GitHub-Request-Id:
- 9062:04AB:10E60E0:224BF6E:6490C298
Content-Type:
- application/json; charset=utf-8
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
id: 32854008
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
null:
- HTTP/1.1 200 OK
Server:
- GitHub.com
Access-Control-Allow-Origin:
- '*'
X-Content-Type-Options:
- nosniff
x-github-api-version-selected:
- '2022-11-28'
Date:
- Mon, 19 Jun 2023 21:03:20 GMT
Referrer-Policy:
- origin-when-cross-origin, strict-origin-when-cross-origin
X-Frame-Options:
- deny
Strict-Transport-Security:
- max-age=31536000; includeSubdomains; preload
Access-Control-Expose-Headers:
- ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining,
X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes,
X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation,
Sunset
Cache-Control:
- public, max-age=60, s-maxage=60
X-GitHub-Media-Type:
- github.v3; format=json
ETag:
- '"026ac6df67b63882e122c9225ad6d6c914a5eaac01036a3cd89c5e7971728938"'
Content-Security-Policy:
- default-src 'none'
Vary:
- Accept-Encoding, Accept, X-Requested-With
- Accept
X-XSS-Protection:
- '0'
Content-Length:
- '1831'
X-GitHub-Request-Id:
- 9062:04AB:10E6064:224BE5C:6490C298
Content-Type:
- application/json; charset=utf-8
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
id: 38741780
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
null:
- HTTP/1.1 200 OK
Server:
- GitHub.com
Access-Control-Allow-Origin:
- '*'
X-Content-Type-Options:
- nosniff
x-github-api-version-selected:
- '2022-11-28'
Date:
- Mon, 19 Jun 2023 21:03:20 GMT
Referrer-Policy:
- origin-when-cross-origin, strict-origin-when-cross-origin
X-Frame-Options:
- deny
Strict-Transport-Security:
- max-age=31536000; includeSubdomains; preload
Access-Control-Expose-Headers:
- ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining,
X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes,
X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation,
Sunset
Cache-Control:
- public, max-age=60, s-maxage=60
X-GitHub-Media-Type:
- github.v3; format=json
ETag:
- '"c6e3627fe9ee0dc1726621157b5c004e50ff26530a1aa9eacc2ff3db27fd4b7e"'
Content-Security-Policy:
- default-src 'none'
Vary:
- Accept-Encoding, Accept, X-Requested-With
- Accept
X-XSS-Protection:
- '0'
Content-Length:
- '1741'
X-GitHub-Request-Id:
- 9062:04AB:10E60AB:224BF15:6490C298
Content-Type:
- application/json; charset=utf-8

0 comments on commit 6e9b540

Please sign in to comment.