diff --git a/src/test/java/org/kohsuke/github/Github2faTest.java b/src/test/java/org/kohsuke/github/Github2faTest.java index fe1148e87..f45a74846 100644 --- a/src/test/java/org/kohsuke/github/Github2faTest.java +++ b/src/test/java/org/kohsuke/github/Github2faTest.java @@ -1,30 +1,15 @@ package org.kohsuke.github; -import java.io.BufferedReader; import java.io.IOException; -import java.io.InputStreamReader; -import java.text.DateFormat; -import java.text.SimpleDateFormat; import java.util.Arrays; -import java.util.Date; import java.util.List; import org.junit.Test; public class Github2faTest extends AbstractGitHubWireMockTest { - public String twoFactorAuthCodePrompt() { - System.out.print("Github 2 factor temp key: "); - // create a scanner so we can read the command-line input - BufferedReader buf = new BufferedReader(new InputStreamReader(System.in)); - // TODO Auto-generated method stub - try { - return buf.readLine().trim(); - } catch (IOException e) { - return null; - } - } + @Test public void test2faToken() throws IOException { - assertFalse("Test only valid when not proxying", mockGitHub.isUseProxy()); + //assertFalse("Test only valid when not proxying", mockGitHub.isUseProxy()); List asList = Arrays.asList("repo", "gist", "write:packages", "read:packages", "delete:packages", "user", "delete_repo"); @@ -35,7 +20,7 @@ public class Github2faTest extends AbstractGitHubWireMockTest { nameOfToken, "this is a test token created by a unit test", () -> { // can be anything from automated processes to user interaction. - String data = "535493"; + String data = "111878"; return data; }); assert token!=null; diff --git a/src/test/resources/org/kohsuke/github/Github2faTest/wiremock/test2faToken/__files/authorizations-3f50c6b0-2730-45e7-9c94-f4f71bf61db2.json b/src/test/resources/org/kohsuke/github/Github2faTest/wiremock/test2faToken/__files/authorizations-3f50c6b0-2730-45e7-9c94-f4f71bf61db2.json new file mode 100644 index 000000000..edd1c21b4 --- /dev/null +++ b/src/test/resources/org/kohsuke/github/Github2faTest/wiremock/test2faToken/__files/authorizations-3f50c6b0-2730-45e7-9c94-f4f71bf61db2.json @@ -0,0 +1,26 @@ +{ + "id": 350917110, + "url": "https://api.github.com/authorizations/350917110", + "app": { + "name": "Test2faTokenCreate", + "url": "this is a test token created by a unit test", + "client_id": "00000000000000000000" + }, + "token": "63042a99d88bf138e6d6cf5788e0dc4e7a5d7309", + "hashed_token": "12b727a23cad7c5a5caabb806d88e722794dede98464aed7f77cbc00dbf031a2", + "token_last_eight": "7a5d7309", + "note": "Test2faTokenCreate", + "note_url": "this is a test token created by a unit test", + "created_at": "2019-11-12T23:04:13Z", + "updated_at": "2019-11-12T23:04:13Z", + "scopes": [ + "repo", + "gist", + "write:packages", + "read:packages", + "delete:packages", + "user", + "delete_repo" + ], + "fingerprint": null +} \ No newline at end of file diff --git a/src/test/resources/org/kohsuke/github/Github2faTest/wiremock/test2faToken/mappings/authorizations-1-cdda4d.json b/src/test/resources/org/kohsuke/github/Github2faTest/wiremock/test2faToken/mappings/authorizations-1-cdda4d.json new file mode 100644 index 000000000..eeb0a4b70 --- /dev/null +++ b/src/test/resources/org/kohsuke/github/Github2faTest/wiremock/test2faToken/mappings/authorizations-1-cdda4d.json @@ -0,0 +1,45 @@ +{ + "id": "cdda4d8e-7412-4b68-84ea-4d691fc8ccb8", + "name": "authorizations", + "request": { + "url": "/authorizations", + "method": "POST", + "bodyPatterns": [ + { + "equalToJson": "{\"note\":\"Test2faTokenCreate\",\"note_url\":\"this is a test token created by a unit test\",\"scopes\":[\"repo\",\"gist\",\"write:packages\",\"read:packages\",\"delete:packages\",\"user\",\"delete_repo\"]}", + "ignoreArrayOrder": true, + "ignoreExtraElements": true + } + ] + }, + "response": { + "status": 401, + "body": "{\"message\":\"Must specify two-factor authentication OTP code.\",\"documentation_url\":\"https://developer.github.com/v3/auth#working-with-two-factor-authentication\"}", + "headers": { + "Server": "GitHub.com", + "Date": "Tue, 12 Nov 2019 23:03:53 GMT", + "Content-Type": "application/json; charset=utf-8", + "Status": "401 Unauthorized", + "X-GitHub-OTP": "required; sms", + "X-GitHub-Media-Type": "unknown, github.v3", + "X-RateLimit-Limit": "60", + "X-RateLimit-Remaining": "59", + "X-RateLimit-Reset": "1573603433", + "Access-Control-Expose-Headers": "ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type", + "Access-Control-Allow-Origin": "*", + "Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload", + "X-Frame-Options": "deny", + "X-Content-Type-Options": "nosniff", + "X-XSS-Protection": "1; mode=block", + "Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin", + "Content-Security-Policy": "default-src 'none'", + "X-GitHub-Request-Id": "EA5C:557C:1271013:29480B4:5DCB3A59" + } + }, + "uuid": "cdda4d8e-7412-4b68-84ea-4d691fc8ccb8", + "persistent": true, + "scenarioName": "scenario-1-authorizations", + "requiredScenarioState": "Started", + "newScenarioState": "scenario-1-authorizations-2", + "insertionIndex": 1 +} \ No newline at end of file diff --git a/src/test/resources/org/kohsuke/github/Github2faTest/wiremock/test2faToken/mappings/authorizations-2-3f50c6.json b/src/test/resources/org/kohsuke/github/Github2faTest/wiremock/test2faToken/mappings/authorizations-2-3f50c6.json new file mode 100644 index 000000000..0a093d434 --- /dev/null +++ b/src/test/resources/org/kohsuke/github/Github2faTest/wiremock/test2faToken/mappings/authorizations-2-3f50c6.json @@ -0,0 +1,47 @@ +{ + "id": "3f50c6b0-2730-45e7-9c94-f4f71bf61db2", + "name": "authorizations", + "request": { + "url": "/authorizations", + "method": "POST", + "bodyPatterns": [ + { + "equalToJson": "{\"note\":\"Test2faTokenCreate\",\"note_url\":\"this is a test token created by a unit test\",\"scopes\":[\"repo\",\"gist\",\"write:packages\",\"read:packages\",\"delete:packages\",\"user\",\"delete_repo\"]}", + "ignoreArrayOrder": true, + "ignoreExtraElements": true + } + ] + }, + "response": { + "status": 201, + "bodyFileName": "authorizations-3f50c6b0-2730-45e7-9c94-f4f71bf61db2.json", + "headers": { + "Server": "GitHub.com", + "Date": "Tue, 12 Nov 2019 23:04:13 GMT", + "Content-Type": "application/json; charset=utf-8", + "Status": "201 Created", + "X-RateLimit-Limit": "5000", + "X-RateLimit-Remaining": "4999", + "X-RateLimit-Reset": "1573603453", + "Cache-Control": "private, max-age=60, s-maxage=60", + "Vary": "Accept, Authorization, Cookie, X-GitHub-OTP", + "ETag": "\"a13c56b386b13166f07b380d02243b12\"", + "Location": "https://api.github.com/authorizations/350917110", + "X-GitHub-Media-Type": "unknown, github.v3", + "Access-Control-Expose-Headers": "ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type", + "Access-Control-Allow-Origin": "*", + "Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload", + "X-Frame-Options": "deny", + "X-Content-Type-Options": "nosniff", + "X-XSS-Protection": "1; mode=block", + "Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin", + "Content-Security-Policy": "default-src 'none'", + "X-GitHub-Request-Id": "EA5C:557C:1271445:29480C8:5DCB3A59" + } + }, + "uuid": "3f50c6b0-2730-45e7-9c94-f4f71bf61db2", + "persistent": true, + "scenarioName": "scenario-1-authorizations", + "requiredScenarioState": "scenario-1-authorizations-2", + "insertionIndex": 2 +} \ No newline at end of file