Skip to content

Commit 8c7075a

Browse files
committed
Merge pull request #33 from code-check/30-UserEnv
30 user env
2 parents 95a9367 + 689848a commit 8c7075a

File tree

7 files changed

+128
-11
lines changed

7 files changed

+128
-11
lines changed

src/main/scala/codecheck/github/api/GitHubAPI.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ class GitHubAPI(token: String, client: AsyncHttpClient, tokenType: String = "tok
5353
request
5454
.setHeader("Authorization", s"$tokenType $token")
5555
.setHeader("Content-Type", "application/json")
56+
if (method == "PUT" && body == JNothing){
57+
request
58+
.setHeader("Content-Length", "0")
59+
}
5660
request.execute(new AsyncCompletionHandler[Response]() {
5761
def onCompleted(res: Response) = {
5862
val json = Option(res.getResponseBody).filter(_.length > 0).map(parseJson(_)).getOrElse(JNothing)

src/main/scala/codecheck/github/models/Collaborator.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ case class Collaborator(value: JValue) extends AbstractJson(value) {
88
def url = get("url")
99
def site_admin: Boolean = boolean("site_admin")
1010
}
11-
//case class CollaboratorInput extends AbstractInput
11+
//case class CollaboratorInput extends AbstractInput

src/main/scala/codecheck/github/operations/CollaboratorOp.scala

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,28 @@ trait CollaboratorOp {
2020
}
2121
)
2222
}
23+
24+
def isCollaborator(owner: String, repo: String, name: String): Future[Boolean] = {
25+
val path = s"/repos/${owner}/${repo}/collaborators/" + encode(name)
26+
exec("GET", path, fail404 = false).map { res =>
27+
res.statusCode match {
28+
case 404 => false
29+
case 204 => true
30+
}
31+
}
32+
}
33+
34+
def addCollaborator(owner: String, repo: String, name: String): Future[Boolean] = {
35+
val path = s"/repos/${owner}/${repo}/collaborators/" + encode(name)
36+
exec("PUT", path).map {
37+
_.statusCode == 204
38+
}
39+
}
40+
41+
def removeCollaborator(owner: String, repo: String, name: String): Future[Boolean] = {
42+
val path = s"/repos/${owner}/${repo}/collaborators/" + encode(name)
43+
exec("DELETE", path).map {
44+
_.statusCode == 204
45+
}
46+
}
2347
}

src/test/scala/CollaboratorOpSpec.scala

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import org.scalatest.path.FunSpec
2-
import codecheck.github.models.Collaborator
32
import scala.concurrent.Await
3+
import codecheck.github.models.Collaborator
4+
import codecheck.github.exceptions.GitHubAPIException
5+
import codecheck.github.exceptions.NotFoundException
46

57
class CollaboratorOpSpec extends FunSpec with Constants {
68

@@ -16,4 +18,44 @@ class CollaboratorOpSpec extends FunSpec with Constants {
1618
assert(c.site_admin == false)
1719
}
1820
}
21+
describe("isCollaborator"){
22+
it("if it is Collaborator"){
23+
val res = Await.result(api.addCollaborator(user, userRepo, collaboratorUser),TIMEOUT)
24+
assert(res)
25+
val res1 = Await.result(api.isCollaborator(user, userRepo, collaboratorUser),TIMEOUT)
26+
assert(res1 == true)
27+
var res2 = Await.result(api.removeCollaborator(user, userRepo, collaboratorUser),TIMEOUT)
28+
assert(res2)
29+
}
30+
it("if it is not a valid Collaborator"){
31+
val res1 = Await.result(api.isCollaborator(organization, repo, otherUserInvalid),TIMEOUT)
32+
assert(res1 == false)
33+
}
34+
}
35+
describe("addCollaborator"){
36+
it("should add Collaborator User to user Repo"){
37+
val res = Await.result(api.addCollaborator(user, userRepo, collaboratorUser),TIMEOUT)
38+
assert(res)
39+
}
40+
it("should fail for non existent User Repo"){
41+
val res = Await.result(api.addCollaborator(user, repoInvalid, collaboratorUser).failed,TIMEOUT)
42+
res match {
43+
case e: NotFoundException =>
44+
case _ => fail
45+
}
46+
}
47+
}
48+
describe("removeCollaborator"){
49+
it("should remove the Collaborator"){
50+
var res = Await.result(api.removeCollaborator(user, userRepo, collaboratorUser),TIMEOUT)
51+
assert(res)
52+
}
53+
}
54+
it("should fail for non existent User Repo"){
55+
var res = Await.result(api.removeCollaborator(user, repoInvalid, collaboratorUser).failed,TIMEOUT)
56+
res match {
57+
case e: NotFoundException =>
58+
case _ => fail
59+
}
60+
}
1961
}

