|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Feature; |
| 4 | + |
| 5 | +use App\Models\Certificate; |
| 6 | +use App\Models\Challenge; |
| 7 | +use App\Models\ChallengeUser; |
| 8 | +use App\Models\User; |
| 9 | +use App\Models\UserActionPoints; |
| 10 | +use Illuminate\Foundation\Testing\RefreshDatabase; |
| 11 | +use Tests\TestCase; |
| 12 | + |
| 13 | +class UserProfileTest extends TestCase |
| 14 | +{ |
| 15 | + use RefreshDatabase; |
| 16 | + |
| 17 | + /** @test */ |
| 18 | + public function it_returns_a_user_profile_by_github_username(): void |
| 19 | + { |
| 20 | + $user = User::factory()->pro()->create([ |
| 21 | + 'github_user' => 'testuser', |
| 22 | + 'linkedin_user' => 'test-linkedin', |
| 23 | + ]); |
| 24 | + |
| 25 | + $challenge = Challenge::factory()->published()->create(); |
| 26 | + |
| 27 | + ChallengeUser::factory()->create([ |
| 28 | + 'user_id' => $user->id, |
| 29 | + 'challenge_id' => $challenge->id, |
| 30 | + 'submitted_at' => now(), |
| 31 | + 'listed' => true, |
| 32 | + ]); |
| 33 | + |
| 34 | + UserActionPoints::create([ |
| 35 | + 'user_id' => $user->id, |
| 36 | + 'action_name' => 'challenge_completed', |
| 37 | + 'points' => 100, |
| 38 | + ]); |
| 39 | + |
| 40 | + UserActionPoints::create([ |
| 41 | + 'user_id' => $user->id, |
| 42 | + 'action_name' => 'reaction_received', |
| 43 | + 'points' => 5, |
| 44 | + ]); |
| 45 | + |
| 46 | + $response = $this->getJson('/api/users/testuser/profile'); |
| 47 | + |
| 48 | + $response->assertOk() |
| 49 | + ->assertJsonStructure([ |
| 50 | + 'data' => [ |
| 51 | + 'user' => [ |
| 52 | + 'name', |
| 53 | + 'github_user', |
| 54 | + 'linkedin_user', |
| 55 | + 'avatar' => ['avatar_url', 'name', 'badge', 'github_user'], |
| 56 | + 'created_at', |
| 57 | + ], |
| 58 | + 'stats' => [ |
| 59 | + 'points', |
| 60 | + 'completed_challenge_count', |
| 61 | + 'received_reaction_count', |
| 62 | + ], |
| 63 | + 'completed_challenges', |
| 64 | + 'certificates', |
| 65 | + ], |
| 66 | + ]) |
| 67 | + ->assertJsonPath('data.user.github_user', 'testuser') |
| 68 | + ->assertJsonPath('data.user.linkedin_user', 'test-linkedin') |
| 69 | + ->assertJsonPath('data.user.avatar.badge', 'pro') |
| 70 | + ->assertJsonPath('data.stats.points', 105) |
| 71 | + ->assertJsonPath('data.stats.completed_challenge_count', 1) |
| 72 | + ->assertJsonPath('data.stats.received_reaction_count', 1); |
| 73 | + |
| 74 | + $this->assertCount(1, $response->json('data.completed_challenges')); |
| 75 | + } |
| 76 | + |
| 77 | + /** @test */ |
| 78 | + public function it_returns_404_for_nonexistent_user(): void |
| 79 | + { |
| 80 | + $response = $this->getJson('/api/users/nonexistent/profile'); |
| 81 | + |
| 82 | + $response->assertNotFound(); |
| 83 | + } |
| 84 | + |
| 85 | + /** @test */ |
| 86 | + public function it_filters_out_unlisted_submissions(): void |
| 87 | + { |
| 88 | + $user = User::factory()->create(['github_user' => 'filteruser']); |
| 89 | + $challenge1 = Challenge::factory()->published()->create(); |
| 90 | + $challenge2 = Challenge::factory()->published()->create(); |
| 91 | + |
| 92 | + ChallengeUser::factory()->create([ |
| 93 | + 'user_id' => $user->id, |
| 94 | + 'challenge_id' => $challenge1->id, |
| 95 | + 'submitted_at' => now(), |
| 96 | + 'listed' => true, |
| 97 | + ]); |
| 98 | + |
| 99 | + ChallengeUser::factory()->create([ |
| 100 | + 'user_id' => $user->id, |
| 101 | + 'challenge_id' => $challenge2->id, |
| 102 | + 'submitted_at' => now(), |
| 103 | + 'listed' => false, |
| 104 | + ]); |
| 105 | + |
| 106 | + $response = $this->getJson('/api/users/filteruser/profile'); |
| 107 | + |
| 108 | + $response->assertOk(); |
| 109 | + $this->assertCount(1, $response->json('data.completed_challenges')); |
| 110 | + } |
| 111 | + |
| 112 | + /** @test */ |
| 113 | + public function it_respects_show_badge_setting(): void |
| 114 | + { |
| 115 | + User::factory()->pro()->create([ |
| 116 | + 'github_user' => 'nobadge', |
| 117 | + 'settings' => ['show_badge' => false], |
| 118 | + ]); |
| 119 | + |
| 120 | + $response = $this->getJson('/api/users/nobadge/profile'); |
| 121 | + |
| 122 | + $response->assertOk() |
| 123 | + ->assertJsonPath('data.user.avatar.badge', null); |
| 124 | + } |
| 125 | + |
| 126 | + /** @test */ |
| 127 | + public function it_returns_empty_data_for_user_with_no_activity(): void |
| 128 | + { |
| 129 | + User::factory()->create(['github_user' => 'newuser']); |
| 130 | + |
| 131 | + $response = $this->getJson('/api/users/newuser/profile'); |
| 132 | + |
| 133 | + $response->assertOk() |
| 134 | + ->assertJsonPath('data.stats.points', 0) |
| 135 | + ->assertJsonPath('data.stats.completed_challenge_count', 0) |
| 136 | + ->assertJsonPath('data.stats.received_reaction_count', 0); |
| 137 | + |
| 138 | + $this->assertCount(0, $response->json('data.completed_challenges')); |
| 139 | + $this->assertCount(0, $response->json('data.certificates')); |
| 140 | + } |
| 141 | + |
| 142 | + /** @test */ |
| 143 | + public function it_returns_404_for_soft_deleted_user(): void |
| 144 | + { |
| 145 | + $user = User::factory()->create(['github_user' => 'deleteduser']); |
| 146 | + $user->delete(); |
| 147 | + |
| 148 | + $response = $this->getJson('/api/users/deleteduser/profile'); |
| 149 | + |
| 150 | + $response->assertNotFound(); |
| 151 | + } |
| 152 | + |
| 153 | + /** @test */ |
| 154 | + public function it_does_not_expose_email(): void |
| 155 | + { |
| 156 | + User::factory()->create([ |
| 157 | + 'github_user' => 'emailtest', |
| 158 | + 'email' => 'secret@example.com', |
| 159 | + ]); |
| 160 | + |
| 161 | + $response = $this->getJson('/api/users/emailtest/profile'); |
| 162 | + |
| 163 | + $response->assertOk(); |
| 164 | + $this->assertArrayNotHasKey('email', $response->json('data.user')); |
| 165 | + } |
| 166 | +} |
0 commit comments