package org.kohsuke.github;

import com.jcraft.jsch.IO;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;

public class GHOrganizationTest extends AbstractGitHubWireMockTest {

    public static final String GITHUB_API_TEST = "github-api-test";


    @Test
    public void testCreateRepository() throws IOException {
        cleanupRepository(GITHUB_API_TEST_ORG + '/' + GITHUB_API_TEST);

        GHOrganization org = gitHub.getOrganization(GITHUB_API_TEST_ORG);
        GHRepository repository = org.createRepository(GITHUB_API_TEST,
            "a test repository used to test kohsuke's github-api", "http://github-api.kohsuke.org/", "Core Developers", true);
        Assert.assertNotNull(repository);
    }

    @Test
    public void testCreateRepositoryWithAutoInitialization() throws IOException {
        cleanupRepository(GITHUB_API_TEST_ORG + '/' + GITHUB_API_TEST);

        GHOrganization org = gitHub.getOrganization(GITHUB_API_TEST_ORG);
        GHRepository repository = org.createRepository(GITHUB_API_TEST)
            .description("a test repository used to test kohsuke's github-api")
            .homepage("http://github-api.kohsuke.org/")
            .team(org.getTeamByName("Core Developers"))
            .autoInit(true).create();
        Assert.assertNotNull(repository);
        Assert.assertNotNull(repository.getReadme());
    }

    @Test
    public void testInviteUser() throws IOException {
        GHOrganization org = gitHub.getOrganization(GITHUB_API_TEST_ORG);
        GHUser user = gitHub.getUser("martinvanzijl2");

        // First remove the user
        if (org.hasMember(user)) {
            org.remove(user);
        }

        // Then invite the user again
        org.add(user, GHOrganization.Role.MEMBER);

        // Now the user has to accept the invitation
        // Can this be automated?
        // user.acceptInvitationTo(org); // ?

        // Check the invitation has worked.
        // assertTrue(org.hasMember(user));
    }

    @Test
    public void testCreateTeamWithRepoAccess() throws IOException {
        String REPO_NAME = "github-api";
        String TEAM_NAME = "create-team-test";

        GHOrganization org = gitHub.getOrganization(GITHUB_API_TEST_ORG);
        GHRepository repo = org.getRepository(REPO_NAME);

        // Clean up team if exists.
        GHTeam existingTeam = org.getTeamByName(TEAM_NAME);
        if (existingTeam != null) {
            existingTeam.delete();
        }

        // Create team with access to repository. Check access was granted.
        GHTeam team = org.createTeam(TEAM_NAME, GHOrganization.Permission.PUSH, repo);
        Assert.assertTrue(team.getRepositories().containsKey(REPO_NAME));
    }
}

@@ -2040,32 +2040,415 @@
 (user));%0A    %7D%0A%0A
+    @Before%0A    public void cleanUpTeam() throws IOException %7B%0A        // Cleanup is only needed when proxying%0A        if (!mockGitHub.isUseProxy()) %7B%0A            return;%0A        %7D%0A%0A        GHTeam team = gitHubBeforeAfter.getOrganization(GITHUB_API_TEST_ORG).%0A                getTeamByName(%22create-team-test%22);%0A        if (team != null) %7B%0A            team.delete();%0A        %7D%0A    %7D%0A%0A
     @Test%0A    pu
@@ -2433,32 +2433,32 @@
    %7D%0A%0A    @Test%0A
-
     public void 
@@ -2553,55 +2553,8 @@
 pi%22;
-%0A        String TEAM_NAME = %22create-team-test%22;
 %0A%0A  
@@ -2688,186 +2688,8 @@
 );%0A%0A
-        // Clean up team if exists.%0A        GHTeam existingTeam = org.getTeamByName(TEAM_NAME);%0A        if (existingTeam != null) %7B%0A            existingTeam.delete();%0A        %7D%0A%0A
     
@@ -2801,17 +2801,26 @@
 eam(
-TEAM_NAME
+%22create-team-test%22
 , GH

Move team cleanup to @Before method as per GitHub review.