Skip to content
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

Fixes oauth/access_token Interpreter #509

Merged
merged 3 commits into from
Jun 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion github4s/src/main/scala/github4s/Encoders.scala
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ object Encoders {
implicit val encoderSubscriptionRequest: Encoder[SubscriptionRequest] =
deriveEncoder[SubscriptionRequest]
implicit val encoderNewAuthRequest: Encoder[NewAuthRequest] = deriveEncoder[NewAuthRequest]
implicit val encoderNewOAuthRequest: Encoder[NewOAuthRequest] = deriveEncoder[NewOAuthRequest]
implicit val encoderNewGistRequest: Encoder[NewGistRequest] = deriveEncoder[NewGistRequest]
implicit val encoderEditGistRequest: Encoder[EditGistRequest] = deriveEncoder[EditGistRequest]
implicit val encoderNewIssueRequest: Encoder[NewIssueRequest] = deriveEncoder[NewIssueRequest]
Expand Down
8 changes: 0 additions & 8 deletions github4s/src/main/scala/github4s/domain/Authorization.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,3 @@ final case class OAuthToken(
token_type: String,
scope: String
)

final case class NewOAuthRequest(
client_id: String,
client_secret: String,
code: String,
redirect_uri: String,
state: String
)
8 changes: 4 additions & 4 deletions github4s/src/main/scala/github4s/http/HttpClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,15 @@ class HttpClient[F[_]: Sync](client: Client[F], val config: GithubConfig) {
): F[GHResponse[Res]] =
run[Req, Res](RequestBuilder(buildURL(method)).postMethod.withHeaders(headers).withData(data))

def postOAuth[Req: Encoder, Res: Decoder](
def postOAuth[Res: Decoder](
url: String,
headers: Map[String, String] = Map.empty,
data: Req
params: Map[String, String] = Map.empty
): F[GHResponse[Res]] =
run[Req, Res](
run[Unit, Res](
RequestBuilder(url).postMethod
.withHeaders(Map("Accept" -> "application/json") ++ headers)
.withData(data)
.withParams(params)
)

def delete(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,15 @@ class AuthInterpreter[F[_]: Applicative](implicit
state: String,
headers: Map[String, String]
): F[GHResponse[OAuthToken]] =
client.postOAuth[NewOAuthRequest, OAuthToken](
client.postOAuth[OAuthToken](
url = client.config.accessTokenUrl,
headers = headers,
data = NewOAuthRequest(client_id, client_secret, code, redirect_uri, state)
Map(
"client_id" -> client_id,
"client_secret" -> client_secret,
"code" -> code,
"redirect_uri" -> redirect_uri,
"state" -> state
)
)
}
4 changes: 2 additions & 2 deletions github4s/src/test/scala/github4s/integration/AuthSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ trait AuthSpec extends BaseIntegrationSpec {
}
.unsafeRunSync()

testIsLeft[GHError.BasicError, OAuthToken](response)
response.statusCode shouldBe notFoundStatusCode
testIsLeft[GHError.JsonParsingError, OAuthToken](response)
response.statusCode shouldBe okStatusCode
}

}
11 changes: 1 addition & 10 deletions github4s/src/test/scala/github4s/unit/AuthSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,8 @@ class AuthSpec extends BaseSpec {
val response: IO[GHResponse[OAuthToken]] =
IO(GHResponse(oAuthToken.asRight, okStatusCode, Map.empty))

val request = NewOAuthRequest(
validClientId,
invalidClientSecret,
validCode,
validRedirectUri,
validAuthState
)

implicit val httpClientMock = httpClientMockPostOAuth[NewOAuthRequest, OAuthToken](
implicit val httpClientMock = httpClientMockPostOAuth[OAuthToken](
url = dummyConfig.accessTokenUrl,
req = request,
response = response
)

Expand Down
8 changes: 3 additions & 5 deletions github4s/src/test/scala/github4s/utils/BaseSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,16 @@ trait BaseSpec extends AnyFlatSpec with Matchers with TestData with MockFactory
httpClientMock
}

def httpClientMockPostOAuth[In, Out](
def httpClientMockPostOAuth[Out](
url: String,
req: In,
response: IO[GHResponse[Out]]
): HttpClient[IO] = {
val httpClientMock = mock[HttpClientTest]
(httpClientMock
.postOAuth[In, Out](_: String, _: Map[String, String], _: In)(
_: Encoder[In],
.postOAuth[Out](_: String, _: Map[String, String], _: Map[String, String])(
_: Decoder[Out]
))
.expects(url, headerUserAgent, req, *, *)
.expects(url, headerUserAgent, *, *)
.returns(response)
httpClientMock
}
Expand Down