Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f9874ee61e97fbeeb2696daf4a23298b9127f188 (plain) (blame)
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
/******************************************************************************
 *  Copyright (c) 2011 GitHub Inc.
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License 2.0
 *  which accompanies this distribution, and is available at
 *  https://www.eclipse.org/legal/epl-2.0/
 *
 *  SPDX-License-Identifier: EPL-2.0
 *
 *  Contributors:
 *    Kevin Sawicki (GitHub Inc.) - initial API and implementation
 *****************************************************************************/
package org.eclipse.egit.github.core.tests.live;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.List;

import org.eclipse.egit.github.core.Key;
import org.eclipse.egit.github.core.RepositoryId;
import org.eclipse.egit.github.core.client.RequestException;
import org.eclipse.egit.github.core.service.DeployKeyService;
import org.junit.Test;

/**
 * Unit tests of {@link DeployKeyService}
 */
public class DeployKeyTest extends LiveTest {

	/**
	 * Create, fetch, and delete repository deploy key
	 * 
	 * @throws Exception
	 */
	@Test
	public void createFetchDeleteKey() throws Exception {
		checkUser();
		assertNotNull("Repo is required for test", writableRepo);

		RepositoryId repo = RepositoryId.create(client.getUser(), writableRepo);
		DeployKeyService service = new DeployKeyService(client);
		Key key = new Key();
		key.setTitle("key" + System.currentTimeMillis());
		key.setKey("ssh-rsa " + System.nanoTime());
		Key created = service.createKey(repo, key);
		assertNotNull(created);
		assertEquals(key.getTitle(), created.getTitle());
		assertEquals(key.getKey(), created.getKey());
		assertNotNull(created.getUrl());
		assertTrue(created.getId() > 0);

		Key fetch = service.getKey(repo, created.getId());
		assertNotNull(fetch);
		assertEquals(created.getTitle(), fetch.getTitle());
		assertEquals(created.getKey(), fetch.getKey());
		assertEquals(created.getUrl(), fetch.getUrl());
		assertEquals(created.getId(), fetch.getId());

		Key found = null;
		List<Key> keys = service.getKeys(repo);
		assertNotNull(keys);
		assertFalse(keys.isEmpty());
		for (Key possible : service.getKeys(repo)) {
			if (created.getId() == possible.getId()) {
				found = possible;
				break;
			}
		}
		assertNotNull(found);
		assertEquals(created.getTitle(), found.getTitle());
		assertEquals(created.getKey(), found.getKey());
		assertEquals(created.getUrl(), found.getUrl());
		assertEquals(created.getId(), found.getId());

		service.deleteKey(repo, created.getId());
		try {
			service.getKey(repo, created.getId());
			fail("Request exception not thrown");
		} catch (RequestException e) {
			assertEquals(404, e.getStatus());
		}
	}

	/**
	 * Create, edit and delete a deploy key
	 * 
	 * @throws Exception
	 */
	@Test
	public void createEditDeleteKey() throws Exception {
		checkUser();
		assertNotNull("Repo is required for test", writableRepo);

		RepositoryId repo = RepositoryId.create(client.getUser(), writableRepo);
		DeployKeyService service = new DeployKeyService(client);
		Key key = new Key();
		key.setTitle("key" + System.currentTimeMillis());
		key.setKey("ssh-rsa " + System.nanoTime());
		Key created = service.createKey(repo, key);
		assertNotNull(created);
		assertEquals(key.getTitle(), created.getTitle());
		assertEquals(key.getKey(), created.getKey());
		assertNotNull(created.getUrl());
		assertTrue(created.getId() > 0);

		Key fetch = service.getKey(repo, created.getId());
		assertNotNull(fetch);
		assertEquals(created.getTitle(), fetch.getTitle());
		assertEquals(created.getKey(), fetch.getKey());
		assertEquals(created.getUrl(), fetch.getUrl());
		assertEquals(created.getId(), fetch.getId());
		fetch.setTitle("new title");

		Key edited = service.editKey(repo, fetch);
		assertNotNull(edited);
		assertNotSame(fetch, edited);
		assertEquals(fetch.getTitle(), edited.getTitle());
		assertEquals(fetch.getKey(), edited.getKey());
		assertEquals(fetch.getUrl(), edited.getUrl());
		assertEquals(fetch.getId(), edited.getId());

		service.deleteKey(repo, created.getId());
		try {
			service.getKey(repo, created.getId());
			fail("Request exception not thrown");
		} catch (RequestException e) {
			assertEquals(404, e.getStatus());
		}
	}
}

Back to the top