src/test/scala/Constants.scala

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ trait Constants {
1212
protected val TIMEOUT = 5 seconds
1313
protected val api = Constants.API
1414

15-
protected val user = "fanwashere" //REQUIRED: Edit this to your own username.
16-
1715
//Request membership of dummy organization "celestialbeing" if you are not member. Do not edit.
1816
protected val organization = "celestialbeings"
1917
protected val repo = "test-repo"
@@ -27,13 +25,18 @@ trait Constants {
2725
println(v)
2826
}
2927
}
30-
protected val otherUser = "shunjikonishi"
28+
29+
protected val user = sys.env("GITHUB_USER")
30+
protected val userRepo = sys.env("GITHUB_REPO")
31+
32+
protected val otherUser = "shunjikonishi"
33+
protected val collaboratorUser = "givery-dev"
3134
protected val otherUserInvalid = "loremipsom123"
3235
protected val organizationInvalid = "loremipsom123"
3336
protected val repoInvalid = "loremipsom123"
3437

3538
val wordBank = Array("Alpha", "Beta", "Gamma", "Delta", "Epsilon", "Zeta", "Theta", "Lambda", "Pi", "Sigma")
36-
def generateRandomString: String =
39+
def generateRandomString: String =
3740
wordBank(nextInt(10)) + " " + wordBank(nextInt(10)) + " " + wordBank(nextInt(10))
3841
def generateRandomWord: String = wordBank(nextInt(10))
3942
def generateRandomInt: Int = nextInt(1000)
@@ -44,4 +47,4 @@ object Constants {
4447
implicit val client = new AsyncHttpClient()
4548

4649
val API = GitHubAPI(token)
47-
}
50+
}

src/test/scala/README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22
This readme documents the changes to the tests.
33

44
## Initial Setup
5-
1. Change variable user in Constants.scala to your own Github username.
5+
1. Please export username and repository that exists under your name.
66
2. If you have no yet exported your Github token, create one [here](https://github.com/settings/tokens) and export it.
77

88
``` bash
9-
export GITHUB_TOKEN=[Your GitHub Token]
9+
export GITHUB_TOKEN=[Your GitHub Token]
10+
export GITHUB_USER=[Your User Name]
11+
export GITHUB_REPO=[Your Repo name that exists]
1012
```
1113

1214
## Optional settings
13-
- showResponse
15+
- showResponse
1416
-- Set this to true if you would like to see the response JSON data. Otherwise it is omitted when running tests.
1517

1618
The following variables are for futureproofing. Generally won't need to be modified.
@@ -37,4 +39,4 @@ The random string generator is located in Constants.scala and uses words from th
3739
-- Returns a String with a single random word.
3840
- generatedRandomInt()
3941
-- Returns a random Int from 0 to 999. This was added to avoid having to import the Random class in every file (so it is bundled with Constants)
40-
Use these to generate random field values to test create and update functions.
42+
Use these to generate random field values to test create and update functions.

test.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Tests
2+
This readme documents the changes to the tests.
3+
4+
## Initial Setup
5+
1. Please export username and repository that exists under your name.
6+
2. If you have no yet exported your Github token, create one [here](https://github.com/settings/tokens) and export it.
7+
8+
``` bash
9+
export GITHUB_TOKEN=[Your GitHub Token]
10+
export GITHUB_USER=[Your User Name]
11+
export GITHUB_REPO=[Your Repo name that exists]
12+
```
13+
14+
## Optional settings
15+
- showResponse
16+
-- Set this to true if you would like to see the response JSON data. Otherwise it is omitted when running tests.
17+
18+
The following variables are for futureproofing. Generally won't need to be modified.
19+
- otherUser
20+
-- Another user's (not yourself) username.
21+
- otherUserInvalid
22+
-- An invalid username.
23+
- organizationInvalid
24+
-- An invalid organization.
25+
- repoInvalid
26+
-- An invalid repo.
27+
28+
The following variables should not be changed.
29+
- organization
30+
-- This is by default set to our dummy test organization "celestialbeings".
31+
- repo
32+
-- This is by default set to the dummy est repo "test-repo".
33+
34+
## Random Generator
35+
The random string generator is located in Constants.scala and uses words from the wordBank array. It has three methods.
36+
- generateRandomString()
37+
-- Returns a String with three random words seperated by spaces.
38+
- generateRandomWord()
39+
-- Returns a String with a single random word.
40+
- generatedRandomInt()
41+
-- Returns a random Int from 0 to 999. This was added to avoid having to import the Random class in every file (so it is bundled with Constants)
42+
Use these to generate random field values to test create and update functions.

0 commit comments

Comments
 (0)