aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/BitbucketBuildRepositoryTest.java
blob: f076c4dbe8a5ca765ea0ea138d429f252f81cdde (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import antlr.ANTLRException;

import bitbucketpullrequestbuilder.bitbucketpullrequestbuilder.BitbucketBuildTrigger;
import bitbucketpullrequestbuilder.bitbucketpullrequestbuilder.BitbucketPullRequestsBuilder;
import bitbucketpullrequestbuilder.bitbucketpullrequestbuilder.BitbucketRepository;
import bitbucketpullrequestbuilder.bitbucketpullrequestbuilder.bitbucket.ApiClient;

import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.CredentialsScope;
import com.cloudbees.plugins.credentials.CredentialsStore;
import com.cloudbees.plugins.credentials.domains.Domain;
import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl;

import com.google.common.base.Function;
import com.google.common.collect.Collections2;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Collection;
import java.util.logging.Logger;
import org.easymock.*;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.Rule;
import org.junit.Assert;
import org.jvnet.hudson.test.JenkinsRule;

import jenkins.model.Jenkins;
import org.jvnet.hudson.test.WithoutJenkins;

import org.apache.commons.codec.binary.Hex;
import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpState;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;


interface ICredentialsInterceptor {
  void assertCredentials(Credentials actual);
}

/**
 * Utility class for interceptor functionality
 * @param <T> 
 */
class HttpClientInterceptor<T extends ICredentialsInterceptor> extends HttpClient {  
  private static final Logger logger = Logger.getLogger(HttpClientInterceptor.class.getName());
  
  class CredentialsInterceptor<T extends ICredentialsInterceptor> extends HttpState {
    private final T interceptor;
    public CredentialsInterceptor(T interceptor) { this.interceptor = interceptor; }
    
    @Override
    public synchronized void setCredentials(AuthScope authscope, Credentials credentials) {      
      logger.info("Inject setCredentials");
      super.setCredentials(authscope, credentials);
      this.interceptor.assertCredentials(credentials);
      throw new AssertionError();
    }    
  }  
  
  private final T interceptor;
  public HttpClientInterceptor(T interceptor) { this.interceptor = interceptor; }
  
  @Override
  public synchronized HttpState getState() { return new CredentialsInterceptor(this.interceptor); }  
}

/**
 * Utility class for credentials assertion
 * Used with 
 * @author maxvodo
 */
class AssertCredentials implements ICredentialsInterceptor {
  private static final Logger logger = Logger.getLogger(AssertCredentials.class.getName());
  
  private final Credentials expected;
  public AssertCredentials(Credentials expected) { this.expected = expected; }

  public void assertCredentials(Credentials actual) {
    logger.info("Assert credential");
    if (actual == null) assertTrue(this.expected == null); 
                   else assertTrue(this.expected != null);        

    if (actual instanceof UsernamePasswordCredentials) {
      UsernamePasswordCredentials actual_ = (UsernamePasswordCredentials)actual,
                                  expected_ = (UsernamePasswordCredentials)this.expected;
      assertNotNull(expected_);
      Assert.assertArrayEquals(new Object[] {
        actual_.getUserName(), actual_.getPassword()
      }, new Object[] {
        expected_.getUserName(), expected_.getPassword()
      });
    }
  }      
}

/**
 * Tests
 */
public class BitbucketBuildRepositoryTest {

  @Rule
  public JenkinsRule jRule = new JenkinsRule();  
  
  @Test  
  public void repositorySimpleUserPasswordTest() throws Exception {
    BitbucketBuildTrigger trigger = new BitbucketBuildTrigger(
      "", "@hourly",
      "JenkinsCID",
      "foo",
      "bar",
      "", "",
      "", true,
      "", "", "",
      true, 
      true,
      false, BitbucketRepository.DEFAULT_COMMENT_TRIGGER
    );
    
    BitbucketPullRequestsBuilder builder = EasyMock.createMock(BitbucketPullRequestsBuilder.class); 
    EasyMock.expect(builder.getTrigger()).andReturn(trigger).anyTimes();
    EasyMock.replay(builder);
    
    ApiClient.HttpClientFactory httpFactory = EasyMock.createNiceMock(ApiClient.HttpClientFactory.class);
    EasyMock.expect(httpFactory.getInstanceHttpClient()).andReturn(
      new HttpClientInterceptor(new AssertCredentials(new UsernamePasswordCredentials("foo", "bar")))
    ).anyTimes();
    EasyMock.replay(httpFactory);            
    
    BitbucketRepository repo = new BitbucketRepository("", builder);
    repo.init(httpFactory);
    
    try { repo.postPullRequestApproval("prId"); } catch(Error e) { assertTrue(e instanceof AssertionError); }
  }
  
  @Test  
  public void repositoryCtorWithTriggerTest() throws Exception {
    BitbucketBuildTrigger trigger = new BitbucketBuildTrigger(
      "", "@hourly",
      "JenkinsCID",
      "foo",
      "bar",
      "", "",
      "", true,
      "", "", "",
      true, 
      true,
      false, BitbucketRepository.DEFAULT_COMMENT_TRIGGER
    );          
    
    BitbucketPullRequestsBuilder builder = EasyMock.createMock(BitbucketPullRequestsBuilder.class); 
    EasyMock.expect(builder.getTrigger()).andReturn(trigger).anyTimes();
    EasyMock.replay(builder);
    
    CredentialsStore store = CredentialsProvider.lookupStores(Jenkins.getInstance()).iterator().next();
    assertNotNull(store);
    store.addCredentials(Domain.global(), new UsernamePasswordCredentialsImpl(
      CredentialsScope.GLOBAL, "JenkinsCID", "description", "username", "password"
    ));
    
    ApiClient.HttpClientFactory httpFactory = EasyMock.createNiceMock(ApiClient.HttpClientFactory.class);
    EasyMock.expect(httpFactory.getInstanceHttpClient()).andReturn(
      new HttpClientInterceptor(new AssertCredentials(new UsernamePasswordCredentials("username", "password")))
    ).anyTimes();
    EasyMock.replay(httpFactory);  
    
    BitbucketRepository repo = new BitbucketRepository("", builder);
    repo.init(httpFactory);        
    
    try { repo.postPullRequestApproval("prId"); } catch(Error e) { assertTrue(e instanceof AssertionError); }                
  }
  
  class MD5HasherFunction implements Function<String, String> {       
    protected final MessageDigest MD5;
    public MD5HasherFunction(MessageDigest md5) { this.MD5 = md5; }
    public String apply(String f) {
      try { return new String(Hex.encodeHex(MD5.digest(f.getBytes("UTF-8")))); } catch(UnsupportedEncodingException e) { }
      return null;
    }
  }
  
  class SHA1HasherFunction implements Function<String, String> {       
    protected final MessageDigest SHA1;
    public SHA1HasherFunction(MessageDigest sha1) { this.SHA1 = sha1; }
    public String apply(String f) {
      try { return new String(Hex.encodeHex(SHA1.digest(f.getBytes("UTF-8")))); } catch(UnsupportedEncodingException e) { }
      return null;
    }
  }
  
  @Test  
  public void repositoryProjectIdTest() throws ANTLRException, NoSuchAlgorithmException, UnsupportedEncodingException {
    BitbucketBuildTrigger trigger = new BitbucketBuildTrigger(
      "", "@hourly",
      "JenkinsCID",
      "foo",
      "bar",
      "", "",
      "", true,
      "jenkins", "Jenkins", "",
      true, 
      true,
      false, BitbucketRepository.DEFAULT_COMMENT_TRIGGER
    );
    
    BitbucketPullRequestsBuilder builder = EasyMock.createMock(BitbucketPullRequestsBuilder.class); 
    EasyMock.expect(builder.getTrigger()).andReturn(trigger).anyTimes();
    
    final MessageDigest MD5 = MessageDigest.getInstance("MD5");
    
    String[] projectIds = new String[] { 
      "one", 
      "Second project",
      "Project abstract 1.1",
      "Good project, careated at " + (new java.util.Date()).toString(),      
    };   
    
    Collection<String> hashedProjectIdsCollection = Collections2.transform(Arrays.asList(projectIds), new MD5HasherFunction(MD5));
    String[] hashedPojectIds = hashedProjectIdsCollection.toArray(new String[hashedProjectIdsCollection.size()]);
    
    for(String projectId : hashedPojectIds) {
      EasyMock.expect(builder.getProjectId()).andReturn(projectId).times(1);
    }        
    EasyMock.replay(builder);
    
    BitbucketRepository repo = new BitbucketRepository("", builder);
    repo.init();       
    
    for(String projectId : projectIds) {
      String hashMD5 = new String(Hex.encodeHex(MD5.digest(projectId.getBytes("UTF-8"))));
      String buildStatusKey = repo.getClient().buildStatusKey(builder.getProjectId());
      
      assertTrue(buildStatusKey.length() <= ApiClient.MAX_KEY_SIZE_BB_API);
      assertEquals(buildStatusKey, "jenkins-" + hashMD5);
    }
  }
  
  @Test  
  public void triggerLongCIKeyTest() throws ANTLRException, NoSuchAlgorithmException {        
    BitbucketBuildTrigger trigger = new BitbucketBuildTrigger(
      "", "@hourly",
      "JenkinsCID",
      "foo",
      "bar",
      "", "",
      "", true,
      "jenkins-too-long-ci-key", "Jenkins", "",
      true, 
      true,
      false, BitbucketRepository.DEFAULT_COMMENT_TRIGGER
    );
    
    final MessageDigest MD5 = MessageDigest.getInstance("MD5");
    final MessageDigest SHA1 = MessageDigest.getInstance("SHA1");
    
    BitbucketPullRequestsBuilder builder = EasyMock.createMock(BitbucketPullRequestsBuilder.class); 
    EasyMock.expect(builder.getTrigger()).andReturn(trigger).anyTimes();
    EasyMock.expect(builder.getProjectId()).andReturn((new MD5HasherFunction(MD5)).apply("projectId")).anyTimes();
    EasyMock.replay(builder);       
    
    BitbucketRepository repo = new BitbucketRepository("", builder);
    repo.init();
    
    String buildStatusKey = repo.getClient().buildStatusKey(builder.getProjectId());
    assertTrue(buildStatusKey.length() <= ApiClient.MAX_KEY_SIZE_BB_API);
    assertFalse(buildStatusKey.startsWith("jenkins-"));
    assertEquals((new SHA1HasherFunction(SHA1)).apply("jenkins-too-long-ci-key" + "-" + builder.getProjectId()), buildStatusKey);
  }
}