|
| 1 | +/** @fileoverview Tests for SDK method success paths to increase coverage. */ |
| 2 | + |
| 3 | +import nock from 'nock' |
| 4 | +import { describe, expect, it } from 'vitest' |
| 5 | + |
| 6 | +import { setupTestClient } from '../utils/environment.mts' |
| 7 | + |
| 8 | +describe('SocketSdk - Success Path Coverage', () => { |
| 9 | + const getClient = setupTestClient('test-api-token', { |
| 10 | + retries: 0, |
| 11 | + }) |
| 12 | + |
| 13 | + describe('Repository Management', () => { |
| 14 | + it('should successfully create a repository', async () => { |
| 15 | + nock('https://api.socket.dev') |
| 16 | + .post('/v0/orgs/test-org/repos') |
| 17 | + .reply(200, { data: { name: 'test-repo' } }) |
| 18 | + |
| 19 | + const result = await getClient().createRepository('test-org', { |
| 20 | + description: 'Test repository', |
| 21 | + name: 'test-repo', |
| 22 | + }) |
| 23 | + |
| 24 | + expect(result.success).toBe(true) |
| 25 | + }) |
| 26 | + |
| 27 | + it('should successfully delete a repository', async () => { |
| 28 | + nock('https://api.socket.dev') |
| 29 | + .delete('/v0/orgs/test-org/repos/test-repo') |
| 30 | + .reply(200, { success: true }) |
| 31 | + |
| 32 | + const result = await getClient().deleteRepository('test-org', 'test-repo') |
| 33 | + |
| 34 | + expect(result.success).toBe(true) |
| 35 | + }) |
| 36 | + |
| 37 | + it('should successfully get a repository', async () => { |
| 38 | + nock('https://api.socket.dev') |
| 39 | + .get('/v0/orgs/test-org/repos/test-repo') |
| 40 | + .reply(200, { data: { name: 'test-repo' } }) |
| 41 | + |
| 42 | + const result = await getClient().getRepository('test-org', 'test-repo') |
| 43 | + |
| 44 | + expect(result.success).toBe(true) |
| 45 | + }) |
| 46 | + |
| 47 | + it('should successfully update a repository', async () => { |
| 48 | + nock('https://api.socket.dev') |
| 49 | + .post('/v0/orgs/test-org/repos/test-repo') |
| 50 | + .reply(200, { data: { name: 'test-repo' } }) |
| 51 | + |
| 52 | + const result = await getClient().updateRepository( |
| 53 | + 'test-org', |
| 54 | + 'test-repo', |
| 55 | + { |
| 56 | + defaultBranch: 'develop', |
| 57 | + }, |
| 58 | + ) |
| 59 | + |
| 60 | + expect(result.success).toBe(true) |
| 61 | + }) |
| 62 | + |
| 63 | + it('should successfully list repositories', async () => { |
| 64 | + nock('https://api.socket.dev') |
| 65 | + .get('/v0/orgs/test-org/repos') |
| 66 | + .reply(200, { data: [] }) |
| 67 | + |
| 68 | + const result = await getClient().listRepositories('test-org') |
| 69 | + |
| 70 | + expect(result.success).toBe(true) |
| 71 | + }) |
| 72 | + }) |
| 73 | + |
| 74 | + describe('Repository Labels', () => { |
| 75 | + it('should successfully create a repository label', async () => { |
| 76 | + nock('https://api.socket.dev') |
| 77 | + .post('/v0/orgs/test-org/repos/labels') |
| 78 | + .reply(200, { data: { name: 'bug' } }) |
| 79 | + |
| 80 | + const result = await getClient().createRepositoryLabel('test-org', { |
| 81 | + color: '#FF0000', |
| 82 | + name: 'bug', |
| 83 | + }) |
| 84 | + |
| 85 | + expect(result.success).toBe(true) |
| 86 | + }) |
| 87 | + |
| 88 | + it('should successfully delete a repository label', async () => { |
| 89 | + nock('https://api.socket.dev') |
| 90 | + .delete('/v0/orgs/test-org/repos/labels/bug') |
| 91 | + .reply(200, { success: true }) |
| 92 | + |
| 93 | + const result = await getClient().deleteRepositoryLabel('test-org', 'bug') |
| 94 | + |
| 95 | + expect(result.success).toBe(true) |
| 96 | + }) |
| 97 | + |
| 98 | + it('should successfully get a repository label', async () => { |
| 99 | + nock('https://api.socket.dev') |
| 100 | + .get('/v0/orgs/test-org/repos/labels/bug') |
| 101 | + .reply(200, { data: { name: 'bug' } }) |
| 102 | + |
| 103 | + const result = await getClient().getRepositoryLabel('test-org', 'bug') |
| 104 | + |
| 105 | + expect(result.success).toBe(true) |
| 106 | + }) |
| 107 | + |
| 108 | + it('should successfully update a repository label', async () => { |
| 109 | + nock('https://api.socket.dev') |
| 110 | + .put('/v0/orgs/test-org/repos/labels/bug') |
| 111 | + .reply(200, { data: { name: 'bug' } }) |
| 112 | + |
| 113 | + const result = await getClient().updateRepositoryLabel( |
| 114 | + 'test-org', |
| 115 | + 'bug', |
| 116 | + { |
| 117 | + color: '#00FF00', |
| 118 | + }, |
| 119 | + ) |
| 120 | + |
| 121 | + expect(result.success).toBe(true) |
| 122 | + }) |
| 123 | + |
| 124 | + it('should successfully list repository labels', async () => { |
| 125 | + nock('https://api.socket.dev') |
| 126 | + .get('/v0/orgs/test-org/repos/labels') |
| 127 | + .reply(200, { data: [] }) |
| 128 | + |
| 129 | + const result = await getClient().listRepositoryLabels('test-org') |
| 130 | + |
| 131 | + expect(result.success).toBe(true) |
| 132 | + }) |
| 133 | + }) |
| 134 | + |
| 135 | + describe('Full Scans', () => { |
| 136 | + it('should successfully delete a full scan', async () => { |
| 137 | + nock('https://api.socket.dev') |
| 138 | + .delete('/v0/orgs/test-org/full-scans/scan-123') |
| 139 | + .reply(200, { success: true }) |
| 140 | + |
| 141 | + const result = await getClient().deleteFullScan('test-org', 'scan-123') |
| 142 | + |
| 143 | + expect(result.success).toBe(true) |
| 144 | + }) |
| 145 | + |
| 146 | + it('should successfully get a full scan', async () => { |
| 147 | + nock('https://api.socket.dev') |
| 148 | + .get('/v0/orgs/test-org/full-scans/scan-123') |
| 149 | + .reply(200, { data: { id: 'scan-123' } }) |
| 150 | + |
| 151 | + const result = await getClient().getFullScan('test-org', 'scan-123') |
| 152 | + |
| 153 | + expect(result.success).toBe(true) |
| 154 | + }) |
| 155 | + |
| 156 | + it('should successfully get full scan metadata', async () => { |
| 157 | + nock('https://api.socket.dev') |
| 158 | + .get('/v0/orgs/test-org/full-scans/scan-123/metadata') |
| 159 | + .reply(200, { data: { id: 'scan-123' } }) |
| 160 | + |
| 161 | + const result = await getClient().getFullScanMetadata( |
| 162 | + 'test-org', |
| 163 | + 'scan-123', |
| 164 | + ) |
| 165 | + |
| 166 | + expect(result.success).toBe(true) |
| 167 | + }) |
| 168 | + }) |
| 169 | + |
| 170 | + describe('Organizations', () => { |
| 171 | + it('should successfully list organizations', async () => { |
| 172 | + nock('https://api.socket.dev') |
| 173 | + .get('/v0/organizations') |
| 174 | + .reply(200, { data: [] }) |
| 175 | + |
| 176 | + const result = await getClient().listOrganizations() |
| 177 | + |
| 178 | + expect(result.success).toBe(true) |
| 179 | + }) |
| 180 | + }) |
| 181 | + |
| 182 | + describe('SBOM Export', () => { |
| 183 | + it('should successfully export SPDX', async () => { |
| 184 | + nock('https://api.socket.dev') |
| 185 | + .get('/v0/orgs/test-org/full-scans/scan-123/sbom/export/spdx') |
| 186 | + .reply(200, { data: { spdxVersion: 'SPDX-2.3' } }) |
| 187 | + |
| 188 | + const result = await getClient().exportSPDX('test-org', 'scan-123') |
| 189 | + |
| 190 | + expect(result.success).toBe(true) |
| 191 | + }) |
| 192 | + }) |
| 193 | + |
| 194 | + describe('Entitlements - Filter Logic', () => { |
| 195 | + it('should filter enabled entitlements with complex data', async () => { |
| 196 | + nock('https://api.socket.dev') |
| 197 | + .get('/v0/orgs/test-org/entitlements') |
| 198 | + .reply(200, { |
| 199 | + items: [ |
| 200 | + { enabled: true, key: 'firewall' }, |
| 201 | + { enabled: false, key: 'scanning' }, |
| 202 | + { enabled: true, key: 'alerts' }, |
| 203 | + // Test edge cases |
| 204 | + // Empty key should be filtered |
| 205 | + { enabled: true, key: '' }, |
| 206 | + { enabled: false, key: 'disabled' }, |
| 207 | + // Missing enabled property |
| 208 | + { key: 'no-enabled-prop' }, |
| 209 | + // Null item |
| 210 | + null, |
| 211 | + // Missing key property |
| 212 | + { enabled: true }, |
| 213 | + ], |
| 214 | + }) |
| 215 | + |
| 216 | + const result = await getClient().getEnabledEntitlements('test-org') |
| 217 | + |
| 218 | + // Should only return enabled items with non-empty keys |
| 219 | + expect(result).toEqual(['firewall', 'alerts']) |
| 220 | + }) |
| 221 | + |
| 222 | + it('should handle all disabled entitlements', async () => { |
| 223 | + nock('https://api.socket.dev') |
| 224 | + .get('/v0/orgs/test-org/entitlements') |
| 225 | + .reply(200, { |
| 226 | + items: [ |
| 227 | + { enabled: false, key: 'firewall' }, |
| 228 | + { enabled: false, key: 'scanning' }, |
| 229 | + ], |
| 230 | + }) |
| 231 | + |
| 232 | + const result = await getClient().getEnabledEntitlements('test-org') |
| 233 | + |
| 234 | + expect(result).toEqual([]) |
| 235 | + }) |
| 236 | + |
| 237 | + it('should handle entitlements with special characters', async () => { |
| 238 | + nock('https://api.socket.dev') |
| 239 | + .get('/v0/orgs/test-org/entitlements') |
| 240 | + .reply(200, { |
| 241 | + items: [ |
| 242 | + { enabled: true, key: 'fire-wall' }, |
| 243 | + { enabled: true, key: 'scan_ning' }, |
| 244 | + { enabled: true, key: 'alert.system' }, |
| 245 | + ], |
| 246 | + }) |
| 247 | + |
| 248 | + const result = await getClient().getEnabledEntitlements('test-org') |
| 249 | + |
| 250 | + expect(result).toEqual(['fire-wall', 'scan_ning', 'alert.system']) |
| 251 | + }) |
| 252 | + }) |
| 253 | +}) |
0 commit comments